diff --git a/development.Dockerfile b/development.Dockerfile index 66e7aa4..f620c4f 100644 --- a/development.Dockerfile +++ b/development.Dockerfile @@ -10,6 +10,7 @@ RUN yarn cache clean && yarn install FROM node:16-bullseye-slim as development ENV NODE_PATH=/node_modules +ENV PATH="/node_modules/.bin:${PATH}" COPY --from=dependencies /dependencies/node_modules /node_modules WORKDIR /app diff --git a/run-migrations.sh b/run-migrations.sh new file mode 100755 index 0000000..4b48d48 --- /dev/null +++ b/run-migrations.sh @@ -0,0 +1,8 @@ +#!/usr/bin/env sh +set -e + +CONTAINER_NAME="wallet-backend-server" + +echo "Running migrations inside container $CONTAINER_NAME..." +docker exec -it $CONTAINER_NAME yarn typeorm migration:run +echo "Migrations completed." diff --git a/src/AppDataSource.ts b/src/AppDataSource.ts index 029273e..20280c6 100644 --- a/src/AppDataSource.ts +++ b/src/AppDataSource.ts @@ -11,7 +11,10 @@ const AppDataSource: DataSource = new DataSource({ password: config.db.password, database: config.db.dbname, entities: [__dirname + "/entities/*.entity.{js,ts}"], - synchronize: true + migrations: [ + __dirname + "/migrations/*.{js,ts}" + ], + synchronize: false, }); (async function initDataSource() { diff --git a/src/entities/CredentialIssuer.entity.ts b/src/entities/CredentialIssuer.entity.ts index 5fc9843..0166adc 100644 --- a/src/entities/CredentialIssuer.entity.ts +++ b/src/entities/CredentialIssuer.entity.ts @@ -17,7 +17,7 @@ class CredentialIssuerEntity { // Explicit default to workaround a bug in typeorm: https://github.com/typeorm/typeorm/issues/3076#issuecomment-703128687 - @Column({ nullable: true, type: "tinyint" }) + @Column({ nullable: true, type: "tinyint", default: () => "NULL" }) visible: boolean; } diff --git a/src/migrations/1762258689037-RemoveFcmToken.ts b/src/migrations/1762258689037-RemoveFcmToken.ts new file mode 100644 index 0000000..594dffe --- /dev/null +++ b/src/migrations/1762258689037-RemoveFcmToken.ts @@ -0,0 +1,38 @@ +import { MigrationInterface, QueryRunner, TableForeignKey } from "typeorm"; + +export class RemoveFcmToken1762258689037 implements MigrationInterface { + name = 'RemoveFcmToken1762258689037'; + + public async up(queryRunner: QueryRunner): Promise { + const table = await queryRunner.getTable("fcm_token"); + // Name-agnostic drop of user entity's one-to-many relationship + if (table) { + const fk = table.foreignKeys.find(k => k.columnNames.includes("userId")); + if (fk) await queryRunner.dropForeignKey("fcm_token", fk); + await queryRunner.query("DROP TABLE `fcm_token`"); + } + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(` + CREATE TABLE \`fcm_token\` ( + \`id\` int NOT NULL AUTO_INCREMENT, + \`value\` varchar(255) NOT NULL, + \`userId\` int NULL, + PRIMARY KEY (\`id\`) + ) ENGINE=InnoDB + `); + + await queryRunner.createForeignKey( + "fcm_token", + new TableForeignKey({ + columnNames: ["userId"], + referencedTableName: "user", + referencedColumnNames: ["id"], + onDelete: "NO ACTION", + onUpdate: "NO ACTION", + }) + ); + } + +}