icynet-auth-server/src/migration/1662799286155-initial.ts

297 lines
17 KiB
TypeScript

import { MigrationInterface, QueryRunner } from 'typeorm';
export class initial1662799286155 implements MigrationInterface {
name = 'initial1662799286155';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`CREATE TABLE \`privilege\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` text NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`upload\` (\`id\` int NOT NULL AUTO_INCREMENT, \`original_name\` varchar(255) NOT NULL, \`mimetype\` varchar(255) NOT NULL, \`file\` varchar(255) NOT NULL, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`uploaderId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`user\` (\`id\` int NOT NULL AUTO_INCREMENT, \`uuid\` varchar(36) NOT NULL, \`username\` varchar(26) NOT NULL, \`email\` varchar(255) NOT NULL, \`display_name\` varchar(32) NOT NULL, \`password\` text NULL, \`activated\` tinyint NOT NULL DEFAULT 0, \`activity_at\` timestamp NOT NULL, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`pictureId\` int NULL, UNIQUE INDEX \`IDX_a95e949168be7b7ece1a2382fe\` (\`uuid\`), UNIQUE INDEX \`IDX_78a916df40e02a9deb1c4b75ed\` (\`username\`), UNIQUE INDEX \`IDX_e12875dfb3b1d92d7d7c5377e2\` (\`email\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`audit_log\` (\`id\` int NOT NULL AUTO_INCREMENT, \`action\` text NOT NULL, \`content\` text NULL, \`actor_ip\` text NULL, \`actor_ua\` text NULL, \`flagged\` tinyint NOT NULL DEFAULT 0, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`actorId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`document\` (\`id\` int NOT NULL AUTO_INCREMENT, \`title\` text NOT NULL, \`slug\` text NOT NULL, \`body\` text NOT NULL, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`authorId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`o_auth2_client_url\` (\`id\` int NOT NULL AUTO_INCREMENT, \`url\` varchar(255) NOT NULL, \`type\` enum ('redirect_uri', 'terms', 'privacy', 'website') NOT NULL, \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`clientId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`o_auth2_client\` (\`id\` int NOT NULL AUTO_INCREMENT, \`client_id\` varchar(36) NOT NULL, \`client_secret\` text NOT NULL, \`title\` varchar(255) NOT NULL, \`description\` text NULL, \`scope\` text NULL, \`grants\` text NOT NULL DEFAULT 'authorization_code', \`activated\` tinyint NOT NULL DEFAULT 0, \`verified\` tinyint NOT NULL DEFAULT 0, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`pictureId\` int NULL, \`ownerId\` int NULL, UNIQUE INDEX \`IDX_e9d16c213910ad57bd05e97b42\` (\`client_id\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`o_auth2_client_authorization\` (\`id\` int NOT NULL AUTO_INCREMENT, \`scope\` text NULL, \`expires_at\` timestamp NOT NULL, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`clientId\` int NULL, \`userId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`o_auth2_token\` (\`id\` int NOT NULL AUTO_INCREMENT, \`type\` enum ('code', 'access_token', 'refresh_token') NOT NULL, \`token\` text NOT NULL, \`scope\` text NULL, \`expires_at\` timestamp NOT NULL, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`userId\` int NULL, \`clientId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`user_token\` (\`id\` int NOT NULL AUTO_INCREMENT, \`token\` text NOT NULL, \`type\` enum ('generic', 'activation', 'deactivation', 'password', 'login', 'gdpr', 'totp', 'recovery') NOT NULL, \`expires_at\` timestamp NULL, \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`userId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`CREATE TABLE \`user_privileges_privilege\` (\`userId\` int NOT NULL, \`privilegeId\` int NOT NULL, INDEX \`IDX_0664a7ff494a1859a09014c0f1\` (\`userId\`), INDEX \`IDX_e71171f4ed20bc8564a1819d0b\` (\`privilegeId\`), PRIMARY KEY (\`userId\`, \`privilegeId\`)) ENGINE=InnoDB`,
);
await queryRunner.query(
`ALTER TABLE \`upload\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`upload\` ADD \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`upload\` DROP COLUMN \`updated_at\``,
);
await queryRunner.query(
`ALTER TABLE \`upload\` ADD \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`created_at\``);
await queryRunner.query(
`ALTER TABLE \`user\` ADD \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`updated_at\``);
await queryRunner.query(
`ALTER TABLE \`user\` ADD \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`document\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`document\` ADD \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`document\` DROP COLUMN \`updated_at\``,
);
await queryRunner.query(
`ALTER TABLE \`document\` ADD \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` ADD \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` DROP COLUMN \`updated_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` ADD \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_authorization\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_authorization\` ADD \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP COLUMN \`updated_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD \`updated_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`user_token\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`user_token\` ADD \`created_at\` timestamp(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`upload\` ADD CONSTRAINT \`FK_7b8d52838a953b188255682597b\` FOREIGN KEY (\`uploaderId\`) REFERENCES \`user\`(\`id\`) ON DELETE SET NULL ON UPDATE CASCADE`,
);
await queryRunner.query(
`ALTER TABLE \`user\` ADD CONSTRAINT \`FK_7478a15985dbfa32ed5fc77a7a1\` FOREIGN KEY (\`pictureId\`) REFERENCES \`upload\`(\`id\`) ON DELETE SET NULL ON UPDATE CASCADE`,
);
await queryRunner.query(
`ALTER TABLE \`audit_log\` ADD CONSTRAINT \`FK_cb6aa6f6fd56f08eafb60316225\` FOREIGN KEY (\`actorId\`) REFERENCES \`user\`(\`id\`) ON DELETE SET NULL ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`document\` ADD CONSTRAINT \`FK_6a2eb13cadfc503989cbe367572\` FOREIGN KEY (\`authorId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_url\` ADD CONSTRAINT \`FK_aca59c7bdd65987487eea98d00f\` FOREIGN KEY (\`clientId\`) REFERENCES \`o_auth2_client\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` ADD CONSTRAINT \`FK_e8d65b1eec13474e493420517d7\` FOREIGN KEY (\`pictureId\`) REFERENCES \`upload\`(\`id\`) ON DELETE SET NULL ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` ADD CONSTRAINT \`FK_4a6c878506b872e85b3d07f6252\` FOREIGN KEY (\`ownerId\`) REFERENCES \`user\`(\`id\`) ON DELETE SET NULL ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_authorization\` ADD CONSTRAINT \`FK_9ca9ebb654e7ce71954d5fdb281\` FOREIGN KEY (\`clientId\`) REFERENCES \`o_auth2_client\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_authorization\` ADD CONSTRAINT \`FK_8227110f58510b7233f3db90cfb\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD CONSTRAINT \`FK_81ffb9b8d672cf3af1af9e789f3\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD CONSTRAINT \`FK_3ecb760b321ef9bbab635f05b45\` FOREIGN KEY (\`clientId\`) REFERENCES \`o_auth2_client\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`user_token\` ADD CONSTRAINT \`FK_d37db50eecdf9b8ce4eedd2f918\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`,
);
await queryRunner.query(
`ALTER TABLE \`user_privileges_privilege\` ADD CONSTRAINT \`FK_0664a7ff494a1859a09014c0f17\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`,
);
await queryRunner.query(
`ALTER TABLE \`user_privileges_privilege\` ADD CONSTRAINT \`FK_e71171f4ed20bc8564a1819d0b7\` FOREIGN KEY (\`privilegeId\`) REFERENCES \`privilege\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`,
);
await queryRunner.query(
`INSERT INTO \`privilege\` (name) VALUES ('admin'), ('admin:user'), ('admin:user:privilege'), ('admin:oauth2'), ('admin:audit'), ('admin:document'), ('self:oauth2')`,
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`ALTER TABLE \`user_privileges_privilege\` DROP FOREIGN KEY \`FK_e71171f4ed20bc8564a1819d0b7\``,
);
await queryRunner.query(
`ALTER TABLE \`user_privileges_privilege\` DROP FOREIGN KEY \`FK_0664a7ff494a1859a09014c0f17\``,
);
await queryRunner.query(
`ALTER TABLE \`user_token\` DROP FOREIGN KEY \`FK_d37db50eecdf9b8ce4eedd2f918\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP FOREIGN KEY \`FK_3ecb760b321ef9bbab635f05b45\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP FOREIGN KEY \`FK_81ffb9b8d672cf3af1af9e789f3\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_authorization\` DROP FOREIGN KEY \`FK_8227110f58510b7233f3db90cfb\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_authorization\` DROP FOREIGN KEY \`FK_9ca9ebb654e7ce71954d5fdb281\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` DROP FOREIGN KEY \`FK_4a6c878506b872e85b3d07f6252\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` DROP FOREIGN KEY \`FK_e8d65b1eec13474e493420517d7\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_url\` DROP FOREIGN KEY \`FK_aca59c7bdd65987487eea98d00f\``,
);
await queryRunner.query(
`ALTER TABLE \`document\` DROP FOREIGN KEY \`FK_6a2eb13cadfc503989cbe367572\``,
);
await queryRunner.query(
`ALTER TABLE \`audit_log\` DROP FOREIGN KEY \`FK_cb6aa6f6fd56f08eafb60316225\``,
);
await queryRunner.query(
`ALTER TABLE \`user\` DROP FOREIGN KEY \`FK_7478a15985dbfa32ed5fc77a7a1\``,
);
await queryRunner.query(
`ALTER TABLE \`upload\` DROP FOREIGN KEY \`FK_7b8d52838a953b188255682597b\``,
);
await queryRunner.query(
`ALTER TABLE \`user_token\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`user_token\` ADD \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP COLUMN \`updated_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_token\` ADD \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_authorization\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client_authorization\` ADD \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` DROP COLUMN \`updated_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` ADD \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`o_auth2_client\` ADD \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`document\` DROP COLUMN \`updated_at\``,
);
await queryRunner.query(
`ALTER TABLE \`document\` ADD \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`document\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`document\` ADD \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`updated_at\``);
await queryRunner.query(
`ALTER TABLE \`user\` ADD \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`created_at\``);
await queryRunner.query(
`ALTER TABLE \`user\` ADD \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`upload\` DROP COLUMN \`updated_at\``,
);
await queryRunner.query(
`ALTER TABLE \`upload\` ADD \`updated_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`ALTER TABLE \`upload\` DROP COLUMN \`created_at\``,
);
await queryRunner.query(
`ALTER TABLE \`upload\` ADD \`created_at\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6)`,
);
await queryRunner.query(
`DROP INDEX \`IDX_e71171f4ed20bc8564a1819d0b\` ON \`user_privileges_privilege\``,
);
await queryRunner.query(
`DROP INDEX \`IDX_0664a7ff494a1859a09014c0f1\` ON \`user_privileges_privilege\``,
);
await queryRunner.query(`DROP TABLE \`user_privileges_privilege\``);
await queryRunner.query(`DROP TABLE \`user_token\``);
await queryRunner.query(`DROP TABLE \`o_auth2_token\``);
await queryRunner.query(`DROP TABLE \`o_auth2_client_authorization\``);
await queryRunner.query(
`DROP INDEX \`IDX_e9d16c213910ad57bd05e97b42\` ON \`o_auth2_client\``,
);
await queryRunner.query(`DROP TABLE \`o_auth2_client\``);
await queryRunner.query(`DROP TABLE \`o_auth2_client_url\``);
await queryRunner.query(`DROP TABLE \`document\``);
await queryRunner.query(`DROP TABLE \`audit_log\``);
await queryRunner.query(
`DROP INDEX \`IDX_e12875dfb3b1d92d7d7c5377e2\` ON \`user\``,
);
await queryRunner.query(
`DROP INDEX \`IDX_78a916df40e02a9deb1c4b75ed\` ON \`user\``,
);
await queryRunner.query(
`DROP INDEX \`IDX_a95e949168be7b7ece1a2382fe\` ON \`user\``,
);
await queryRunner.query(`DROP TABLE \`user\``);
await queryRunner.query(`DROP TABLE \`upload\``);
await queryRunner.query(`DROP TABLE \`privilege\``);
}
}