-
Notifications
You must be signed in to change notification settings - Fork 67
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2899 from FlowFuse/fix-db-constraint
Remove not null constraint on ProjectSnapshot.ProjectId column
- Loading branch information
Showing
1 changed file
with
29 additions
and
0 deletions.
There are no files selected for viewing
29 changes: 29 additions & 0 deletions
29
forge/db/migrations/20231005-01-update-projectSnapshot-constraint.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* Remove not null on ProjectSnapshots.ProjectId | ||
*/ | ||
const { DataTypes } = require('sequelize') | ||
|
||
module.exports = { | ||
up: async (context) => { | ||
const dialect = context.sequelize.options.dialect | ||
if (dialect === 'sqlite') { | ||
// 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) => { | ||
} | ||
} |