migration refactor for any plattform

This commit is contained in:
Julian Krauser 2024-08-25 17:36:25 +02:00
parent 7df7cf2697
commit b3d1c2d729
4 changed files with 176 additions and 47 deletions

View file

@ -1,18 +1,93 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm";
export class Initial1724317398939 implements MigrationInterface {
name = 'Initial1724317398939'
name = "Initial1724317398939";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE \`user\` (\`id\` int NOT NULL AUTO_INCREMENT, \`mail\` varchar(255) NOT NULL, \`username\` varchar(255) NOT NULL, \`secret\` varchar(255) NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`CREATE TABLE \`refresh\` (\`id\` int NOT NULL AUTO_INCREMENT, \`token\` varchar(255) NOT NULL, \`expiry\` datetime NOT NULL, \`userId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "user",
columns: [
{
name: "id",
type: "int",
isPrimary: true,
isNullable: false,
isGenerated: true,
generationStrategy: "increment",
},
{
name: "mail",
type: "varchar",
length: "255",
isNullable: false,
},
{
name: "username",
type: "varchar",
length: "255",
isNullable: false,
},
{
name: "secret",
type: "varchar",
length: "255",
isNullable: false,
},
],
}),
true
);
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``);
await queryRunner.query(`DROP TABLE \`refresh\``);
await queryRunner.query(`DROP TABLE \`user\``);
}
await queryRunner.createTable(
new Table({
name: "refresh",
columns: [
{
name: "id",
type: "int",
isPrimary: true,
isNullable: false,
isGenerated: true,
generationStrategy: "increment",
},
{
name: "token",
type: "varchar",
length: "255",
isNullable: false,
},
{
name: "expiry",
type: "datetime",
isNullable: false,
},
{
name: "userId",
type: "int",
isNullable: true,
},
],
}),
true
);
await queryRunner.createForeignKey(
"refresh",
new TableForeignKey({
columnNames: ["userId"],
referencedColumnNames: ["id"],
referencedTableName: "user",
onDelete: "No Action",
})
);
}
public async down(queryRunner: QueryRunner): Promise<void> {
const table = await queryRunner.getTable("refresh");
const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.indexOf("userId") !== -1);
await queryRunner.dropForeignKey("refresh", foreignKey);
await queryRunner.dropTable("refresh");
await queryRunner.dropTable("user");
}
}

View file

@ -1,26 +1,25 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MigrationInterface, QueryRunner, TableColumn } from "typeorm";
export class RefreshPrimaryChange1724573307851 implements MigrationInterface {
name = 'RefreshPrimaryChange1724573307851'
name = "RefreshPrimaryChange1724573307851";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`id\` \`id\` int NOT NULL`);
await queryRunner.query(`ALTER TABLE \`refresh\` DROP PRIMARY KEY`);
await queryRunner.query(`ALTER TABLE \`refresh\` DROP COLUMN \`id\``);
await queryRunner.query(`ALTER TABLE \`refresh\` ADD PRIMARY KEY (\`token\`, \`userId\`)`);
await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``);
await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`userId\` \`userId\` int NOT NULL`);
await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``);
await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`userId\` \`userId\` int NULL DEFAULT 'NULL'`);
await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE \`refresh\` DROP PRIMARY KEY`);
await queryRunner.query(`ALTER TABLE \`refresh\` ADD \`id\` int NOT NULL AUTO_INCREMENT`);
await queryRunner.query(`ALTER TABLE \`refresh\` ADD PRIMARY KEY (\`id\`)`);
await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`id\` \`id\` int NOT NULL AUTO_INCREMENT`);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropColumn("refresh", "id");
await queryRunner.createPrimaryKey("refresh", ["token", "userId"]);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropPrimaryKey("refresh");
await queryRunner.addColumn(
"refresh",
new TableColumn({
name: "id",
type: "int",
isPrimary: true,
isNullable: false,
isGenerated: true,
generationStrategy: "increment",
})
);
}
}

View file

@ -1,18 +1,73 @@
import { MigrationInterface, QueryRunner } from "typeorm";
import { MigrationInterface, QueryRunner, Table, TableColumn } from "typeorm";
export class Invite1724579024939 implements MigrationInterface {
name = 'Invite1724579024939'
name = "Invite1724579024939";
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE \`invite\` (\`mail\` varchar(255) NOT NULL, \`token\` varchar(255) NOT NULL, \`username\` varchar(255) NOT NULL, \`firstname\` varchar(255) NOT NULL, \`lastname\` varchar(255) NOT NULL, \`secret\` varchar(255) NOT NULL, PRIMARY KEY (\`mail\`)) ENGINE=InnoDB`);
await queryRunner.query(`ALTER TABLE \`user\` ADD \`firstname\` varchar(255) NOT NULL`);
await queryRunner.query(`ALTER TABLE \`user\` ADD \`lastname\` varchar(255) NOT NULL`);
}
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.createTable(
new Table({
name: "invite",
columns: [
{
name: "mail",
type: "varchar",
length: "255",
isPrimary: true,
isNullable: false,
},
{
name: "token",
type: "varchar",
length: "255",
isNullable: false,
},
{
name: "username",
type: "varchar",
length: "255",
isNullable: false,
},
{
name: "firstname",
type: "varchar",
length: "255",
isNullable: false,
},
{
name: "lastname",
type: "varchar",
length: "255",
isNullable: false,
},
{
name: "secret",
type: "varchar",
length: "255",
isNullable: false,
},
],
}),
true
);
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`lastname\``);
await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`firstname\``);
await queryRunner.query(`DROP TABLE \`invite\``);
}
await queryRunner.addColumns("user", [
new TableColumn({
name: "firstname",
type: "varchar",
length: "255",
isNullable: false,
}),
new TableColumn({
name: "lastname",
type: "varchar",
length: "255",
isNullable: false,
}),
]);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.dropColumns("user", ["lastname", "firstname"]);
await queryRunner.dropTable("invite");
}
}