Skip to content

Commit

Permalink
feat: add migrations (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonbeckas authored May 17, 2023
1 parent 8e02894 commit 27d52de
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 3 deletions.
8 changes: 5 additions & 3 deletions backend/src/db/Database.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {Connection, createConnection} from "typeorm";
import * as Path from "path";
import { CONFIG } from "../config/Config";
import { InitMigration1684329519144 } from "./migrations.ts/1684329519144-InitMigration";


export class Database {
Expand All @@ -11,10 +12,11 @@ export class Database {
Path.resolve(Path.parse(__dirname).dir,"model").replace(/\\/gi,"/")+"/*.js"
],
migrations: [
Path.resolve(Path.parse(__dirname).dir,"migrations").replace(/\\/gi,"/")+"/*.js"
InitMigration1684329519144
],
synchronize: true,
logging: new Array()
migrationsTransactionMode: 'each',
synchronize: false,
logging: ["schema","query","error"]
}

constructor() {
Expand Down
26 changes: 26 additions & 0 deletions backend/src/db/migrations.ts/1684329519144-InitMigration.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class InitMigration1684329519144 implements MigrationInterface {
name?: "Init Migration";
transaction?: true;
async up(queryRunner: QueryRunner): Promise<void> {
queryRunner.query("CREATE TABLE `permission` (`id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, `groupId` varchar(255) NOT NULL, PRIMARY KEY (`id`))");
queryRunner.query("CREATE TABLE `group` (`id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, PRIMARY KEY (`id`))");
queryRunner.query("CREATE TABLE `user` (`id` varchar(36) NOT NULL, `name` varchar(255) NOT NULL, `firstname` varchar(255) NOT NULL, `password` varchar(255) NOT NULL, `username` varchar(255) NOT NULL, `lastLogin` datetime NULL, PRIMARY KEY (`id`))");
queryRunner.query("CREATE TABLE `log` (`id` int NOT NULL AUTO_INCREMENT, `timestamp` datetime NOT NULL, `data` varchar(255) NOT NULL, `type` int NOT NULL, `category` varchar(255) NOT NULL, `user` text NULL, PRIMARY KEY (`id`))");
queryRunner.query("CREATE TABLE `runner` (`id` varchar(36) NOT NULL, `state` text NULL, `lastStateChange` datetime NULL, `round` int NOT NULL, `timestamp` datetime NULL, `station` text NULL, PRIMARY KEY (`id`))");
queryRunner.query("CREATE TABLE `group_users_user` (`groupId` varchar(36) NOT NULL, `userId` varchar(36) NOT NULL, PRIMARY KEY (`groupId`, `userId`))");
queryRunner.query("ALTER TABLE `permission` ADD CONSTRAINT `FK_3ef2f3921bb8b09c16b21e197f4` FOREIGN KEY (`groupId`) REFERENCES `group`(`id`) ON DELETE NO ACTION ON UPDATE NO ACTION");
queryRunner.query("ALTER TABLE `group_users_user` ADD CONSTRAINT `FK_fe6cce7d479552c17823e267aff` FOREIGN KEY (`groupId`) REFERENCES `group`(`id`) ON DELETE CASCADE ON UPDATE CASCADE");

}
async down(queryRunner: QueryRunner): Promise<void> {
queryRunner.dropTable("permission", true, true, true);
queryRunner.dropTable("group", true, true, true);
queryRunner.dropTable("user", true, true, true);
queryRunner.dropTable("log", true, true, true);
queryRunner.dropTable("runner", true, true, true);
queryRunner.dropTable("group_users_user", true, true, true);
}

}
1 change: 1 addition & 0 deletions backend/src/model/Group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export class Group {
users!: User[]

@OneToMany(type => Permission, permission => permission.group)
@JoinTable()
permissions!: Permission[]

}

0 comments on commit 27d52de

Please sign in to comment.