Skip to content

Commit

Permalink
Fix cascade delete quotes tags migration to await on temp table creat…
Browse files Browse the repository at this point in the history
…ion (#116)
  • Loading branch information
gavrielrh authored May 3, 2020
1 parent 62558fa commit c0fc542
Showing 1 changed file with 34 additions and 28 deletions.
62 changes: 34 additions & 28 deletions db/migrations/20171231073953-cascade-delete-quotes-tags.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// Add ON DELETE CASCADE and ON UPDATE CASCADE to the quotes_tags table
export function up(queryInterface, Sequelize) {
export async function up(queryInterface, Sequelize) {
const dialect = queryInterface.sequelize.getDialect();
// SQLite does not support the DROP CONSTRAINT syntax,
// so we have to make a temporary table, copy all the data into it,
// and then rename it to the original table. Since we use SQLite in
// development and testing with small amounts of data, while this is
// gross, it is not necessarily the end of the world.
if (dialect === 'sqlite') {
return queryInterface.sequelize.transaction(t => Promise.all([
queryInterface.createTable('temp', {
return queryInterface.sequelize.transaction(async (t) => {
await queryInterface.createTable('temp', {
tagName: {
type: Sequelize.STRING,
allowNull: false,
Expand All @@ -33,17 +33,20 @@ export function up(queryInterface, Sequelize) {
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
}, { transaction: t }),
queryInterface
.sequelize
.query('INSERT INTO temp SELECT * FROM quotes_tags;', { transaction: t }),
queryInterface
.sequelize
.query('DROP TABLE quotes_tags;', { transaction: t }),
queryInterface
.sequelize
.query('ALTER TABLE temp RENAME TO quotes_tags;', { transaction: t }),
]));
}, {transaction: t});

return Promise.all([
queryInterface
.sequelize
.query('INSERT INTO temp SELECT * FROM quotes_tags;', {transaction: t}),
queryInterface
.sequelize
.query('DROP TABLE quotes_tags;', {transaction: t}),
queryInterface
.sequelize
.query('ALTER TABLE temp RENAME TO quotes_tags;', {transaction: t}),
]);
});
} else if (dialect === 'postgres') {
return queryInterface.sequelize.transaction(t => Promise.all([
queryInterface
Expand Down Expand Up @@ -71,12 +74,12 @@ export function up(queryInterface, Sequelize) {
}

// Remove ON DELETE CASCADE and ON UPDATE CASCADE FROM the quotes_tags table
export function down(queryInterface, Sequelize) {
export async function down(queryInterface, Sequelize) {
const dialect = queryInterface.sequelize.getDialect();
// See above comment about SQLite
if (dialect === 'sqlite') {
return queryInterface.sequelize.transaction(t => Promise.all([
queryInterface.createTable('temp', {
return queryInterface.sequelize.transaction(async (t) => {
await queryInterface.createTable('temp', {
tagName: {
type: Sequelize.STRING,
allowNull: false,
Expand All @@ -91,17 +94,20 @@ export function down(queryInterface, Sequelize) {
},
createdAt: Sequelize.DATE,
updatedAt: Sequelize.DATE,
}, { transaction: t }),
queryInterface
.sequelize
.query('INSERT INTO temp SELECT * FROM quotes_tags;', { transaction: t }),
queryInterface
.sequelize
.query('DROP TABLE quotes_tags;', { transaction: t }),
queryInterface
.sequelize
.query('ALTER TABLE temp RENAME TO quotes_tags;', { transaction: t }),
]));
}, { transaction: t });

return Promise.all([
queryInterface
.sequelize
.query('INSERT INTO temp SELECT * FROM quotes_tags;', { transaction: t }),
queryInterface
.sequelize
.query('DROP TABLE quotes_tags;', { transaction: t }),
queryInterface
.sequelize
.query('ALTER TABLE temp RENAME TO quotes_tags;', { transaction: t }),
]);
});
} else if (dialect === 'postgres') {
return queryInterface.sequelize.transaction(t => Promise.all([
queryInterface
Expand Down

0 comments on commit c0fc542

Please sign in to comment.