migration refactor for any plattform
This commit is contained in:
parent
7df7cf2697
commit
b3d1c2d729
4 changed files with 176 additions and 47 deletions
|
@ -23,8 +23,8 @@ Login is possible via Username and TOTP.
|
||||||
|
|
||||||
1. Install the database-system-package you like (e.g. mysql, mariadb, postgresql, sqlite3)
|
1. Install the database-system-package you like (e.g. mysql, mariadb, postgresql, sqlite3)
|
||||||
2. Configure type inside src/data-source.ts to run the database-system you like.
|
2. Configure type inside src/data-source.ts to run the database-system you like.
|
||||||
3. Set migrationsRun to false and synchronize to true. (Migrations are build to suit mysql)
|
3. Set migrationsRun to false and synchronize to true for rapid prototyping
|
||||||
4. Building the schema via CLI:
|
4. Building the schema via CLI:
|
||||||
- Run `npm run update-database` to build the schema using the migrations without starting the application
|
- Run `npm run update-database` to build the schema using the migrations without starting the application
|
||||||
- Run `npm run synchronize-database` to build the schema without using migrations without starting the application
|
- Run `npm run synchronize-database` to build the schema without using migrations without starting the application
|
||||||
5. Run `npm run dev` to run inside dev-environment
|
5. Run `npm run dev` to run inside dev-environment (runs migrations if migrationsRun is set to true)
|
||||||
|
|
|
@ -1,18 +1,93 @@
|
||||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
import { MigrationInterface, QueryRunner, Table, TableForeignKey } from "typeorm";
|
||||||
|
|
||||||
export class Initial1724317398939 implements MigrationInterface {
|
export class Initial1724317398939 implements MigrationInterface {
|
||||||
name = 'Initial1724317398939'
|
name = "Initial1724317398939";
|
||||||
|
|
||||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
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.createTable(
|
||||||
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`);
|
new Table({
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
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> {
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``);
|
const table = await queryRunner.getTable("refresh");
|
||||||
await queryRunner.query(`DROP TABLE \`refresh\``);
|
const foreignKey = table.foreignKeys.find((fk) => fk.columnNames.indexOf("userId") !== -1);
|
||||||
await queryRunner.query(`DROP TABLE \`user\``);
|
await queryRunner.dropForeignKey("refresh", foreignKey);
|
||||||
|
await queryRunner.dropTable("refresh");
|
||||||
|
await queryRunner.dropTable("user");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,26 +1,25 @@
|
||||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
import { MigrationInterface, QueryRunner, TableColumn } from "typeorm";
|
||||||
|
|
||||||
export class RefreshPrimaryChange1724573307851 implements MigrationInterface {
|
export class RefreshPrimaryChange1724573307851 implements MigrationInterface {
|
||||||
name = 'RefreshPrimaryChange1724573307851'
|
name = "RefreshPrimaryChange1724573307851";
|
||||||
|
|
||||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
public async up(queryRunner: QueryRunner): Promise<void> {
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`id\` \`id\` int NOT NULL`);
|
await queryRunner.dropColumn("refresh", "id");
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` DROP PRIMARY KEY`);
|
await queryRunner.createPrimaryKey("refresh", ["token", "userId"]);
|
||||||
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> {
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` DROP FOREIGN KEY \`FK_b39e4ed3bfa789758e476870ec2\``);
|
await queryRunner.dropPrimaryKey("refresh");
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`userId\` \`userId\` int NULL DEFAULT 'NULL'`);
|
await queryRunner.addColumn(
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` ADD CONSTRAINT \`FK_b39e4ed3bfa789758e476870ec2\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`);
|
"refresh",
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` DROP PRIMARY KEY`);
|
new TableColumn({
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` ADD \`id\` int NOT NULL AUTO_INCREMENT`);
|
name: "id",
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` ADD PRIMARY KEY (\`id\`)`);
|
type: "int",
|
||||||
await queryRunner.query(`ALTER TABLE \`refresh\` CHANGE \`id\` \`id\` int NOT NULL AUTO_INCREMENT`);
|
isPrimary: true,
|
||||||
|
isNullable: false,
|
||||||
|
isGenerated: true,
|
||||||
|
generationStrategy: "increment",
|
||||||
|
})
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,18 +1,73 @@
|
||||||
import { MigrationInterface, QueryRunner } from "typeorm";
|
import { MigrationInterface, QueryRunner, Table, TableColumn } from "typeorm";
|
||||||
|
|
||||||
export class Invite1724579024939 implements MigrationInterface {
|
export class Invite1724579024939 implements MigrationInterface {
|
||||||
name = 'Invite1724579024939'
|
name = "Invite1724579024939";
|
||||||
|
|
||||||
public async up(queryRunner: QueryRunner): Promise<void> {
|
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.createTable(
|
||||||
await queryRunner.query(`ALTER TABLE \`user\` ADD \`firstname\` varchar(255) NOT NULL`);
|
new Table({
|
||||||
await queryRunner.query(`ALTER TABLE \`user\` ADD \`lastname\` varchar(255) NOT NULL`);
|
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
|
||||||
|
);
|
||||||
|
|
||||||
|
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> {
|
public async down(queryRunner: QueryRunner): Promise<void> {
|
||||||
await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`lastname\``);
|
await queryRunner.dropColumns("user", ["lastname", "firstname"]);
|
||||||
await queryRunner.query(`ALTER TABLE \`user\` DROP COLUMN \`firstname\``);
|
await queryRunner.dropTable("invite");
|
||||||
await queryRunner.query(`DROP TABLE \`invite\``);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue