mirror of https://github.com/nocodb/nocodb
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
117 lines
2.7 KiB
117 lines
2.7 KiB
2 years ago
|
import Audit from '../../../src/lib/models/Audit';
|
||
|
import Project from '../../../src/lib/models/Project';
|
||
2 years ago
|
import TestDbMngr from '../TestDbMngr';
|
||
2 years ago
|
|
||
2 years ago
|
const dropTablesOfSakila = async () => {
|
||
|
await TestDbMngr.disableForeignKeyChecks(TestDbMngr.sakilaKnex);
|
||
|
|
||
2 years ago
|
for (const tableName of sakilaTableNames) {
|
||
2 years ago
|
try {
|
||
2 years ago
|
if (TestDbMngr.isPg()) {
|
||
|
await TestDbMngr.sakilaKnex.raw(
|
||
|
`DROP TABLE IF EXISTS "${tableName}" CASCADE`
|
||
|
);
|
||
|
} else {
|
||
|
await TestDbMngr.sakilaKnex.raw(`DROP TABLE ${tableName}`);
|
||
|
}
|
||
|
} catch (e) {}
|
||
2 years ago
|
}
|
||
2 years ago
|
await TestDbMngr.enableForeignKeyChecks(TestDbMngr.sakilaKnex);
|
||
2 years ago
|
};
|
||
|
|
||
|
const dropSchemaAndSeedSakila = async () => {
|
||
|
try {
|
||
|
await TestDbMngr.sakilaKnex.raw(`DROP SCHEMA "public" CASCADE`);
|
||
|
await TestDbMngr.sakilaKnex.raw(`CREATE SCHEMA "public"`);
|
||
|
await TestDbMngr.seedSakila();
|
||
|
} catch (e) {
|
||
|
console.error('dropSchemaAndSeedSakila', e);
|
||
|
throw e;
|
||
|
}
|
||
|
};
|
||
2 years ago
|
|
||
2 years ago
|
const resetAndSeedSakila = async () => {
|
||
2 years ago
|
try {
|
||
2 years ago
|
await dropTablesOfSakila();
|
||
|
await TestDbMngr.seedSakila();
|
||
2 years ago
|
} catch (e) {
|
||
|
console.error('resetSakila', e);
|
||
2 years ago
|
throw e;
|
||
2 years ago
|
}
|
||
2 years ago
|
};
|
||
2 years ago
|
|
||
2 years ago
|
const cleanUpSakila = async () => {
|
||
2 years ago
|
try {
|
||
2 years ago
|
const sakilaProject = await Project.getByTitle('sakila');
|
||
|
|
||
2 years ago
|
const audits =
|
||
|
sakilaProject && (await Audit.projectAuditList(sakilaProject.id, {}));
|
||
2 years ago
|
|
||
2 years ago
|
if (audits?.length > 0) {
|
||
|
// if PG, drop schema
|
||
|
if (TestDbMngr.isPg()) {
|
||
|
return await dropSchemaAndSeedSakila();
|
||
|
}
|
||
|
// if mysql, drop tables
|
||
2 years ago
|
return await resetAndSeedSakila();
|
||
2 years ago
|
}
|
||
|
|
||
2 years ago
|
const tablesInSakila = await TestDbMngr.showAllTables(
|
||
|
TestDbMngr.sakilaKnex
|
||
|
);
|
||
2 years ago
|
|
||
|
await Promise.all(
|
||
|
tablesInSakila
|
||
|
.filter((tableName) => !sakilaTableNames.includes(tableName))
|
||
|
.map(async (tableName) => {
|
||
|
try {
|
||
2 years ago
|
if (TestDbMngr.isPg()) {
|
||
|
await TestDbMngr.sakilaKnex.raw(
|
||
|
`DROP TABLE "${tableName}" CASCADE`
|
||
|
);
|
||
|
} else {
|
||
|
await TestDbMngr.sakilaKnex.raw(`DROP TABLE ${tableName}`);
|
||
|
}
|
||
2 years ago
|
} catch (e) {
|
||
|
console.error(e);
|
||
|
}
|
||
|
})
|
||
|
);
|
||
|
} catch (e) {
|
||
|
console.error('cleanUpSakila', e);
|
||
|
}
|
||
|
};
|
||
|
|
||
2 years ago
|
const sakilaTableNames = [
|
||
|
'actor',
|
||
|
'address',
|
||
|
'category',
|
||
|
'city',
|
||
|
'country',
|
||
|
'customer',
|
||
|
'film',
|
||
|
'film_actor',
|
||
|
'film_category',
|
||
|
'inventory',
|
||
|
'language',
|
||
|
'payment',
|
||
2 years ago
|
'payment_p2007_01',
|
||
|
'payment_p2007_02',
|
||
|
'payment_p2007_03',
|
||
|
'payment_p2007_04',
|
||
|
'payment_p2007_05',
|
||
|
'payment_p2007_06',
|
||
2 years ago
|
'rental',
|
||
|
'staff',
|
||
|
'store',
|
||
|
'actor_info',
|
||
|
'customer_list',
|
||
|
'film_list',
|
||
|
'nicer_but_slower_film_list',
|
||
|
'sales_by_film_category',
|
||
|
'sales_by_store',
|
||
|
'staff_list',
|
||
|
];
|
||
|
|
||
2 years ago
|
export { cleanUpSakila, resetAndSeedSakila };
|