|
|
@ -2,8 +2,8 @@ import axios from 'axios'; |
|
|
|
import Knex from 'knex'; |
|
|
|
import Knex from 'knex'; |
|
|
|
|
|
|
|
|
|
|
|
import { promises as fs } from 'fs'; |
|
|
|
import { promises as fs } from 'fs'; |
|
|
|
import Audit from '../../../models/Audit'; |
|
|
|
|
|
|
|
import { sakilaTableNames } from '../../../utils/globals'; |
|
|
|
import { sakilaTableNames } from '../../../utils/globals'; |
|
|
|
|
|
|
|
import Project from '../../../models/Project'; |
|
|
|
|
|
|
|
|
|
|
|
const sqliteSakilaSqlViews = [ |
|
|
|
const sqliteSakilaSqlViews = [ |
|
|
|
'actor_info', |
|
|
|
'actor_info', |
|
|
@ -15,6 +15,55 @@ const sqliteSakilaSqlViews = [ |
|
|
|
'staff_list', |
|
|
|
'staff_list', |
|
|
|
]; |
|
|
|
]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const resetMetaSakilaSqliteProject = async ({ |
|
|
|
|
|
|
|
metaKnex, |
|
|
|
|
|
|
|
token, |
|
|
|
|
|
|
|
title, |
|
|
|
|
|
|
|
oldProject, |
|
|
|
|
|
|
|
}: { |
|
|
|
|
|
|
|
metaKnex: Knex; |
|
|
|
|
|
|
|
token: string; |
|
|
|
|
|
|
|
title: string; |
|
|
|
|
|
|
|
oldProject: Project; |
|
|
|
|
|
|
|
}) => { |
|
|
|
|
|
|
|
const project = await createProject(token, title); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await dropTablesAndViews(metaKnex, oldProject.prefix); |
|
|
|
|
|
|
|
await dropTablesAndViews(metaKnex, project.prefix); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await resetMetaSakilaSqlite(metaKnex, project.prefix, oldProject); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
await syncMeta(project, token); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const createProject = async (token: string, title: string) => { |
|
|
|
|
|
|
|
const response = await axios.post( |
|
|
|
|
|
|
|
'http://localhost:8080/api/v1/db/meta/projects/', |
|
|
|
|
|
|
|
{ title }, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
headers: { |
|
|
|
|
|
|
|
'xc-auth': token, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
if (response.status !== 200) { |
|
|
|
|
|
|
|
console.error('Error creating project', response.data); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
return response.data; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const syncMeta = async (project: Project, token: string) => { |
|
|
|
|
|
|
|
await axios.post( |
|
|
|
|
|
|
|
`http://localhost:8080/api/v1/db/meta/projects/${project.id}/meta-diff`, |
|
|
|
|
|
|
|
{}, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
headers: { |
|
|
|
|
|
|
|
'xc-auth': token, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const dropTablesAndViews = async (metaKnex: Knex, prefix: string) => { |
|
|
|
const dropTablesAndViews = async (metaKnex: Knex, prefix: string) => { |
|
|
|
try { |
|
|
|
try { |
|
|
|
for (const view of sqliteSakilaSqlViews) { |
|
|
|
for (const view of sqliteSakilaSqlViews) { |
|
|
@ -29,25 +78,12 @@ const dropTablesAndViews = async (metaKnex: Knex, prefix: string) => { |
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const isMetaSakilaSqliteToBeReset = async (metaKnex: Knex, project: any) => { |
|
|
|
const resetMetaSakilaSqlite = async ( |
|
|
|
const tablesInDb: Array<string> = await metaKnex.raw( |
|
|
|
metaKnex: Knex, |
|
|
|
`SELECT name FROM sqlite_master WHERE type='table' AND name LIKE '${project.prefix}%'` |
|
|
|
prefix: string, |
|
|
|
); |
|
|
|
oldProject: Project |
|
|
|
|
|
|
|
) => { |
|
|
|
if ( |
|
|
|
await dropTablesAndViews(metaKnex, oldProject.prefix); |
|
|
|
tablesInDb.length === 0 || |
|
|
|
|
|
|
|
(tablesInDb.length > 0 && !tablesInDb.includes(`${project.prefix}actor`)) |
|
|
|
|
|
|
|
) { |
|
|
|
|
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const audits = await Audit.projectAuditList(project.id, {}); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return audits?.length > 0; |
|
|
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const resetMetaSakilaSqlite = async (metaKnex: Knex, prefix: string) => { |
|
|
|
|
|
|
|
await dropTablesAndViews(metaKnex, prefix); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const testsDir = __dirname.replace( |
|
|
|
const testsDir = __dirname.replace( |
|
|
|
'/src/lib/services/test/TestResetService', |
|
|
|
'/src/lib/services/test/TestResetService', |
|
|
@ -74,62 +110,35 @@ const resetMetaSakilaSqlite = async (metaKnex: Knex, prefix: string) => { |
|
|
|
console.error('Error resetting meta sakila sqlite:db', e); |
|
|
|
console.error('Error resetting meta sakila sqlite:db', e); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
const trx = await metaKnex.transaction(); |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
const dataFile = await fs.readFile( |
|
|
|
const dataFile = await fs.readFile( |
|
|
|
`${testsDir}/sqlite-sakila-db/04-sqlite-prefix-sakila-insert-data.sql` |
|
|
|
`${testsDir}/sqlite-sakila-db/04-sqlite-prefix-sakila-insert-data.sql` |
|
|
|
); |
|
|
|
); |
|
|
|
const dataFileStr = dataFile.toString().replace(/prefix___/g, prefix); |
|
|
|
const dataFileStr = dataFile.toString().replace(/prefix___/g, prefix); |
|
|
|
const dataSqlQueries = dataFileStr.split(';'); |
|
|
|
const dataSqlQueries = dataFileStr |
|
|
|
|
|
|
|
.split(';') |
|
|
|
for (const sqlQuery of dataSqlQueries) { |
|
|
|
.filter((str) => str.trim().length > 0) |
|
|
|
if (sqlQuery.trim().length > 0) { |
|
|
|
.map((str) => str.trim()); |
|
|
|
await trx.raw(sqlQuery.trim()); |
|
|
|
|
|
|
|
} |
|
|
|
const batchSize = 1000; |
|
|
|
} |
|
|
|
const batches = dataSqlQueries.reduce((acc, _, i) => { |
|
|
|
await trx.commit(); |
|
|
|
if (!(i % batchSize)) { |
|
|
|
} catch (e) { |
|
|
|
// if index is 0 or can be divided by the `size`...
|
|
|
|
console.log('Error resetting sqlite db', e); |
|
|
|
acc.push(dataSqlQueries.slice(i, i + batchSize)); // ..push a chunk of the original array to the accumulator
|
|
|
|
await trx.rollback(e); |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
}; |
|
|
|
return acc; |
|
|
|
|
|
|
|
}, []); |
|
|
|
|
|
|
|
|
|
|
|
const resetMetaSakilaSqliteProject = async ({ |
|
|
|
for (const sqlQueryBatch of batches) { |
|
|
|
metaKnex, |
|
|
|
const trx = await metaKnex.transaction(); |
|
|
|
token, |
|
|
|
|
|
|
|
title, |
|
|
|
|
|
|
|
}: { |
|
|
|
|
|
|
|
metaKnex: Knex; |
|
|
|
|
|
|
|
token: string; |
|
|
|
|
|
|
|
title: string; |
|
|
|
|
|
|
|
}) => { |
|
|
|
|
|
|
|
const response = await axios.post( |
|
|
|
|
|
|
|
'http://localhost:8080/api/v1/db/meta/projects/', |
|
|
|
|
|
|
|
{ title }, |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
headers: { |
|
|
|
|
|
|
|
'xc-auth': token, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
); |
|
|
|
|
|
|
|
if (response.status !== 200) { |
|
|
|
|
|
|
|
console.error('Error creating project', response.data); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
const project = response.data; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (await isMetaSakilaSqliteToBeReset(metaKnex, project)) { |
|
|
|
for (const sqlQuery of sqlQueryBatch) { |
|
|
|
await resetMetaSakilaSqlite(metaKnex, project.prefix); |
|
|
|
await trx.raw(sqlQuery); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await axios.post( |
|
|
|
await trx.commit(); |
|
|
|
`http://localhost:8080/api/v1/db/meta/projects/${project.id}/meta-diff`, |
|
|
|
// wait for 40 ms to avoid SQLITE_BUSY error
|
|
|
|
{}, |
|
|
|
await new Promise((resolve) => setTimeout(resolve, 40)); |
|
|
|
{ |
|
|
|
|
|
|
|
headers: { |
|
|
|
|
|
|
|
'xc-auth': token, |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
); |
|
|
|
|
|
|
|
}; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export default resetMetaSakilaSqliteProject; |
|
|
|
export default resetMetaSakilaSqliteProject; |
|
|
|