Skip to content

Commit

Permalink
Fix migration for sqlite to not throw away all constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
knolleary committed Oct 5, 2023
1 parent 846ed4d commit 78795c5
Showing 1 changed file with 15 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,21 @@ module.exports = {
up: async (context) => {
const dialect = context.sequelize.options.dialect
if (dialect === 'sqlite') {
// Disable foreign_keys so that when sequelize does its sqlite-specific
// copy/drop workaround for renaming columns, we don't cause cascade
// delete on any other tables
await context.sequelize.query('PRAGMA foreign_keys = 0;')
}

await context.changeColumn('ProjectSnapshots', 'ProjectId', {
type: DataTypes.UUID,
allowNull: true
})

if (dialect === 'sqlite') {
await context.sequelize.query('PRAGMA foreign_keys = 1;')
// We have to do this the hard way due to limitations of sqlite and
// changing table constraints. The changeColumn approach causes all
// table constraints to be lost. We can keep them on the one column
// we're modifying, but it also strips them for the others.
await context.sequelize.query('pragma writable_schema=1;')
const sql = "update SQLITE_MASTER set sql = replace(sql, '`ProjectId` UUID NOT NULL', '`ProjectId` UUID') where name = 'ProjectSnapshots' and type = 'table';"
context.sequelize.query(sql)
await context.sequelize.query('pragma writable_schema=0;')
} else {
// Postgres allows us to modify a constraint without breaking all
// the other properties
await context.changeColumn('ProjectSnapshots', 'ProjectId', {
type: DataTypes.UUID,
allowNull: true
})
}
},
down: async (context) => {
Expand Down

0 comments on commit 78795c5

Please sign in to comment.