Add foreign key migration

pull/161/head
Chocobozzz 2017-12-19 10:50:57 +01:00
parent e12a009254
commit c6bf1bb656
No known key found for this signature in database
GPG Key ID: 583A612D890159BE
1 changed files with 36 additions and 17 deletions

View File

@ -99,7 +99,11 @@ async function up (utils: {
const data = { const data = {
type: DataType.INTEGER, type: DataType.INTEGER,
allowNull: true, allowNull: true,
defaultValue: null references: {
model: 'actor',
key: 'id'
},
onDelete: 'CASCADE'
} }
await utils.queryInterface.addColumn('account', 'actorId', data) await utils.queryInterface.addColumn('account', 'actorId', data)
@ -108,11 +112,6 @@ async function up (utils: {
data.allowNull = false data.allowNull = false
await utils.queryInterface.changeColumn('account', 'actorId', data) await utils.queryInterface.changeColumn('account', 'actorId', data)
const query2 = `ALTER TABLE ONLY account
ADD CONSTRAINT "account_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;
`
await utils.sequelize.query(query2)
} }
{ {
@ -136,7 +135,11 @@ async function up (utils: {
const data = { const data = {
type: DataType.INTEGER, type: DataType.INTEGER,
allowNull: true, allowNull: true,
defaultValue: null references: {
model: 'actor',
key: 'id'
},
onDelete: 'CASCADE'
} }
await utils.queryInterface.addColumn('videoChannel', 'actorId', data) await utils.queryInterface.addColumn('videoChannel', 'actorId', data)
@ -145,12 +148,6 @@ async function up (utils: {
data.allowNull = false data.allowNull = false
await utils.queryInterface.changeColumn('videoChannel', 'actorId', data) await utils.queryInterface.changeColumn('videoChannel', 'actorId', data)
const query2 = `
ALTER TABLE ONLY "videoChannel"
ADD CONSTRAINT "videoChannel_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;
`
await utils.sequelize.query(query2)
} }
{ {
@ -166,16 +163,28 @@ async function up (utils: {
await utils.queryInterface.removeConstraint('actorFollow', 'accountFollow_targetAccountId_fkey') await utils.queryInterface.removeConstraint('actorFollow', 'accountFollow_targetAccountId_fkey')
} }
const query1 = `UPDATE "actorFollow" {
const query1 = `UPDATE "actorFollow"
SET "actorId" = SET "actorId" =
(SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."actorId")` (SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."actorId")`
await utils.sequelize.query(query1) await utils.sequelize.query(query1)
const query2 = `UPDATE "actorFollow" const query2 = `UPDATE "actorFollow"
SET "targetActorId" = SET "targetActorId" =
(SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."targetActorId")` (SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."targetActorId")`
await utils.sequelize.query(query2) await utils.sequelize.query(query2)
}
{
const query1 = `ALTER TABLE ONLY "actorFollow"
ADD CONSTRAINT "actorFollow_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;`
await utils.sequelize.query(query1)
const query2 = `ALTER TABLE ONLY "actorFollow"
ADD CONSTRAINT "actorFollow_targetActorId_fkey" FOREIGN KEY ("targetActorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;`
await utils.sequelize.query(query2)
}
} }
{ {
@ -191,6 +200,16 @@ async function up (utils: {
SET "actorId" = SET "actorId" =
(SELECT "actorId" FROM account WHERE id = "videoShare"."actorId")` (SELECT "actorId" FROM account WHERE id = "videoShare"."actorId")`
await utils.sequelize.query(query) await utils.sequelize.query(query)
{
const query1 = `ALTER TABLE ONLY "videoShare"
ADD CONSTRAINT "videoShare_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;`
await utils.sequelize.query(query1)
const query2 = `ALTER TABLE ONLY "videoShare"
ADD CONSTRAINT "videoShare_videoId_fkey" FOREIGN KEY ("videoId") REFERENCES video(id) ON UPDATE CASCADE ON DELETE CASCADE;`
await utils.sequelize.query(query2)
}
} }
{ {