fix(migration): support PostgreSQL safe drop constraint in foreign key migration
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 11s
All checks were successful
Deploy Beta (NATIVE) / deploy (push) Successful in 11s
This commit is contained in:
parent
6657d90d1c
commit
a974249e6e
1 changed files with 11 additions and 14 deletions
|
|
@ -31,20 +31,17 @@ const tables = [
|
|||
|
||||
exports.up = async function (knex) {
|
||||
for (const table of tables) {
|
||||
try {
|
||||
await knex.schema.alterTable(table.name, (t) => {
|
||||
t.dropForeign("user_id");
|
||||
});
|
||||
} catch (error) {
|
||||
// Not sure why it only happens on Ubuntu 22.04 + Node.js 26
|
||||
// Error: Can't DROP 'docker_host_user_id_foreign'; check that column/key exists
|
||||
console.error(`Error dropping foreign key for table ${table.name}:`, error);
|
||||
|
||||
const columns = await knex(table.name).columnInfo();
|
||||
console.error(`Schema for ${table.name}:`, JSON.stringify(columns, null, 2));
|
||||
|
||||
// Try to ignore it, because the foreign key not existing, we can just drop user_id
|
||||
// throw error;
|
||||
const isPostgres = knex.client.dialect === "postgresql" || knex.client.config.client === "pg";
|
||||
if (isPostgres) {
|
||||
await knex.raw(`ALTER TABLE "${table.name}" DROP CONSTRAINT IF EXISTS "${table.name}_user_id_foreign"`);
|
||||
} else {
|
||||
try {
|
||||
await knex.schema.alterTable(table.name, (t) => {
|
||||
t.dropForeign("user_id");
|
||||
});
|
||||
} catch (error) {
|
||||
console.error(`Error dropping foreign key for table ${table.name}:`, error.message);
|
||||
}
|
||||
}
|
||||
|
||||
await knex.schema.alterTable(table.name, (t) => {
|
||||
|
|
|
|||
Loading…
Reference in a new issue