1 import * as Sequelize from 'sequelize'
2 import { DataType } from 'sequelize-typescript'
4 async function up (utils: {
5 transaction: Sequelize.Transaction,
6 queryInterface: Sequelize.QueryInterface,
7 sequelize: Sequelize.Sequelize
13 CREATE TYPE enum_actor_type AS ENUM (
22 type enum_actor_type NOT NULL,
24 name character varying(255) NOT NULL,
25 url character varying(2000) NOT NULL,
26 "publicKey" character varying(5000),
27 "privateKey" character varying(5000),
28 "followersCount" integer NOT NULL,
29 "followingCount" integer NOT NULL,
30 "inboxUrl" character varying(2000) NOT NULL,
31 "outboxUrl" character varying(2000) NOT NULL,
32 "sharedInboxUrl" character varying(2000) NOT NULL,
33 "followersUrl" character varying(2000) NOT NULL,
34 "followingUrl" character varying(2000) NOT NULL,
37 "createdAt" timestamp with time zone NOT NULL,
38 "updatedAt" timestamp with time zone NOT NULL
41 CREATE SEQUENCE actor_id_seq
49 `ALTER SEQUENCE actor_id_seq OWNED BY actor.id`,
50 `ALTER TABLE ONLY actor ALTER COLUMN id SET DEFAULT nextval('actor_id_seq'::regclass)`,
51 `ALTER TABLE ONLY actor ADD CONSTRAINT actor_pkey PRIMARY KEY (id);`,
52 `CREATE UNIQUE INDEX actor_name_server_id ON actor USING btree (name, "serverId")`,
53 `ALTER TABLE ONLY actor
54 ADD CONSTRAINT "actor_avatarId_fkey" FOREIGN KEY ("avatarId") REFERENCES avatar(id) ON UPDATE CASCADE ON DELETE CASCADE`,
55 `ALTER TABLE ONLY actor
56 ADD CONSTRAINT "actor_serverId_fkey" FOREIGN KEY ("serverId") REFERENCES server(id) ON UPDATE CASCADE ON DELETE CASCADE;`
59 for (const query of queries) {
60 await utils.sequelize.query(query)
65 // tslint:disable:no-trailing-whitespace
70 type, uuid, name, url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
71 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
74 'Application', uuid, name, url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
75 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
77 WHERE "applicationId" IS NOT NULL
79 await utils.sequelize.query(query1)
85 type, uuid, name, url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
86 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
89 'Person', uuid, name, url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
90 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
92 WHERE "applicationId" IS NULL
94 await utils.sequelize.query(query2)
99 type: DataType.INTEGER,
103 await utils.queryInterface.addColumn('account', 'actorId', data)
105 const query1 = `UPDATE account SET "actorId" = (SELECT id FROM actor WHERE actor.url = account.url)`
106 await utils.sequelize.query(query1)
108 data.allowNull = false
109 await utils.queryInterface.changeColumn('account', 'actorId', data)
111 const query2 = `ALTER TABLE ONLY account
112 ADD CONSTRAINT "account_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;
114 await utils.sequelize.query(query2)
121 type, uuid, name, url, "publicKey", "privateKey", "followersCount", "followingCount", "inboxUrl", "outboxUrl",
122 "sharedInboxUrl", "followersUrl", "followingUrl", "avatarId", "serverId", "createdAt", "updatedAt"
125 'Group', "videoChannel".uuid, "videoChannel".uuid, "videoChannel".url, null, null, 0, 0, "videoChannel".url || '/inbox',
126 "videoChannel".url || '/outbox', "videoChannel".url || '/inbox', "videoChannel".url || '/followers', "videoChannel".url || '/following',
127 null, account."serverId", "videoChannel"."createdAt", "videoChannel"."updatedAt"
129 INNER JOIN "account" on "videoChannel"."accountId" = "account".id
131 await utils.sequelize.query(query)
136 type: DataType.INTEGER,
140 await utils.queryInterface.addColumn('videoChannel', 'actorId', data)
142 const query1 = `UPDATE "videoChannel" SET "actorId" = (SELECT id FROM actor WHERE actor.url = "videoChannel".url)`
143 await utils.sequelize.query(query1)
145 data.allowNull = false
146 await utils.queryInterface.changeColumn('videoChannel', 'actorId', data)
149 ALTER TABLE ONLY "videoChannel"
150 ADD CONSTRAINT "videoChannel_actorId_fkey" FOREIGN KEY ("actorId") REFERENCES actor(id) ON UPDATE CASCADE ON DELETE CASCADE;
152 await utils.sequelize.query(query2)
156 await utils.queryInterface.renameTable('accountFollow', 'actorFollow')
157 await utils.queryInterface.renameColumn('actorFollow', 'accountId', 'actorId')
158 await utils.queryInterface.renameColumn('actorFollow', 'targetAccountId', 'targetActorId')
161 await utils.queryInterface.removeConstraint('actorFollow', 'AccountFollows_accountId_fkey')
162 await utils.queryInterface.removeConstraint('actorFollow', 'AccountFollows_targetAccountId_fkey')
164 await utils.queryInterface.removeConstraint('actorFollow', 'accountFollow_accountId_fkey')
165 await utils.queryInterface.removeConstraint('actorFollow', 'accountFollow_targetAccountId_fkey')
168 const query1 = `UPDATE "actorFollow"
170 (SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."actorId")`
171 await utils.sequelize.query(query1)
173 const query2 = `UPDATE "actorFollow"
174 SET "targetActorId" =
175 (SELECT "account"."actorId" FROM account WHERE "account"."id" = "actorFollow"."targetActorId")`
177 await utils.sequelize.query(query2)
181 await utils.queryInterface.renameColumn('videoShare', 'accountId', 'actorId')
184 await utils.queryInterface.removeConstraint('videoShare', 'VideoShares_accountId_fkey')
186 await utils.queryInterface.removeConstraint('videoShare', 'videoShare_accountId_fkey')
189 const query = `UPDATE "videoShare"
191 (SELECT "actorId" FROM account WHERE id = "videoShare"."actorId")`
192 await utils.sequelize.query(query)
196 const columnsToDelete = [
211 for (const columnToDelete of columnsToDelete) {
212 await utils.queryInterface.removeColumn('account', columnToDelete)
217 const columnsToDelete = [
222 for (const columnToDelete of columnsToDelete) {
223 await utils.queryInterface.removeColumn('videoChannel', columnToDelete)
228 function down (options) {
229 throw new Error('Not implemented.')