Drop foreign keys before dropping tables

This commit is contained in:
Alexandre Bodin 2021-08-06 11:19:17 +02:00
parent 07ae49badd
commit 189c819a1e
3 changed files with 31 additions and 12 deletions

View File

@ -5,7 +5,7 @@ services:
image: postgres
restart: always
volumes:
- pgdata:/var/lib/postgresql/data
- pgdata_test:/var/lib/postgresql/data
environment:
POSTGRES_USER: strapi
POSTGRES_PASSWORD: strapi
@ -24,10 +24,10 @@ services:
MYSQL_ROOT_HOST: '%'
MYSQL_ROOT_PASSWORD: strapi
volumes:
- mysqldata:/var/lib/mysql
- mysqldata_test:/var/lib/mysql
ports:
- '3306:3306'
volumes:
pgdata:
mysqldata:
pgdata_test:
mysqldata_test:

View File

@ -72,13 +72,18 @@ module.exports = db => ({
await db.connection.transaction(async trx => {
await this.createTables(schemaDiff.tables.added, trx);
// TODO: drop foreign keys targeting delete tables
// drop all delete table foreign keys then delete the tables
for (const table of schemaDiff.tables.removed) {
debug(`Removing table foreign keys: ${table.name}`);
const schemaBuilder = this.getSchemaBuilder(table, trx);
await dropTableForeignKeys(schemaBuilder, table);
}
for (const table of schemaDiff.tables.removed) {
debug(`Removing table: ${table.name}`);
const schemaBuilder = this.getSchemaBuilder(table, trx);
// TODO: add cascading if possible
await dropTable(schemaBuilder, table);
}
@ -288,6 +293,8 @@ const dropColumn = (tableBuilder, column) => {
const createTable = async (schemaBuilder, table) => {
if (await schemaBuilder.hasTable(table.name)) {
throw new Error(`Table already exists ${table.name}`);
// TODO: alter the table instead
}
await schemaBuilder.createTable(table.name, tableBuilder => {
@ -299,6 +306,13 @@ const createTable = async (schemaBuilder, table) => {
});
};
/**
* Drops a table from a database
* @param {Knex.SchemaBuilder} schemaBuilder
* @param {Table} table
*/
const dropTable = (schemaBuilder, table) => schemaBuilder.dropTableIfExists(table.name);
/**
* Creates a table foreign keys constraints
* @param {SchemaBuilder} schemaBuilder
@ -312,8 +326,13 @@ const createTableForeignKeys = async (schemaBuilder, table) => {
};
/**
* Drops a table from a database
* @param {Knex.SchemaBuilder} schemaBuilder
* Drops a table foreign keys constraints
* @param {SchemaBuilder} schemaBuilder
* @param {Table} table
*/
const dropTable = (schemaBuilder, table) => schemaBuilder.dropTableIfExists(table.name);
const dropTableForeignKeys = async (schemaBuilder, table) => {
// foreign keys
await schemaBuilder.table(table.name, tableBuilder => {
(table.foreignKeys || []).forEach(foreignKey => dropForeignKey(tableBuilder, foreignKey));
});
};

View File

@ -141,7 +141,7 @@ async function createFixtures(dataMap, { strapi: strapiIst } = {}) {
const entries = [];
for (const data of dataMap[model]) {
entries.push(await strapi.query(`application::${model}.${model}`).create({ data }));
entries.push(await strapi.entityService.create(`application::${model}.${model}`, { data }));
}
resultMap[model] = entries;
@ -159,7 +159,7 @@ async function createFixturesFor(model, entries, { strapi: strapiIst } = {}) {
for (const entry of entries) {
const dataToCreate = isFunction(entry) ? entry(results) : entry;
results.push(
await strapi.query(`application::${model}.${model}`).create({ data: dataToCreate })
await strapi.entityService.create(`application::${model}.${model}`, { data: dataToCreate })
);
}
@ -173,7 +173,7 @@ async function deleteFixturesFor(model, entries, { strapi: strapiIst } = {}) {
await strapi
.query(`application::${model}.${model}`)
.delete({ where: { id: entries.map(prop('id')) } });
.deleteMany({ where: { id: entries.map(prop('id')) } });
await cleanup();
}