diff --git a/server/migrations/20191118023010-cascade-delete.js b/server/migrations/20191118023010-cascade-delete.js index 3b686f80a..9c02d8931 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 8b2ed5222..af1873b20 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 68999d257..079a0a14e 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 bb40d3e25..35c9f665d 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 b687a7eb5..ae34473ba 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