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.
62 lines
1.8 KiB
62 lines
1.8 KiB
2 years ago
|
import Model from '../../../src/lib/models/Model';
|
||
|
import Project from '../../../src/lib/models/Project';
|
||
|
import NcConnectionMgrv2 from '../../../src/lib/utils/common/NcConnectionMgrv2';
|
||
|
import { orderedMetaTables } from '../../../src/lib/utils/globals';
|
||
|
import TestDbMngr from '../TestDbMngr';
|
||
|
import { isPg } from './db';
|
||
2 years ago
|
|
||
2 years ago
|
const dropTablesAllNonExternalProjects = async () => {
|
||
2 years ago
|
const projects = await Project.list({});
|
||
2 years ago
|
const userCreatedTableNames: string[] = [];
|
||
2 years ago
|
await Promise.all(
|
||
2 years ago
|
projects
|
||
|
.filter((project) => project.is_meta)
|
||
|
.map(async (project) => {
|
||
|
await project.getBases();
|
||
|
const base = project.bases && project.bases[0];
|
||
|
if (!base) return;
|
||
2 years ago
|
|
||
2 years ago
|
const models = await Model.list({
|
||
|
project_id: project.id,
|
||
2 years ago
|
base_id: base.id!,
|
||
2 years ago
|
});
|
||
|
models.forEach((model) => {
|
||
|
userCreatedTableNames.push(model.table_name);
|
||
|
});
|
||
|
})
|
||
2 years ago
|
);
|
||
|
|
||
2 years ago
|
await TestDbMngr.disableForeignKeyChecks(TestDbMngr.metaKnex);
|
||
|
|
||
2 years ago
|
for (const tableName of userCreatedTableNames) {
|
||
2 years ago
|
if (TestDbMngr.isPg()) {
|
||
|
await TestDbMngr.metaKnex.raw(`DROP TABLE "${tableName}" CASCADE`);
|
||
|
} else {
|
||
|
await TestDbMngr.metaKnex.raw(`DROP TABLE ${tableName}`);
|
||
|
}
|
||
2 years ago
|
}
|
||
2 years ago
|
|
||
|
await TestDbMngr.enableForeignKeyChecks(TestDbMngr.metaKnex);
|
||
2 years ago
|
};
|
||
|
|
||
2 years ago
|
const cleanupMetaTables = async () => {
|
||
|
await TestDbMngr.disableForeignKeyChecks(TestDbMngr.metaKnex);
|
||
2 years ago
|
for (const tableName of orderedMetaTables) {
|
||
|
try {
|
||
2 years ago
|
await TestDbMngr.metaKnex.raw(`DELETE FROM ${tableName}`);
|
||
2 years ago
|
} catch (e) {}
|
||
|
}
|
||
2 years ago
|
await TestDbMngr.enableForeignKeyChecks(TestDbMngr.metaKnex);
|
||
2 years ago
|
};
|
||
|
|
||
2 years ago
|
export default async function () {
|
||
2 years ago
|
try {
|
||
2 years ago
|
await NcConnectionMgrv2.destroyAll();
|
||
|
|
||
2 years ago
|
await dropTablesAllNonExternalProjects();
|
||
|
await cleanupMetaTables();
|
||
2 years ago
|
} catch (e) {
|
||
|
console.error('cleanupMeta', e);
|
||
|
}
|
||
|
}
|