From 7a5d74dc74b56b126f5e134e1a5425411d4f0018 Mon Sep 17 00:00:00 2001 From: Candid Dauth Date: Mon, 22 Apr 2024 13:05:17 +0200 Subject: [PATCH] Rename Pads table to Maps --- server/src/database/database.ts | 3 +- server/src/database/map.ts | 4 +- server/src/database/migrations.ts | 73 +++++++++++++++++++------------ 3 files changed, 48 insertions(+), 32 deletions(-) diff --git a/server/src/database/database.ts b/server/src/database/database.ts index 38a23a0b..f0eaf46f 100644 --- a/server/src/database/database.ts +++ b/server/src/database/database.ts @@ -90,8 +90,9 @@ export default class Database extends TypedEventEmitter { async connect(force?: boolean): Promise { await this._conn.authenticate(); + await this.migrations._runMigrationsBeforeSync(); await this._conn.sync({ force: !!force }); - await this.migrations._runMigrations(); + await this.migrations._runMigrationsAfterSync(); await this.routes.afterConnect(); } diff --git a/server/src/database/map.ts b/server/src/database/map.ts index 91dc8944..97432952 100644 --- a/server/src/database/map.ts +++ b/server/src/database/map.ts @@ -49,7 +49,7 @@ export default class DatabaseMaps { legend2: { type: DataTypes.TEXT, allowNull: false, defaultValue: "" } }, { sequelize: this._db._conn, - modelName: "Pad" + modelName: "Map" }); } @@ -187,7 +187,7 @@ export default class DatabaseMaps { const { count, rows } = await this.MapModel.findAndCountAll({ where: Sequelize.and( { searchEngines: true }, - Sequelize.where(Sequelize.fn("lower", Sequelize.col(`Pad.name`)), {[Op.like]: `%${like}%`}) + Sequelize.where(Sequelize.fn("lower", Sequelize.col(`Map.name`)), {[Op.like]: `%${like}%`}) ), offset: query.start ?? 0, ...(query.limit != null ? { limit: query.limit } : {}), diff --git a/server/src/database/migrations.ts b/server/src/database/migrations.ts index fe9a4587..d48eed1b 100644 --- a/server/src/database/migrations.ts +++ b/server/src/database/migrations.ts @@ -17,7 +17,11 @@ export default class DatabaseMigrations { this._db = database; } - async _runMigrations(): Promise { + async _runMigrationsBeforeSync(): Promise { + await this._renamePadsTableMigration(); + } + + async _runMigrationsAfterSync(): Promise { await this._renameColMigrations(); await this._changeColMigrations(); await this._addColMigrations(); @@ -41,6 +45,17 @@ export default class DatabaseMigrations { }); } + /** Rename Pads table to Maps */ + async _renamePadsTableMigration(): Promise { + const queryInterface = this._db._conn.getQueryInterface(); + + if (await queryInterface.tableExists("Pads")) { + console.log("DB migration: Rename Pads table to Maps"); + + await queryInterface.renameTable("Pads", "Maps"); + } + } + /** Run any migrations that rename columns */ async _renameColMigrations(): Promise { const queryInterface = this._db._conn.getQueryInterface(); @@ -85,23 +100,23 @@ export default class DatabaseMigrations { } - const padAttrs = await queryInterface.describeTable('Pads'); + const mapAttrs = await queryInterface.describeTable('Maps'); // Rename writeId to adminId - if(!padAttrs.adminId) { - console.log("DB migration: Rename pad writeId to adminId"); - const Pad = this._db.maps.MapModel; - await queryInterface.renameColumn('Pads', 'writeId', 'adminId'); - await queryInterface.addColumn('Pads', 'writeId', Pad.rawAttributes.writeId); + if(!mapAttrs.adminId) { + console.log("DB migration: Rename map writeId to adminId"); + const MapModel = this._db.maps.MapModel; + await queryInterface.renameColumn('Maps', 'writeId', 'adminId'); + await queryInterface.addColumn('Maps', 'writeId', MapModel.rawAttributes.writeId); - const pads = await Pad.findAll(); - for(const pad of pads) { + const maps = await MapModel.findAll(); + for(const map of maps) { let writeId; do { writeId = generateRandomId(14); } while (await this._db.maps.mapIdExists(writeId)); - await Pad.update({writeId}, { where: { id: pad.id } }); + await MapModel.update({writeId}, { where: { id: map.id } }); } } } @@ -112,34 +127,34 @@ export default class DatabaseMigrations { const queryInterface = this._db._conn.getQueryInterface(); ////////// - // Pads // + // Maps // ////////// - const padsAttributes = await queryInterface.describeTable("Pads"); + const mapsAttributes = await queryInterface.describeTable("Maps"); - // Forbid null pad name - if (padsAttributes.name.allowNull) { - console.log("DB migration: Change null pad names to \"\""); + // Forbid null map name + if (mapsAttributes.name.allowNull) { + console.log("DB migration: Change null map names to \"\""); await this._db.maps.MapModel.update({ name: "" }, { where: { name: null as any } }); - await queryInterface.changeColumn("Pads", "name", this._db.maps.MapModel.getAttributes().name); + await queryInterface.changeColumn("Maps", "name", this._db.maps.MapModel.getAttributes().name); } // Change description type from STRING to TEXT - if (padsAttributes.description.type !== "TEXT") { - console.log("DB migration: Change Pads.description from STRING to TEXT"); - await queryInterface.changeColumn("Pads", "description", this._db.maps.MapModel.getAttributes().description); + if (mapsAttributes.description.type !== "TEXT") { + console.log("DB migration: Change Maps.description from STRING to TEXT"); + await queryInterface.changeColumn("Maps", "description", this._db.maps.MapModel.getAttributes().description); } // Change legend1 type from STRING to TEXT - if (padsAttributes.legend1.type !== "TEXT") { - console.log("DB migration: Change Pads.legend1 from STRING to TEXT"); - await queryInterface.changeColumn("Pads", "legend1", this._db.maps.MapModel.getAttributes().legend1); + if (mapsAttributes.legend1.type !== "TEXT") { + console.log("DB migration: Change Maps.legend1 from STRING to TEXT"); + await queryInterface.changeColumn("Maps", "legend1", this._db.maps.MapModel.getAttributes().legend1); } // Change legend2 type from STRING to TEXT - if (padsAttributes.legend2.type !== "TEXT") { - console.log("DB migration: Change Pads.legend2 from STRING to TEXT"); - await queryInterface.changeColumn("Pads", "legend2", this._db.maps.MapModel.getAttributes().legend2); + if (mapsAttributes.legend2.type !== "TEXT") { + console.log("DB migration: Change Maps.legend2 from STRING to TEXT"); + await queryInterface.changeColumn("Maps", "legend2", this._db.maps.MapModel.getAttributes().legend2); } @@ -339,7 +354,7 @@ export default class DatabaseMigrations { ['Marker', 'pos'], ['LinePoint', 'pos'], ['RoutePoint', 'pos'] ]; - for (const table of [ 'Pad', 'Marker', 'Type', 'View', 'Line', 'LinePoint' ]) { + for (const table of [ 'Map', 'Marker', 'Type', 'View', 'Line', 'LinePoint' ]) { const model = this._db._conn.model(table); const attributes = await queryInterface.describeTable(model.getTableName()); const rawAttributes = model.getAttributes(); @@ -375,7 +390,7 @@ export default class DatabaseMigrations { if(newVal) newData[dropdown.name] = newVal.value; else if(newData[dropdown.name]) - console.log(`DB migration: Warning: Dropdown key ${newData[dropdown.name]} for field ${dropdown.name} of type ${type.name} of pad ${type.padId} does not exist.`); + console.log(`DB migration: Warning: Dropdown key ${newData[dropdown.name]} for field ${dropdown.name} of type ${type.name} of map ${type.padId} does not exist.`); } if(!isEqual(newData, object.data)) @@ -388,7 +403,7 @@ export default class DatabaseMigrations { if(newDefault) dropdown.default = newDefault.value; else - console.log(`DB migration: Warning: Default dropdown key ${dropdown.default} for field ${dropdown.name} of type ${type.name} of pad ${type.padId} does not exist.`); + console.log(`DB migration: Warning: Default dropdown key ${dropdown.default} for field ${dropdown.name} of type ${type.name} of map ${type.padId} does not exist.`); } dropdown.options?.forEach((option: any) => { @@ -548,7 +563,7 @@ export default class DatabaseMigrations { if(await this._db.meta.getMeta("untitledMigrationCompleted") == "1") return; - console.log("DB migration: Empty name for unnamed markers/lines/pads"); + console.log("DB migration: Empty name for unnamed markers/lines/maps"); await this._db.markers.MarkerModel.update({ name: "" }, { where: { name: "Untitled marker" } }); await this._db.lines.LineModel.update({ name: "" }, { where: { name: "Untitled line" } });