1 import * as Sequelize from 'sequelize'
2 import { DataType } from 'sequelize-typescript'
3 import { createPrivateAndPublicKeys } from '../../helpers'
5 async function up (utils: {
6 transaction: Sequelize.Transaction,
7 queryInterface: Sequelize.QueryInterface,
8 sequelize: Sequelize.Sequelize
14 CREATE TYPE enum_actor_type AS ENUM (
23 type enum_actor_type NOT NULL,
25 "preferredUsername" character varying(255) NOT NULL,
26 url character varying(2000) NOT NULL,
27 "publicKey" character varying(5000),
28 "privateKey" character varying(5000),
29 "followersCount" integer NOT NULL,
30 "followingCount" integer NOT NULL,
31 "inboxUrl" character varying(2000) NOT NULL,
32 "outboxUrl" character varying(2000) NOT NULL,
33 "sharedInboxUrl" character varying(2000) NOT NULL,
34 "followersUrl" character varying(2000) NOT NULL,
35 "followingUrl" character varying(2000) NOT NULL,
38 "createdAt" timestamp with time zone NOT NULL,
39 "updatedAt" timestamp with time zone NOT NULL
42 CREATE SEQUENCE actor_id_seq
50 `ALTER SEQUENCE actor_id_seq OWNED BY actor.id`,
51 `ALTER TABLE ONLY actor ALTER COLUMN id SET DEFAULT nextval('actor_id_seq'::regclass)`,
52 `ALTER TABLE ONLY actor ADD CONSTRAINT actor_pkey PRIMARY KEY (id);`,
53 `CREATE UNIQUE INDEX actor_preferred_username_server_id ON actor USING btree ("preferredUsername", "serverId")`,
54 `ALTER TABLE ONLY actor
55 ADD CONSTRAINT "actor_avatarId_fkey" FOREIGN KEY ("avatarId") REFERENCES avatar(id) ON UPDATE CASCADE ON DELETE CASCADE`,
56 `ALTER TABLE ONLY actor
57 ADD CONSTRAINT "actor_serverId_fkey" FOREIGN KEY ("serverId") REFERENCES server(id) ON UPDATE CASCADE ON DELETE CASCADE;`
60 for (const query of queries) {
61 await utils.sequelize.query(query)
66 // tslint:disable:no-trailing-whitespace
71 type, uuid, "preferredUsername", url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
72 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
75 'Application', uuid, name, url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
76 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
78 WHERE "applicationId" IS NOT NULL
80 await utils.sequelize.query(query1)
86 type, uuid, "preferredUsername", url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
87 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
90 'Person', uuid, name, url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
91 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
93 WHERE "applicationId" IS NULL
95 await utils.sequelize.query(query2)
100 type: DataType.INTEGER,
104 await utils.queryInterface.addColumn('account', 'actorId', data)
106 const query1 = `UPDATE account SET "actorId" = (SELECT id FROM actor WHERE actor.url = account.url)`
107 await utils.sequelize.query(query1)
109 data.allowNull = false
110 await utils.queryInterface.changeColumn('account', 'actorId', data)
112 const query2 = `ALTER TABLE ONLY account
113 ADD CONSTRAINT "account_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;
115 await utils.sequelize.query(query2)
122 type, uuid, "preferredUsername", url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
123 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
126 'Group', "videoChannel".uuid, "videoChannel".uuid, "videoChannel".url, null, null, 0, 0, "videoChannel".url || '/inbox',
127 "videoChannel".url || '/outbox', "videoChannel".url || '/inbox', "videoChannel".url || '/followers', "videoChannel".url || '/following',
128 null, account."serverId", "videoChannel"."createdAt", "videoChannel"."updatedAt"
130 INNER JOIN "account" on "videoChannel"."accountId" = "account".id
132 await utils.sequelize.query(query)
137 type: DataType.INTEGER,
141 await utils.queryInterface.addColumn('videoChannel', 'actorId', data)
143 const query1 = `UPDATE "videoChannel" SET "actorId" = (SELECT id FROM actor WHERE actor.url = "videoChannel".url)`
144 await utils.sequelize.query(query1)
146 data.allowNull = false
147 await utils.queryInterface.changeColumn('videoChannel', 'actorId', data)
150 ALTER TABLE ONLY "videoChannel"
151 ADD CONSTRAINT "videoChannel_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;
153 await utils.sequelize.query(query2)
157 await utils.queryInterface.renameTable('accountFollow', 'actorFollow')
158 await utils.queryInterface.renameColumn('actorFollow', 'accountId', 'actorId')
159 await utils.queryInterface.renameColumn('actorFollow', 'targetAccountId', 'targetActorId')
162 await utils.queryInterface.removeConstraint('actorFollow', 'AccountFollows_accountId_fkey')
163 await utils.queryInterface.removeConstraint('actorFollow', 'AccountFollows_targetAccountId_fkey')
165 await utils.queryInterface.removeConstraint('actorFollow', 'accountFollow_accountId_fkey')
166 await utils.queryInterface.removeConstraint('actorFollow', 'accountFollow_targetAccountId_fkey')
169 const query1 = `UPDATE "actorFollow"
171 (SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."actorId")`
172 await utils.sequelize.query(query1)
174 const query2 = `UPDATE "actorFollow"
175 SET "targetActorId" =
176 (SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."targetActorId")`
178 await utils.sequelize.query(query2)
182 await utils.queryInterface.renameColumn('videoShare', 'accountId', 'actorId')
185 await utils.queryInterface.removeConstraint('videoShare', 'VideoShares_accountId_fkey')
187 await utils.queryInterface.removeConstraint('videoShare', 'videoShare_accountId_fkey')
190 const query = `UPDATE "videoShare"
192 (SELECT "actorId" FROM account WHERE id = "videoShare"."actorId")`
193 await utils.sequelize.query(query)
197 const columnsToDelete = [
212 for (const columnToDelete of columnsToDelete) {
213 await utils.queryInterface.removeColumn('account', columnToDelete)
218 const columnsToDelete = [
223 for (const columnToDelete of columnsToDelete) {
224 await utils.queryInterface.removeColumn('videoChannel', columnToDelete)
229 const query = 'SELECT * FROM "actor" WHERE "serverId" IS NULL AND "publicKey" IS NULL'
230 const [ res ] = await utils.sequelize.query(query)
232 for (const actor of res) {
233 const { privateKey, publicKey } = await createPrivateAndPublicKeys()
235 const queryUpdate = `UPDATE "actor" SET "publicKey" = '${publicKey}', "privateKey" = '${privateKey}' WHERE id = ${actor.id}`
236 await utils.sequelize.query(queryUpdate)
241 function down (options) {
242 throw new Error('Not implemented.')