From 62624eb3ae06faa303631dc84be40be226a1c732 Mon Sep 17 00:00:00 2001 From: David Robert-Ansart Date: Thu, 29 Aug 2024 18:03:02 +0200 Subject: [PATCH] Reset database migrations and add initial file --- docker-compose-windows.yml | 56 ++-- server/migrations/1675763011163-AlterGame.ts | 72 ----- .../1675763051163-CreateClassroomTable.ts | 24 -- .../1675763075677-CreateStudentTable.ts | 21 -- .../1675763247781-CreateUserStudentTable.ts | 22 -- .../1675763496759-AlterActivityTable.ts | 48 ---- .../1675763659256-AlterUserTable.ts | 118 -------- server/migrations/1678113524215-alterToken.ts | 11 - .../1678353976535-AlterClassroom.ts | 30 --- .../migrations/1678809291061-AddCascades.ts | 104 -------- .../1679254082345-CreateFeatureFlagTable.ts | 48 ---- ...254082349-AddCascadesForVillageDeletion.ts | 58 ---- .../1686660843347-alterGameColumnOrigine.ts | 11 - .../1688373838635-alterGameResponseTable.ts | 21 -- .../1711637965713-PhaseStep-activity.ts | 21 -- .../1712732757287-New-Country-Table.ts | 26 -- ...874475677-CreateTablePelicoPresentation.ts | 29 -- .../1717055850587-publishedAtActivityField.ts | 20 -- .../1717055851587-new-anthem-data.ts | 251 ------------------ ...1720537502381-AlterAnalyticSessionTable.ts | 7 - .../1724946964957-initializeDatabase.ts | 198 ++++++++++++++ 21 files changed, 225 insertions(+), 971 deletions(-) delete mode 100644 server/migrations/1675763011163-AlterGame.ts delete mode 100644 server/migrations/1675763051163-CreateClassroomTable.ts delete mode 100644 server/migrations/1675763075677-CreateStudentTable.ts delete mode 100644 server/migrations/1675763247781-CreateUserStudentTable.ts delete mode 100644 server/migrations/1675763496759-AlterActivityTable.ts delete mode 100644 server/migrations/1675763659256-AlterUserTable.ts delete mode 100644 server/migrations/1678113524215-alterToken.ts delete mode 100644 server/migrations/1678353976535-AlterClassroom.ts delete mode 100644 server/migrations/1678809291061-AddCascades.ts delete mode 100644 server/migrations/1679254082345-CreateFeatureFlagTable.ts delete mode 100644 server/migrations/1679254082349-AddCascadesForVillageDeletion.ts delete mode 100644 server/migrations/1686660843347-alterGameColumnOrigine.ts delete mode 100644 server/migrations/1688373838635-alterGameResponseTable.ts delete mode 100644 server/migrations/1711637965713-PhaseStep-activity.ts delete mode 100644 server/migrations/1712732757287-New-Country-Table.ts delete mode 100644 server/migrations/1715874475677-CreateTablePelicoPresentation.ts delete mode 100644 server/migrations/1717055850587-publishedAtActivityField.ts delete mode 100644 server/migrations/1717055851587-new-anthem-data.ts delete mode 100644 server/migrations/1720537502381-AlterAnalyticSessionTable.ts create mode 100644 server/migrations/1724946964957-initializeDatabase.ts diff --git a/docker-compose-windows.yml b/docker-compose-windows.yml index 765daad4b..9369030d3 100644 --- a/docker-compose-windows.yml +++ b/docker-compose-windows.yml @@ -1,29 +1,27 @@ -# services: -# mysql: -# image: mysql:8 -# environment: -# - MYSQL_ROOT_PASSWORD=my-secret-pw -# healthcheck: -# test: [ 'CMD', 'mysqladmin', 'ping', '-h', 'localhost' ] -# timeout: 20s -# retries: 10 -# ports: -# - '3306:3306' -# - '33060:33060' -# volumes: -# - ./.mysql-data:/var/lib/mysql -# minio: -# image: minio/minio -# ports: -# - '9000:9000' -# - '9090:9090' -# environment: -# - MINIO_ROOT_USER=minioadmin -# - MINIO_ROOT_PASSWORD=minioadmin -# - MINIO_ACCESS_KEY=minio -# - MINIO_SECRET_KEY=minio123 -# volumes: -# - ./.minio-data:/data -# command: server --console-address ":9090" /data - - +services: + mysql: + image: mysql:8 + environment: + - MYSQL_ROOT_PASSWORD=my-secret-pw + healthcheck: + test: ['CMD', 'mysqladmin', 'ping', '-h', 'localhost'] + timeout: 20s + retries: 10 + ports: + - '3306:3306' + - '33060:33060' + volumes: + - ./.mysql-data:/var/lib/mysql + minio: + image: minio/minio + ports: + - '9000:9000' + - '9090:9090' + environment: + - MINIO_ROOT_USER=minioadmin + - MINIO_ROOT_PASSWORD=minioadmin + - MINIO_ACCESS_KEY=minio + - MINIO_SECRET_KEY=minio123 + volumes: + - ./.minio-data:/data + command: server --console-address ":9090" /data diff --git a/server/migrations/1675763011163-AlterGame.ts b/server/migrations/1675763011163-AlterGame.ts deleted file mode 100644 index f6e9e1bea..000000000 --- a/server/migrations/1675763011163-AlterGame.ts +++ /dev/null @@ -1,72 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class GameAddJsonColumns1675763011163 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - try { - // add 5 columns - const table = await queryRunner.getTable('game'); - if (table) { - if (!table.findColumnByName('fakeSignification1')) { - await queryRunner.query(`ALTER TABLE game ADD fakeSignification1 TEXT`); - } - if (!table.findColumnByName('fakeSignification2')) { - await queryRunner.query(`ALTER TABLE game ADD fakeSignification2 TEXT`); - } - if (!table.findColumnByName('origine')) { - await queryRunner.query(`ALTER TABLE game ADD origine TEXT`); - } - if (!table.findColumnByName('signification')) { - await queryRunner.query(`ALTER TABLE game ADD signification TEXT`); - } - if (!table.findColumnByName('video')) { - await queryRunner.query(`ALTER TABLE game ADD video TEXT`); - } - } - - // Retrieve all games with value in column "content" and update the 5 colums - const games = await queryRunner.query(`SELECT * FROM game WHERE content IS NOT NULL`); - for (const game of games) { - const { fakeSignification1, fakeSignification2, origine, signification, video } = JSON.parse(game.content); - await queryRunner.query(`UPDATE game SET fakeSignification1=?, fakeSignification2=?, origine=?, signification=?, video=? WHERE id=?`, [ - fakeSignification1, - fakeSignification2, - origine, - signification, - video, - game.id, - ]); - } - - // Delete column "content" - await queryRunner.query(`ALTER TABLE game DROP COLUMN content`); - } catch (error) { - console.error('Error during GameAddJsonColumns1675763011163 migration:', error); - } - } - - public async down(queryRunner: QueryRunner): Promise { - // Add column "content" - try { - await queryRunner.query(`ALTER TABLE game ADD content jsonb`); - - // Retrieve all games with value for 5 columns and update the "content" column - const games = await queryRunner.query( - `SELECT * FROM game WHERE fakeSignification1 IS NOT NULL OR fakeSignification2 IS NOT NULL OR origine IS NOT NULL OR signification IS NOT NULL OR video IS NOT NULL`, - ); - for (const game of games) { - const { fakeSignification1, fakeSignification2, origine, signification, video } = game; - const content = JSON.stringify({ fakeSignification1, fakeSignification2, origine, signification, video }); - await queryRunner.query(`UPDATE game SET content=$1 WHERE id=$2`, [content, game.id]); - } - - // Delete 5 columns - await queryRunner.query(`ALTER TABLE game DROP COLUMN fakeSignification1`); - await queryRunner.query(`ALTER TABLE game DROP COLUMN fakeSignification2`); - await queryRunner.query(`ALTER TABLE game DROP COLUMN origine`); - await queryRunner.query(`ALTER TABLE game DROP COLUMN signification`); - await queryRunner.query(`ALTER TABLE game DROP COLUMN video`); - } catch (error) { - console.error('Error during GameAddJsonColumns1675763011163 rollback:', error); - } - } -} diff --git a/server/migrations/1675763051163-CreateClassroomTable.ts b/server/migrations/1675763051163-CreateClassroomTable.ts deleted file mode 100644 index 549be5b80..000000000 --- a/server/migrations/1675763051163-CreateClassroomTable.ts +++ /dev/null @@ -1,24 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class CreateClassroomTable1675763051163 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE TABLE IF NOT EXISTS classroom ( - id int NOT NULL AUTO_INCREMENT, - \`name\` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, - avatar varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL, - delayedDays int DEFAULT '0', - hasVisibilitySetToClass tinyint DEFAULT '0', - userId int DEFAULT NULL, - villageId int DEFAULT NULL, - PRIMARY KEY (id), - UNIQUE KEY REL_6f2187911a8faba7c91a83194d (userId), - KEY FK_ce22c442aec315cd3aa13b69210 (villageId), - CONSTRAINT FK_6f2187911a8faba7c91a83194d9 FOREIGN KEY (userId) REFERENCES user (id), - CONSTRAINT FK_ce22c442aec315cd3aa13b69210 FOREIGN KEY (villageId) REFERENCES village (id) - ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(``); - } -} diff --git a/server/migrations/1675763075677-CreateStudentTable.ts b/server/migrations/1675763075677-CreateStudentTable.ts deleted file mode 100644 index 666cb7006..000000000 --- a/server/migrations/1675763075677-CreateStudentTable.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class CreateStudentTable1675763075677 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE TABLE IF NOT EXISTS student ( - id int NOT NULL AUTO_INCREMENT, - firstname varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, - lastname varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, - hashedCode varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, - numLinkedAccount tinyint DEFAULT '0', - classroomId int DEFAULT NULL, - PRIMARY KEY (id), - KEY FK_426224f5597213259b1d58fc0f4 (classroomId), - CONSTRAINT FK_426224f5597213259b1d58fc0f4 FOREIGN KEY (classroomId) REFERENCES classroom (id) - ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;`); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(``); - } -} diff --git a/server/migrations/1675763247781-CreateUserStudentTable.ts b/server/migrations/1675763247781-CreateUserStudentTable.ts deleted file mode 100644 index 75513a18f..000000000 --- a/server/migrations/1675763247781-CreateUserStudentTable.ts +++ /dev/null @@ -1,22 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class CreateUserStudentTable1675763247781 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(` - CREATE TABLE IF NOT EXISTS user_to_student ( - id int NOT NULL AUTO_INCREMENT, - userId int DEFAULT NULL, - studentId int DEFAULT NULL, - PRIMARY KEY (id), - KEY FK_c4a67eb239df56e50679e238eec (userId), - KEY FK_39299bddd62684b6bf55a7a8aec (studentId), - CONSTRAINT FK_39299bddd62684b6bf55a7a8aec FOREIGN KEY (studentId) REFERENCES student (id), - CONSTRAINT FK_c4a67eb239df56e50679e238eec FOREIGN KEY (userId) REFERENCES user (id) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - `); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(``); - } -} diff --git a/server/migrations/1675763496759-AlterActivityTable.ts b/server/migrations/1675763496759-AlterActivityTable.ts deleted file mode 100644 index ff9d59fe1..000000000 --- a/server/migrations/1675763496759-AlterActivityTable.ts +++ /dev/null @@ -1,48 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; -import { TableIndex, TableColumn } from 'typeorm'; - -export class AlterActivityTable1675763496759 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - const activity = 'activity'; - const isVisibleToParent = 'isVisibleToParent'; - const indexActivity = 'IDX_UserID_Activity'; - const table = await queryRunner.getTable(activity); - - if (!table || table.columns.find((column) => column.name === isVisibleToParent || table.indices.find((ind) => ind.name === indexActivity))) { - // The table doesn't exist or the column already exists, so we don't need to do anything - return; - } - - await queryRunner.addColumn( - 'activity', - new TableColumn({ - name: 'isVisibleToParent', - type: 'tinyint', - default: '1', - }), - ); - - await queryRunner.createIndex( - 'activity', - new TableIndex({ - name: 'IDX_UserID_Activity', - columnNames: ['userId'], - }), - ); - } - - public async down(queryRunner: QueryRunner): Promise { - const activity = 'activity'; - const isVisibleToParent = 'isVisibleToParent'; - const indexActivity = 'IDX_UserID_Activity'; - - const table = await queryRunner.getTable(activity); - if (!table || table.columns.find((column) => column.name === isVisibleToParent || table.indices.find((ind) => ind.name === indexActivity))) { - // The table doesn't exist or the column already exists, so we don't need to do anything - return; - } - - await queryRunner.dropColumn(activity, isVisibleToParent); - await queryRunner.dropIndex('activity', indexActivity); - } -} diff --git a/server/migrations/1675763659256-AlterUserTable.ts b/server/migrations/1675763659256-AlterUserTable.ts deleted file mode 100644 index 0787eb695..000000000 --- a/server/migrations/1675763659256-AlterUserTable.ts +++ /dev/null @@ -1,118 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; -import { TableColumn } from 'typeorm'; - -export class AlterUserTable1675763659256 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - const user = 'user'; - const firstname = 'firstname'; - const lastname = 'lastname'; - const language = 'language'; - const hasAcceptedNewsletter = 'hasAcceptedNewsletter'; - const isVerified = 'isVerified'; - const hasStudentLinked = 'hasStudentLinked'; - const table = await queryRunner.getTable(user); - - const columnsToAdd = [ - { - name: firstname, - type: 'varchar', - length: '50', - default: '""', - }, - { - name: lastname, - type: 'varchar', - length: '100', - default: '""', - }, - { - name: language, - type: 'varchar', - length: '400', - default: '""', - }, - { - name: hasAcceptedNewsletter, - type: 'tinyint', - default: '0', - }, - { - name: isVerified, - type: 'boolean', - default: false, - }, - { - name: hasStudentLinked, - type: 'boolean', - default: false, - }, - ]; - - if (table) { - for (const columnToAdd of columnsToAdd) { - const column = table.columns.find((c) => c.name === columnToAdd.name); - if (!column) { - await queryRunner.addColumn(user, new TableColumn(columnToAdd)); - } - } - } - - await queryRunner.addColumn( - 'user', - new TableColumn({ - name: 'newtype', - type: 'tinyint', - }), - ); - - // Map the old enum values to the new tinyint values - const mapping = { - '0': 3, // TEACHER = 3 - '1': 5, // OBSERVATOR = 5 - '2': 2, // MEDIATOR = 2 - '3': 1, // ADMIN = 1 - '4': 0, // SUPER_ADMIN = 0 - }; - - const queries = Object.entries(mapping).map(([enumValue, tinyintValue]) => { - return `UPDATE user SET newtype = ${tinyintValue} WHERE type = '${enumValue}'`; - }); - - for (const query of queries) { - await queryRunner.query(query); - } - - await queryRunner.dropColumn('user', 'type'); - await queryRunner.renameColumn('user', 'newtype', 'type'); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.dropColumns('user', ['firstname', 'lastname', 'language', 'hasAcceptedNewsletter', 'isVerified', 'hasStudentLinked']); - // Map the tinyint values to the old enum values - const mapping = { - 3: '0', // TEACHER = 3 - 5: '1', // OBSERVATOR = 5 - 2: '2', // MEDIATOR = 2 - 1: '3', // ADMIN = 1 - 0: '4', // SUPER_ADMIN = 0 - }; - - const queries = Object.entries(mapping).map(([tinyintValue, enumValue]) => { - return `UPDATE user SET type = '${enumValue}' WHERE newtype = ${tinyintValue}`; - }); - - for (const query of queries) { - await queryRunner.query(query); - } - - await queryRunner.dropColumn('user', 'newtype'); - await queryRunner.addColumn( - 'user', - new TableColumn({ - name: 'type', - type: 'enum', - enum: ['TEACHER', 'OBSERVATOR', 'MEDIATOR', 'ADMIN', 'SUPER_ADMIN'], - }), - ); - } -} diff --git a/server/migrations/1678113524215-alterToken.ts b/server/migrations/1678113524215-alterToken.ts deleted file mode 100644 index 0bfbc50a9..000000000 --- a/server/migrations/1678113524215-alterToken.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class alterToken1678113524215 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE token MODIFY token VARCHAR(300);`); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE token MODIFY token VARCHAR(95);`); - } -} diff --git a/server/migrations/1678353976535-AlterClassroom.ts b/server/migrations/1678353976535-AlterClassroom.ts deleted file mode 100644 index 061e275ae..000000000 --- a/server/migrations/1678353976535-AlterClassroom.ts +++ /dev/null @@ -1,30 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; -import { TableColumn } from 'typeorm'; - -export class AlterClassroom1678353976535 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - const table = await queryRunner.getTable('classroom'); - const isColumnExists = table?.findColumnByName('countryCode'); - - if (!isColumnExists) { - await queryRunner.addColumn( - 'classroom', - new TableColumn({ - name: 'countryCode', - type: 'varchar', - length: '2', - isNullable: true, - }), - ); - } - } - - public async down(queryRunner: QueryRunner): Promise { - const table = await queryRunner.getTable('classroom'); - const isColumnExists = table?.findColumnByName('countryCode'); - - if (isColumnExists) { - await queryRunner.dropColumn('classroom', 'countryCode'); - } - } -} diff --git a/server/migrations/1678809291061-AddCascades.ts b/server/migrations/1678809291061-AddCascades.ts deleted file mode 100644 index 743e10836..000000000 --- a/server/migrations/1678809291061-AddCascades.ts +++ /dev/null @@ -1,104 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; -import { TableForeignKey } from 'typeorm'; - -import { dropForeignKeyIfExists } from '../utils/dropForeignKeyIfExists'; - -export class AddCascades1678809291061 implements MigrationInterface { - async up(queryRunner: QueryRunner): Promise { - await dropForeignKeyIfExists(queryRunner, 'classroom', 'userId'); - await dropForeignKeyIfExists(queryRunner, 'user_to_student', 'userId'); - await dropForeignKeyIfExists(queryRunner, 'user_to_student', 'studentId'); - await dropForeignKeyIfExists(queryRunner, 'student', 'classroomId'); - - await queryRunner.createForeignKey( - 'classroom', - new TableForeignKey({ - columnNames: ['userId'], - referencedColumnNames: ['id'], - referencedTableName: 'user', - onDelete: 'CASCADE', - name: 'ClassroomUserFK', - }), - ); - - await queryRunner.createForeignKey( - 'user_to_student', - new TableForeignKey({ - columnNames: ['userId'], - referencedColumnNames: ['id'], - referencedTableName: 'user', - onDelete: 'CASCADE', - name: 'UserStudent_UserFK', - }), - ); - - await queryRunner.createForeignKey( - 'user_to_student', - new TableForeignKey({ - columnNames: ['studentId'], - referencedColumnNames: ['id'], - referencedTableName: 'student', - onDelete: 'CASCADE', - name: 'UserStudent_StudentFK', - }), - ); - - await queryRunner.createForeignKey( - 'student', - new TableForeignKey({ - columnNames: ['classroomId'], - referencedColumnNames: ['id'], - referencedTableName: 'classroom', - onDelete: 'CASCADE', - name: 'StudentClassroomFK', - }), - ); - } - - async down(queryRunner: QueryRunner): Promise { - await dropForeignKeyIfExists(queryRunner, 'classroom', 'userId'); - await dropForeignKeyIfExists(queryRunner, 'user_to_student', 'studentId'); - await dropForeignKeyIfExists(queryRunner, 'user_to_student', 'userId'); - await dropForeignKeyIfExists(queryRunner, 'student', 'classroomId'); - - await queryRunner.createForeignKey( - 'classroom', - new TableForeignKey({ - columnNames: ['userId'], - referencedColumnNames: ['id'], - referencedTableName: 'user', - name: 'FK_6f2187911a8faba7c91a83194d9', - }), - ); - - await queryRunner.createForeignKey( - 'user_to_student', - new TableForeignKey({ - columnNames: ['studentId'], - referencedColumnNames: ['id'], - referencedTableName: 'student', - name: 'FK_39299bddd62684b6bf55a7a8aec', - }), - ); - - await queryRunner.createForeignKey( - 'user_to_student', - new TableForeignKey({ - columnNames: ['userId'], - referencedColumnNames: ['id'], - referencedTableName: 'user', - name: 'FK_c4a67eb239df56e50679e238eec', - }), - ); - - await queryRunner.createForeignKey( - 'student', - new TableForeignKey({ - columnNames: ['classroomId'], - referencedColumnNames: ['id'], - referencedTableName: 'classroom', - name: 'FK_426224f5597213259b1d58fc0f4', - }), - ); - } -} diff --git a/server/migrations/1679254082345-CreateFeatureFlagTable.ts b/server/migrations/1679254082345-CreateFeatureFlagTable.ts deleted file mode 100644 index afbeff8c4..000000000 --- a/server/migrations/1679254082345-CreateFeatureFlagTable.ts +++ /dev/null @@ -1,48 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class CreateFeatureFlagTable1679254082345 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(` - CREATE TABLE IF NOT EXISTS feature_flag ( - id int NOT NULL AUTO_INCREMENT, - name varchar(255) NOT NULL, - isEnabled boolean NOT NULL DEFAULT false, - PRIMARY KEY (id), - UNIQUE KEY unique_name (name) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - `); - - await queryRunner.query(` - CREATE TABLE IF NOT EXISTS user_feature_flags_feature_flag ( - userId int NOT NULL, - featureFlagId int NOT NULL, - PRIMARY KEY (userId, featureFlagId) - ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - `); - - await queryRunner.query( - ` - ALTER TABLE user_feature_flags_feature_flag - ADD CONSTRAINT FK_user - FOREIGN KEY (userId) REFERENCES user (id) ON DELETE CASCADE; - `, - undefined, - true, - ); - - await queryRunner.query( - ` - ALTER TABLE user_feature_flags_feature_flag - ADD CONSTRAINT FK_featureFlag - FOREIGN KEY (featureFlagId) REFERENCES feature_flag (id) ON DELETE CASCADE; - `, - undefined, - true, - ); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`DROP TABLE IF EXISTS user_feature_flags_feature_flag;`); - await queryRunner.query(`DROP TABLE IF EXISTS feature_flag;`); - } -} diff --git a/server/migrations/1679254082349-AddCascadesForVillageDeletion.ts b/server/migrations/1679254082349-AddCascadesForVillageDeletion.ts deleted file mode 100644 index 67df55dca..000000000 --- a/server/migrations/1679254082349-AddCascadesForVillageDeletion.ts +++ /dev/null @@ -1,58 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; -import { TableForeignKey } from 'typeorm'; - -import { dropForeignKeyIfExists } from '../utils/dropForeignKeyIfExists'; - -export class AddCascadesForVillageAndStudentDeletion1679254082349 implements MigrationInterface { - async up(queryRunner: QueryRunner): Promise { - await dropForeignKeyIfExists(queryRunner, 'classroom', 'villageId'); - await dropForeignKeyIfExists(queryRunner, 'student', 'classroomId'); - - await queryRunner.createForeignKey( - 'classroom', - new TableForeignKey({ - columnNames: ['villageId'], - referencedColumnNames: ['id'], - referencedTableName: 'village', - onDelete: 'CASCADE', - name: 'ClassroomVillageFK', - }), - ); - - await queryRunner.createForeignKey( - 'student', - new TableForeignKey({ - columnNames: ['classroomId'], - referencedColumnNames: ['id'], - referencedTableName: 'classroom', - onDelete: 'CASCADE', - name: 'StudentClassroomFK', - }), - ); - } - - async down(queryRunner: QueryRunner): Promise { - await dropForeignKeyIfExists(queryRunner, 'classroom', 'villageId'); - await dropForeignKeyIfExists(queryRunner, 'student', 'classroomId'); - - await queryRunner.createForeignKey( - 'classroom', - new TableForeignKey({ - columnNames: ['villageId'], - referencedColumnNames: ['id'], - referencedTableName: 'village', - name: 'FK_ce22c442aec315cd3aa13b69210', - }), - ); - - await queryRunner.createForeignKey( - 'student', - new TableForeignKey({ - columnNames: ['classroomId'], - referencedColumnNames: ['id'], - referencedTableName: 'classroom', - name: 'FK_ab334f6a4e3ed5adda6e2172334', - }), - ); - } -} diff --git a/server/migrations/1686660843347-alterGameColumnOrigine.ts b/server/migrations/1686660843347-alterGameColumnOrigine.ts deleted file mode 100644 index fcfbac436..000000000 --- a/server/migrations/1686660843347-alterGameColumnOrigine.ts +++ /dev/null @@ -1,11 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class alterGameColumnOrigine1686660843347 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE game MODIFY origine text NULL`); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE game MODIFY origine text NOT NULL`); - } -} diff --git a/server/migrations/1688373838635-alterGameResponseTable.ts b/server/migrations/1688373838635-alterGameResponseTable.ts deleted file mode 100644 index e152efb57..000000000 --- a/server/migrations/1688373838635-alterGameResponseTable.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class alterGameResponseTable1688373838635 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - try { - const table = await queryRunner.getTable('game_response'); - if (table) { - if (!table.findColumnByName('isOldResponse')) { - await queryRunner.query(`ALTER TABLE game_response ADD isOldResponse BOOLEAN DEFAULT false`); - } - } - } catch (error) { - console.error('Error during alterGameResponseTable1688373838635 migration:', error); - throw error; - } - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`ALTER TABLE game_response DROP COLUMN isOldResponse`); - } -} diff --git a/server/migrations/1711637965713-PhaseStep-activity.ts b/server/migrations/1711637965713-PhaseStep-activity.ts deleted file mode 100644 index 8fa28bde6..000000000 --- a/server/migrations/1711637965713-PhaseStep-activity.ts +++ /dev/null @@ -1,21 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class PhaseStepActivity1711637965713 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - const activityTable = await queryRunner.getTable('activity'); - if (activityTable) { - if (!activityTable.findColumnByName('phaseStep')) { - await queryRunner.query(`ALTER TABLE ${activityTable.name} ADD phaseStep TEXT`); - } - } - } - - public async down(queryRunner: QueryRunner): Promise { - const activityTable = await queryRunner.getTable('activity'); - if (activityTable) { - if (activityTable.findColumnByName('phaseStep')) { - await queryRunner.query(`ALTER TABLE ${activityTable.name} DROP COLUMN phaseStep`); - } - } - } -} diff --git a/server/migrations/1712732757287-New-Country-Table.ts b/server/migrations/1712732757287-New-Country-Table.ts deleted file mode 100644 index 267ae846e..000000000 --- a/server/migrations/1712732757287-New-Country-Table.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -import { countries } from '../utils/iso-3166-countries-french'; - -export class NewCountryTable1712732757287 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.query(`CREATE TABLE IF NOT EXISTS country ( - id int PRIMARY KEY AUTO_INCREMENT, - isoCode varchar(3) UNIQUE NOT NULL, - name varchar(255) UNIQUE NOT NULL - );`); - // seed country in db - for (const country of countries) { - // try catch here to prevent if country is already set in db for some reason - try { - await queryRunner.query(`INSERT INTO country (isoCode, name) VALUES ("${country.isoCode}", "${country.name}");`); - } catch (error) { - console.error(error); - } - } - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.query(`DROP TABLE IF EXISTS country;`); - } -} diff --git a/server/migrations/1715874475677-CreateTablePelicoPresentation.ts b/server/migrations/1715874475677-CreateTablePelicoPresentation.ts deleted file mode 100644 index e4cee0324..000000000 --- a/server/migrations/1715874475677-CreateTablePelicoPresentation.ts +++ /dev/null @@ -1,29 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; -import { Table } from 'typeorm'; - -export class CreateTablePelicoPresentation1715874475677 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - await queryRunner.createTable( - new Table({ - name: 'pelico_presentation', - columns: [ - { - name: 'id', - type: 'int', - isPrimary: true, - isGenerated: true, - generationStrategy: 'increment', - }, - { - name: 'content', - type: 'json', - }, - ], - }), - ); - } - - public async down(queryRunner: QueryRunner): Promise { - await queryRunner.dropTable('pelico_presentation'); - } -} diff --git a/server/migrations/1717055850587-publishedAtActivityField.ts b/server/migrations/1717055850587-publishedAtActivityField.ts deleted file mode 100644 index a827254c8..000000000 --- a/server/migrations/1717055850587-publishedAtActivityField.ts +++ /dev/null @@ -1,20 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -export class PublishedAtActivityField1717055850587 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - const activityTable = await queryRunner.getTable('activity'); - if (!activityTable?.columns.find((col) => col.name === 'publishDate')) { - await queryRunner.query(`ALTER TABLE activity - ADD COLUMN publishDate TIMESTAMP`); - await queryRunner.query(`UPDATE activity SET publishDate = activity.createDate WHERE status = 0;`); - } - } - - public async down(queryRunner: QueryRunner): Promise { - const activityTable = await queryRunner.getTable('activity'); - if (activityTable?.columns.find((col) => col.name === 'publishDate')) { - await queryRunner.query(`ALTER TABLE activity - DROP COLUMN publishDate`); - } - } -} diff --git a/server/migrations/1717055851587-new-anthem-data.ts b/server/migrations/1717055851587-new-anthem-data.ts deleted file mode 100644 index 6e31621d2..000000000 --- a/server/migrations/1717055851587-new-anthem-data.ts +++ /dev/null @@ -1,251 +0,0 @@ -import type { MigrationInterface, QueryRunner } from 'typeorm'; - -import type { AnthemData } from '../../types/anthem.type'; -import type { ClassAnthemData } from '../../types/classAnthem.types'; - -enum SampleType { - VOCALS = 0, - HARMONIC1 = 1, - HARMONIC2 = 2, - MELODIC1 = 3, - MELODIC2 = 4, - RYTHMIC1 = 5, - RYTHMIC2 = 6, - INTRO_CHORUS = 7, - OUTRO = 8, -} -type Syllable = { - value: string; - back: boolean; -}; -type Sample = { - value: string; - display: boolean; - label: string; - type: SampleType; - time: number; -}; -type OldAnthemData = { - verseAudios: Sample[]; - introOutro: Sample[]; - verseLyrics: Syllable[]; - chorus: Syllable[]; - finalVerse: string; - finalMix: string; - verseTime: number; -}; -type OldVerseData = { - verseAudios: Sample[]; - introOutro: Sample[]; - verseLyrics: Syllable[]; - chorus: Syllable[]; - verse: string; - verseStart: number; - customizedMix: string; - customizedMixBlob?: Blob; - verseTime: number; - mixWithoutLyrics: string; - classRecord: string; - slicedRecord: string; - customizedMixWithVocals: string; -}; - -const sampleTypeToTrackType = { - [SampleType.VOCALS]: 1, - [SampleType.HARMONIC1]: 2, - [SampleType.HARMONIC2]: 3, - [SampleType.MELODIC1]: 4, - [SampleType.MELODIC2]: 5, - [SampleType.RYTHMIC1]: 6, - [SampleType.RYTHMIC2]: 7, - [SampleType.INTRO_CHORUS]: 0, - [SampleType.OUTRO]: 8, -}; -const sampleTypeToInstrument = { - [SampleType.VOCALS]: 'accordion', - [SampleType.HARMONIC1]: 'slide_guitar', - [SampleType.HARMONIC2]: 'electric_guitar', - [SampleType.MELODIC1]: 'piano', - [SampleType.MELODIC2]: 'electric_piano', - [SampleType.RYTHMIC1]: 'maraca', - [SampleType.RYTHMIC2]: 'jazz_set', - [SampleType.INTRO_CHORUS]: 'accordion', - [SampleType.OUTRO]: 'accordion', -}; -const trackTypeToSampleType = { - 1: SampleType.VOCALS, - 2: SampleType.HARMONIC1, - 3: SampleType.HARMONIC2, - 4: SampleType.MELODIC1, - 5: SampleType.MELODIC2, - 6: SampleType.RYTHMIC1, - 7: SampleType.RYTHMIC2, - 0: SampleType.INTRO_CHORUS, - 8: SampleType.OUTRO, - 9: SampleType.VOCALS, -}; - -type NewAnthemData = AnthemData; -type NewVerseData = ClassAnthemData; - -const isOldAnthemData = (data: unknown): data is OldAnthemData => typeof data === 'object' && data !== null && 'introOutro' in data; -const isOldVerseData = (data: unknown): data is OldVerseData => typeof data === 'object' && data !== null && 'introOutro' in data; - -const isNewAnthemData = (data: unknown): data is NewAnthemData => typeof data === 'object' && data !== null && 'tracks' in data; -const isNewVerseData = (data: unknown): data is NewVerseData => typeof data === 'object' && data !== null && 'tracks' in data; - -export class NewAnthemData1714929595777 implements MigrationInterface { - public async up(queryRunner: QueryRunner): Promise { - // Update current anthems and verses to use new data structure - - const anthems: { id: number; data: unknown }[] = await queryRunner.query('SELECT `id`, `data` FROM `activity` WHERE `type` = 11'); - for (const { id, data } of anthems) { - if (isOldAnthemData(data)) { - const newData: NewAnthemData = { - tracks: [ - ...data.verseAudios.map((sample) => ({ - type: sampleTypeToTrackType[sample.type], - label: sample.label, - sampleUrl: sample.value, - sampleDuration: data.verseTime, - sampleStartTime: 0, - sampleVolume: 1, - iconUrl: sampleTypeToInstrument[sample.type] || 'accordion', - })), - ...data.introOutro.map((sample) => ({ - type: sampleTypeToTrackType[sample.type], - label: sample.label, - sampleUrl: sample.value, - sampleDuration: sample.time, - sampleStartTime: 0, - sampleVolume: 1, - iconUrl: sampleTypeToInstrument[sample.type] || 'accordion', - })), - ], - verseLyrics: data.verseLyrics, - chorusLyrics: data.chorus, - mixUrl: data.finalVerse, - fullMixUrl: data.finalMix, - }; - await queryRunner.query('UPDATE `activity` SET `data` = ? WHERE `id` = ?', [JSON.stringify(newData), id]); - } - } - - const verses: { id: number; data: unknown }[] = await queryRunner.query('SELECT `id`, `data` FROM `activity` WHERE `type` = 12'); - for (const { id, data } of verses) { - if (isOldVerseData(data)) { - const newData: NewVerseData = { - anthemTracks: [ - ...data.verseAudios.map((sample) => ({ - type: sampleTypeToTrackType[sample.type], - label: sample.label, - sampleUrl: sample.value, - sampleDuration: data.verseTime, - sampleStartTime: 0, - sampleVolume: 1, - iconUrl: sampleTypeToInstrument[sample.type] || 'accordion', - })), - ...data.introOutro.map((sample) => ({ - type: sampleTypeToTrackType[sample.type], - label: sample.label, - sampleUrl: sample.value, - sampleDuration: sample.time, - sampleStartTime: 0, - sampleVolume: 1, - iconUrl: sampleTypeToInstrument[sample.type] || 'accordion', - })), - ], - classRecordTrack: { - type: 9, - label: '', - sampleUrl: data.classRecord, - sampleDuration: data.verseTime, - sampleStartTime: data.verseStart, - sampleVolume: 1, - iconUrl: 'accordion', - }, - verseLyrics: data.verseLyrics, - chorusLyrics: data.chorus, - verseMixUrl: data.customizedMix, - verseMixWithIntroUrl: '', - verseMixWithVocalsUrl: data.customizedMixWithVocals, - verseFinalMixUrl: data.verse, - }; - await queryRunner.query('UPDATE `activity` SET `data` = ? WHERE `id` = ?', [JSON.stringify(newData), id]); - } - } - } - - public async down(queryRunner: QueryRunner): Promise { - // Revert current anthems and verses to use old data structure - - const anthems: { id: number; data: unknown }[] = await queryRunner.query('SELECT `id`, `data` FROM `activity` WHERE `type` = 11'); - for (const { id, data } of anthems) { - if (isNewAnthemData(data)) { - const newData: OldAnthemData = { - verseAudios: data.tracks - .filter((track) => track.type !== 0 && track.type !== 8) - .map((track) => ({ - value: track.sampleUrl, - display: true, - label: track.label, - type: trackTypeToSampleType[track.type], - time: track.sampleDuration, - })), - introOutro: data.tracks - .filter((track) => track.type === 0 || track.type === 8) - .map((track) => ({ - value: track.sampleUrl, - display: true, - label: track.label, - type: trackTypeToSampleType[track.type], - time: track.sampleDuration, - })), - verseLyrics: data.verseLyrics, - chorus: data.chorusLyrics, - finalVerse: data.mixUrl ?? '', - finalMix: data.fullMixUrl ?? '', - verseTime: data.tracks.find((track) => track.type === 1)?.sampleDuration ?? 0, - }; - await queryRunner.query('UPDATE `activity` SET `data` = ? WHERE `id` = ?', [JSON.stringify(newData), id]); - } - } - - const verses: { id: number; data: unknown }[] = await queryRunner.query('SELECT `id`, `data` FROM `activity` WHERE `type` = 12'); - for (const { id, data } of verses) { - if (isNewVerseData(data)) { - const newData: OldVerseData = { - verseAudios: data.anthemTracks - .filter((track) => track.type !== 0 && track.type !== 8) - .map((track) => ({ - value: track.sampleUrl, - display: true, - label: track.label, - type: trackTypeToSampleType[track.type], - time: track.sampleDuration, - })), - introOutro: data.anthemTracks - .filter((track) => track.type === 0 || track.type === 8) - .map((track) => ({ - value: track.sampleUrl, - display: true, - label: track.label, - type: trackTypeToSampleType[track.type], - time: track.sampleDuration, - })), - verseLyrics: data.verseLyrics, - chorus: data.chorusLyrics, - verse: data.verseFinalMixUrl, - verseStart: data.classRecordTrack.sampleStartTime, - customizedMix: data.verseMixUrl, - verseTime: data.classRecordTrack.sampleDuration, - mixWithoutLyrics: data.verseMixUrl, - classRecord: data.classRecordTrack.sampleUrl, - slicedRecord: data.classRecordTrack.sampleUrl, - customizedMixWithVocals: data.verseMixWithVocalsUrl, - }; - await queryRunner.query('UPDATE `activity` SET `data` = ? WHERE `id` = ?', [JSON.stringify(newData), id]); - } - } - } -} diff --git a/server/migrations/1720537502381-AlterAnalyticSessionTable.ts b/server/migrations/1720537502381-AlterAnalyticSessionTable.ts deleted file mode 100644 index 243d953bc..000000000 --- a/server/migrations/1720537502381-AlterAnalyticSessionTable.ts +++ /dev/null @@ -1,7 +0,0 @@ -// import type { MigrationInterface, QueryRunner } from 'typeorm'; - -// export class AlterAnalyticSessionTable1720537502381 implements MigrationInterface { -// public async up(queryRunner: QueryRunner): Promise { } - -// public async down(queryRunner: QueryRunner): Promise { } -// } diff --git a/server/migrations/1724946964957-initializeDatabase.ts b/server/migrations/1724946964957-initializeDatabase.ts new file mode 100644 index 000000000..c04554262 --- /dev/null +++ b/server/migrations/1724946964957-initializeDatabase.ts @@ -0,0 +1,198 @@ +import type { MigrationInterface, QueryRunner } from 'typeorm'; + +export class InitializeDatabase1724946964957 implements MigrationInterface { + name = 'InitializeDatabase1724946964957'; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query( + `CREATE TABLE \`feature_flag\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(255) NOT NULL, \`isEnabled\` tinyint NOT NULL DEFAULT 0, UNIQUE INDEX \`IDX_0cb1810eca363db1e0bf13c3cf\` (\`name\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`image\` (\`id\` int NOT NULL AUTO_INCREMENT, \`createDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updateDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`deleteDate\` datetime(6) NULL, \`userId\` int NOT NULL, \`villageId\` int NOT NULL, \`activityId\` int NOT NULL, \`imageType\` tinyint NOT NULL, \`imageUrl\` text NOT NULL, \`inspiredStoryId\` int NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`classroom\` (\`id\` int NOT NULL AUTO_INCREMENT, \`name\` varchar(255) NULL, \`avatar\` varchar(255) NULL, \`delayedDays\` int NULL DEFAULT '0', \`countryCode\` varchar(2) NULL, \`hasVisibilitySetToClass\` tinyint NULL DEFAULT 0, \`villageId\` int NOT NULL, \`userId\` int NULL, UNIQUE INDEX \`REL_6f2187911a8faba7c91a83194d\` (\`userId\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`student\` (\`id\` int NOT NULL AUTO_INCREMENT, \`firstname\` varchar(100) NULL, \`lastname\` varchar(255) NOT NULL, \`hashedCode\` varchar(255) NOT NULL, \`numLinkedAccount\` tinyint NULL DEFAULT '0', \`classroomId\` int NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`user_to_student\` (\`id\` int NOT NULL AUTO_INCREMENT, \`userId\` int NOT NULL, \`studentId\` int NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`user\` (\`id\` int NOT NULL AUTO_INCREMENT, \`email\` varchar(255) NOT NULL, \`pseudo\` varchar(50) NOT NULL DEFAULT '', \`firstname\` varchar(50) NOT NULL DEFAULT '', \`lastname\` varchar(100) NOT NULL DEFAULT '', \`level\` varchar(50) NOT NULL DEFAULT '', \`school\` varchar(255) NOT NULL DEFAULT '', \`city\` varchar(128) NOT NULL DEFAULT '', \`postalCode\` varchar(20) NOT NULL DEFAULT '', \`address\` varchar(255) NOT NULL DEFAULT '', \`avatar\` text NULL, \`displayName\` varchar(400) NULL, \`hasAcceptedNewsletter\` tinyint NOT NULL DEFAULT 0, \`language\` varchar(400) NOT NULL DEFAULT 'français', \`accountRegistration\` int NOT NULL DEFAULT '0', \`passwordHash\` varchar(300) NOT NULL, \`verificationHash\` varchar(300) NOT NULL DEFAULT '', \`isVerified\` tinyint NOT NULL DEFAULT 0, \`firstLogin\` tinyint NOT NULL DEFAULT '0', \`type\` tinyint NOT NULL DEFAULT '3', \`villageId\` int NULL, \`countryCode\` varchar(2) NULL, \`positionLat\` decimal(11,8) NOT NULL DEFAULT '0.00000000', \`positionLon\` decimal(11,8) NOT NULL DEFAULT '0.00000000', \`hasStudentLinked\` tinyint NOT NULL DEFAULT 0, UNIQUE INDEX \`IDX_e12875dfb3b1d92d7d7c5377e2\` (\`email\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`game_response\` (\`id\` int NOT NULL AUTO_INCREMENT, \`createDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updateDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`deleteDate\` datetime(6) NULL, \`userId\` int NOT NULL, \`villageId\` int NOT NULL, \`gameId\` int NOT NULL, \`value\` text NOT NULL, \`isOldResponse\` tinyint NOT NULL DEFAULT 0, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`game\` (\`id\` int NOT NULL AUTO_INCREMENT, \`createDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updateDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`deleteDate\` datetime(6) NULL, \`userId\` int NOT NULL, \`villageId\` int NOT NULL, \`activityId\` int NOT NULL, \`type\` tinyint NULL, \`fakeSignification1\` text NOT NULL, \`fakeSignification2\` text NOT NULL, \`origine\` text NOT NULL, \`signification\` text NOT NULL, \`video\` text NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`activity\` (\`id\` int NOT NULL AUTO_INCREMENT, \`type\` tinyint NOT NULL DEFAULT '0', \`subType\` tinyint NULL, \`phase\` tinyint NOT NULL DEFAULT '1', \`phaseStep\` text NULL, \`status\` tinyint NOT NULL DEFAULT '0', \`createDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`publishDate\` datetime NOT NULL, \`updateDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`deleteDate\` datetime(6) NULL, \`data\` json NOT NULL, \`content\` json NOT NULL, \`userId\` int NOT NULL, \`villageId\` int NOT NULL, \`responseActivityId\` int NULL, \`responseType\` tinyint NULL, \`isPinned\` tinyint NOT NULL DEFAULT 0, \`displayAsUser\` tinyint NOT NULL DEFAULT 0, \`isVisibleToParent\` tinyint NOT NULL DEFAULT 1, INDEX \`IDX_3571467bcbe021f66e2bdce96e\` (\`userId\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`village\` (\`id\` int NOT NULL AUTO_INCREMENT, \`plmId\` int NULL, \`name\` varchar(80) NOT NULL, \`countryCodes\` text NOT NULL, \`activePhase\` tinyint NOT NULL DEFAULT '1', \`anthemId\` int NULL, UNIQUE INDEX \`REL_bb7b5bf4148f0848111511f52d\` (\`anthemId\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`video\` (\`id\` int NOT NULL, \`name\` varchar(64) NOT NULL, \`userId\` int NOT NULL, \`createDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`pelico_presentation\` (\`id\` int NOT NULL AUTO_INCREMENT, \`content\` json NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`country\` (\`id\` int NOT NULL AUTO_INCREMENT, \`isoCode\` text NOT NULL, \`name\` text NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`token\` (\`id\` int NOT NULL AUTO_INCREMENT, \`token\` varchar(300) NOT NULL, \`userId\` int NOT NULL, \`date\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`analytic_session\` (\`id\` varchar(36) NOT NULL, \`uniqueId\` varchar(20) NOT NULL, \`date\` datetime NOT NULL, \`type\` varchar(8) NOT NULL, \`os\` varchar(20) NOT NULL, \`browserName\` varchar(20) NOT NULL, \`browserVersion\` varchar(20) NOT NULL, \`duration\` smallint UNSIGNED NULL, \`width\` smallint UNSIGNED NOT NULL, \`initialPage\` varchar(255) NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`analytic_page_view\` (\`id\` int NOT NULL AUTO_INCREMENT, \`sessionId\` varchar(36) NOT NULL, \`date\` datetime NOT NULL, \`page\` varchar(255) NOT NULL, \`referrer\` varchar(255) NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`analytic_performance\` (\`id\` int NOT NULL AUTO_INCREMENT, \`sessionId\` varchar(36) NOT NULL, \`date\` datetime NOT NULL, \`data\` text NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`comment\` (\`id\` int NOT NULL AUTO_INCREMENT, \`userId\` int NOT NULL, \`activityId\` int NOT NULL, \`createDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updateDate\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), \`text\` text NOT NULL, PRIMARY KEY (\`id\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `CREATE TABLE \`user_feature_flags_feature_flag\` (\`userId\` int NOT NULL, \`featureFlagId\` int NOT NULL, INDEX \`IDX_c2f1d484510174d826ecc48c07\` (\`userId\`), INDEX \`IDX_daba3b0a0fca8e9f9b30541dd2\` (\`featureFlagId\`), PRIMARY KEY (\`userId\`, \`featureFlagId\`)) ENGINE=InnoDB`, + ); + await queryRunner.query( + `ALTER TABLE \`image\` ADD CONSTRAINT \`FK_dc40417dfa0c7fbd70b8eb880cc\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`image\` ADD CONSTRAINT \`FK_113ef388a86b315ee4f69795802\` FOREIGN KEY (\`villageId\`) REFERENCES \`village\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`image\` ADD CONSTRAINT \`FK_21b660ac3034e8cd7e2f343a9c4\` FOREIGN KEY (\`activityId\`) REFERENCES \`activity\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`classroom\` ADD CONSTRAINT \`FK_6f2187911a8faba7c91a83194d9\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`classroom\` ADD CONSTRAINT \`FK_ce22c442aec315cd3aa13b69210\` FOREIGN KEY (\`villageId\`) REFERENCES \`village\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`student\` ADD CONSTRAINT \`FK_426224f5597213259b1d58fc0f4\` FOREIGN KEY (\`classroomId\`) REFERENCES \`classroom\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`user_to_student\` ADD CONSTRAINT \`FK_c4a67eb239df56e50679e238eec\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`user_to_student\` ADD CONSTRAINT \`FK_39299bddd62684b6bf55a7a8aec\` FOREIGN KEY (\`studentId\`) REFERENCES \`student\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`user\` ADD CONSTRAINT \`FK_2583b4710ea9710eedddb3e7e9f\` FOREIGN KEY (\`villageId\`) REFERENCES \`village\`(\`id\`) ON DELETE SET NULL ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`game_response\` ADD CONSTRAINT \`FK_472d93272998bbe1f47ababe76b\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`game_response\` ADD CONSTRAINT \`FK_c2ac9c3907457e9ca456ec6ef98\` FOREIGN KEY (\`villageId\`) REFERENCES \`village\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`game_response\` ADD CONSTRAINT \`FK_3a0c737a217bbd6bbd268203fb8\` FOREIGN KEY (\`gameId\`) REFERENCES \`game\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`game\` ADD CONSTRAINT \`FK_a8106c0a84d70ecfc3358301c54\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`game\` ADD CONSTRAINT \`FK_da0ecad97069e6dd256857358a7\` FOREIGN KEY (\`villageId\`) REFERENCES \`village\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`game\` ADD CONSTRAINT \`FK_eb424044e76957a3b9f814067f3\` FOREIGN KEY (\`activityId\`) REFERENCES \`activity\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`activity\` ADD CONSTRAINT \`FK_3571467bcbe021f66e2bdce96ea\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`activity\` ADD CONSTRAINT \`FK_cc60a5bb15f6d08a21e5b7ef27b\` FOREIGN KEY (\`villageId\`) REFERENCES \`village\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`activity\` ADD CONSTRAINT \`FK_4913eed2cc74630efeba1114358\` FOREIGN KEY (\`responseActivityId\`) REFERENCES \`activity\`(\`id\`) ON DELETE SET NULL ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`village\` ADD CONSTRAINT \`FK_bb7b5bf4148f0848111511f52df\` FOREIGN KEY (\`anthemId\`) REFERENCES \`activity\`(\`id\`) ON DELETE SET NULL ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`video\` ADD CONSTRAINT \`FK_74e27b13f8ac66f999400df12f6\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`analytic_page_view\` ADD CONSTRAINT \`FK_f60b860e3691b49e597854bc284\` FOREIGN KEY (\`sessionId\`) REFERENCES \`analytic_session\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`analytic_performance\` ADD CONSTRAINT \`FK_b4ae11c38897a5fbe83c6359ef7\` FOREIGN KEY (\`sessionId\`) REFERENCES \`analytic_session\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`comment\` ADD CONSTRAINT \`FK_43edbd83eb91401b157f9895ea5\` FOREIGN KEY (\`activityId\`) REFERENCES \`activity\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`comment\` ADD CONSTRAINT \`FK_c0354a9a009d3bb45a08655ce3b\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE NO ACTION`, + ); + await queryRunner.query( + `ALTER TABLE \`user_feature_flags_feature_flag\` ADD CONSTRAINT \`FK_c2f1d484510174d826ecc48c071\` FOREIGN KEY (\`userId\`) REFERENCES \`user\`(\`id\`) ON DELETE CASCADE ON UPDATE CASCADE`, + ); + await queryRunner.query( + `ALTER TABLE \`user_feature_flags_feature_flag\` ADD CONSTRAINT \`FK_daba3b0a0fca8e9f9b30541dd26\` FOREIGN KEY (\`featureFlagId\`) REFERENCES \`feature_flag\`(\`id\`) ON DELETE NO ACTION ON UPDATE NO ACTION`, + ); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE \`user_feature_flags_feature_flag\` DROP FOREIGN KEY \`FK_daba3b0a0fca8e9f9b30541dd26\``); + await queryRunner.query(`ALTER TABLE \`user_feature_flags_feature_flag\` DROP FOREIGN KEY \`FK_c2f1d484510174d826ecc48c071\``); + await queryRunner.query(`ALTER TABLE \`comment\` DROP FOREIGN KEY \`FK_c0354a9a009d3bb45a08655ce3b\``); + await queryRunner.query(`ALTER TABLE \`comment\` DROP FOREIGN KEY \`FK_43edbd83eb91401b157f9895ea5\``); + await queryRunner.query(`ALTER TABLE \`analytic_performance\` DROP FOREIGN KEY \`FK_b4ae11c38897a5fbe83c6359ef7\``); + await queryRunner.query(`ALTER TABLE \`analytic_page_view\` DROP FOREIGN KEY \`FK_f60b860e3691b49e597854bc284\``); + await queryRunner.query(`ALTER TABLE \`video\` DROP FOREIGN KEY \`FK_74e27b13f8ac66f999400df12f6\``); + await queryRunner.query(`ALTER TABLE \`village\` DROP FOREIGN KEY \`FK_bb7b5bf4148f0848111511f52df\``); + await queryRunner.query(`ALTER TABLE \`activity\` DROP FOREIGN KEY \`FK_4913eed2cc74630efeba1114358\``); + await queryRunner.query(`ALTER TABLE \`activity\` DROP FOREIGN KEY \`FK_cc60a5bb15f6d08a21e5b7ef27b\``); + await queryRunner.query(`ALTER TABLE \`activity\` DROP FOREIGN KEY \`FK_3571467bcbe021f66e2bdce96ea\``); + await queryRunner.query(`ALTER TABLE \`game\` DROP FOREIGN KEY \`FK_eb424044e76957a3b9f814067f3\``); + await queryRunner.query(`ALTER TABLE \`game\` DROP FOREIGN KEY \`FK_da0ecad97069e6dd256857358a7\``); + await queryRunner.query(`ALTER TABLE \`game\` DROP FOREIGN KEY \`FK_a8106c0a84d70ecfc3358301c54\``); + await queryRunner.query(`ALTER TABLE \`game_response\` DROP FOREIGN KEY \`FK_3a0c737a217bbd6bbd268203fb8\``); + await queryRunner.query(`ALTER TABLE \`game_response\` DROP FOREIGN KEY \`FK_c2ac9c3907457e9ca456ec6ef98\``); + await queryRunner.query(`ALTER TABLE \`game_response\` DROP FOREIGN KEY \`FK_472d93272998bbe1f47ababe76b\``); + await queryRunner.query(`ALTER TABLE \`user\` DROP FOREIGN KEY \`FK_2583b4710ea9710eedddb3e7e9f\``); + await queryRunner.query(`ALTER TABLE \`user_to_student\` DROP FOREIGN KEY \`FK_39299bddd62684b6bf55a7a8aec\``); + await queryRunner.query(`ALTER TABLE \`user_to_student\` DROP FOREIGN KEY \`FK_c4a67eb239df56e50679e238eec\``); + await queryRunner.query(`ALTER TABLE \`student\` DROP FOREIGN KEY \`FK_426224f5597213259b1d58fc0f4\``); + await queryRunner.query(`ALTER TABLE \`classroom\` DROP FOREIGN KEY \`FK_ce22c442aec315cd3aa13b69210\``); + await queryRunner.query(`ALTER TABLE \`classroom\` DROP FOREIGN KEY \`FK_6f2187911a8faba7c91a83194d9\``); + await queryRunner.query(`ALTER TABLE \`image\` DROP FOREIGN KEY \`FK_21b660ac3034e8cd7e2f343a9c4\``); + await queryRunner.query(`ALTER TABLE \`image\` DROP FOREIGN KEY \`FK_113ef388a86b315ee4f69795802\``); + await queryRunner.query(`ALTER TABLE \`image\` DROP FOREIGN KEY \`FK_dc40417dfa0c7fbd70b8eb880cc\``); + await queryRunner.query(`DROP INDEX \`IDX_daba3b0a0fca8e9f9b30541dd2\` ON \`user_feature_flags_feature_flag\``); + await queryRunner.query(`DROP INDEX \`IDX_c2f1d484510174d826ecc48c07\` ON \`user_feature_flags_feature_flag\``); + await queryRunner.query(`DROP TABLE \`user_feature_flags_feature_flag\``); + await queryRunner.query(`DROP TABLE \`comment\``); + await queryRunner.query(`DROP TABLE \`analytic_performance\``); + await queryRunner.query(`DROP TABLE \`analytic_page_view\``); + await queryRunner.query(`DROP TABLE \`analytic_session\``); + await queryRunner.query(`DROP TABLE \`token\``); + await queryRunner.query(`DROP TABLE \`country\``); + await queryRunner.query(`DROP TABLE \`pelico_presentation\``); + await queryRunner.query(`DROP TABLE \`video\``); + await queryRunner.query(`DROP INDEX \`REL_bb7b5bf4148f0848111511f52d\` ON \`village\``); + await queryRunner.query(`DROP TABLE \`village\``); + await queryRunner.query(`DROP INDEX \`IDX_3571467bcbe021f66e2bdce96e\` ON \`activity\``); + await queryRunner.query(`DROP TABLE \`activity\``); + await queryRunner.query(`DROP TABLE \`game\``); + await queryRunner.query(`DROP TABLE \`game_response\``); + await queryRunner.query(`DROP INDEX \`IDX_e12875dfb3b1d92d7d7c5377e2\` ON \`user\``); + await queryRunner.query(`DROP TABLE \`user\``); + await queryRunner.query(`DROP TABLE \`user_to_student\``); + await queryRunner.query(`DROP TABLE \`student\``); + await queryRunner.query(`DROP INDEX \`REL_6f2187911a8faba7c91a83194d\` ON \`classroom\``); + await queryRunner.query(`DROP TABLE \`classroom\``); + await queryRunner.query(`DROP TABLE \`image\``); + await queryRunner.query(`DROP INDEX \`IDX_0cb1810eca363db1e0bf13c3cf\` ON \`feature_flag\``); + await queryRunner.query(`DROP TABLE \`feature_flag\``); + } +}