diff --git a/server/migrations/20191118023010-cascade-delete.js b/server/migrations/20191118023010-cascade-delete.js index 3b686f80..9c02d893 100644 --- a/server/migrations/20191118023010-cascade-delete.js +++ b/server/migrations/20191118023010-cascade-delete.js @@ -1,22 +1,43 @@ const tableName = 'revisions'; -const constraintName = 'revisions_documentId_fkey'; + +// because of this issue in Sequelize the foreign key constraint may be named differently depending +// on when the previous migrations were ran https://github.com/sequelize/sequelize/pull/9890 +const constraintNames = ['revisions_documentId_fkey', 'documentId_foreign_idx']; module.exports = { up: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}" - add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") - on delete cascade` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}" + add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") + on delete cascade` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, down: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}"\ - add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") - on delete no action` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}"\ + add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") + on delete no action` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, }; \ No newline at end of file diff --git a/server/migrations/20191119023010-cascade-backlinks.js b/server/migrations/20191119023010-cascade-backlinks.js index 8b2ed522..af1873b2 100644 --- a/server/migrations/20191119023010-cascade-backlinks.js +++ b/server/migrations/20191119023010-cascade-backlinks.js @@ -1,22 +1,43 @@ const tableName = 'backlinks'; -const constraintName = 'backlinks_reverseDocumentId_fkey'; + +// because of this issue in Sequelize the foreign key constraint may be named differently depending +// on when the previous migrations were ran https://github.com/sequelize/sequelize/pull/9890 +const constraintNames = ['backlinks_reverseDocumentId_fkey', 'reverseDocumentId_foreign_idx']; module.exports = { up: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}" - add constraint "${constraintName}" foreign key("reverseDocumentId") references "documents" ("id") - on delete cascade` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}" + add constraint "${constraintName}" foreign key("reverseDocumentId") references "documents" ("id") + on delete cascade` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, down: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}"\ - add constraint "${constraintName}" foreign key("reverseDocumentId") references "documents" ("id") - on delete no action` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}"\ + add constraint "${constraintName}" foreign key("reverseDocumentId") references "documents" ("id") + on delete no action` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, }; \ No newline at end of file diff --git a/server/migrations/20191119023011-cascade-parent-documents.js b/server/migrations/20191119023011-cascade-parent-documents.js index 68999d25..079a0a14 100644 --- a/server/migrations/20191119023011-cascade-parent-documents.js +++ b/server/migrations/20191119023011-cascade-parent-documents.js @@ -1,22 +1,43 @@ const tableName = 'documents'; -const constraintName = 'documents_parentDocumentId_fkey'; + +// because of this issue in Sequelize the foreign key constraint may be named differently depending +// on when the previous migrations were ran https://github.com/sequelize/sequelize/pull/9890 +const constraintNames = ['documents_parentDocumentId_fkey', 'parentDocumentId_foreign_idx']; module.exports = { up: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}" - add constraint "${constraintName}" foreign key("parentDocumentId") references "documents" ("id") - on delete cascade` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}" + add constraint "${constraintName}" foreign key("parentDocumentId") references "documents" ("id") + on delete cascade` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, down: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}"\ - add constraint "${constraintName}" foreign key("parentDocumentId") references "documents" ("id") - on delete no action` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}"\ + add constraint "${constraintName}" foreign key("parentDocumentId") references "documents" ("id") + on delete no action` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, }; \ No newline at end of file diff --git a/server/migrations/20191119023012-cascade-shares.js b/server/migrations/20191119023012-cascade-shares.js index bb40d3e2..35c9f665 100644 --- a/server/migrations/20191119023012-cascade-shares.js +++ b/server/migrations/20191119023012-cascade-shares.js @@ -1,22 +1,43 @@ const tableName = 'shares'; -const constraintName = 'shares_documentId_fkey'; + +// because of this issue in Sequelize the foreign key constraint may be named differently depending +// on when the previous migrations were ran https://github.com/sequelize/sequelize/pull/9890 +const constraintNames = ['shares_documentId_fkey', 'documentId_foreign_idx']; module.exports = { up: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}" - add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") - on delete cascade` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}" + add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") + on delete cascade` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, down: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}"\ - add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") - on delete no action` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}"\ + add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") + on delete no action` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, }; \ No newline at end of file diff --git a/server/migrations/20191119023013-cascade-backlinks2.js b/server/migrations/20191119023013-cascade-backlinks2.js index b687a7eb..ae34473b 100644 --- a/server/migrations/20191119023013-cascade-backlinks2.js +++ b/server/migrations/20191119023013-cascade-backlinks2.js @@ -1,22 +1,43 @@ const tableName = 'backlinks'; -const constraintName = 'backlinks_documentId_fkey'; + +// because of this issue in Sequelize the foreign key constraint may be named differently depending +// on when the previous migrations were ran https://github.com/sequelize/sequelize/pull/9890 +const constraintNames = ['backlinks_documentId_fkey', 'documentId_foreign_idx']; module.exports = { up: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}" - add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") - on delete cascade` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}" + add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") + on delete cascade` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, down: async (queryInterface, Sequelize) => { - await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) - await queryInterface.sequelize.query( - `alter table "${tableName}"\ - add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") - on delete no action` - ); + let error; + for (const constraintName of constraintNames) { + try { + await queryInterface.sequelize.query(`alter table "${tableName}" drop constraint "${constraintName}"`) + await queryInterface.sequelize.query( + `alter table "${tableName}"\ + add constraint "${constraintName}" foreign key("documentId") references "documents" ("id") + on delete no action` + ); + return; + } catch (err) { + error = err; + } + } + throw error; }, }; \ No newline at end of file