kopia lustrzana https://github.com/FacilMap/facilmap
Rename Pads table to Maps
rodzic
7fa2208079
commit
7a5d74dc74
|
@ -90,8 +90,9 @@ export default class Database extends TypedEventEmitter<DatabaseEvents> {
|
||||||
|
|
||||||
async connect(force?: boolean): Promise<void> {
|
async connect(force?: boolean): Promise<void> {
|
||||||
await this._conn.authenticate();
|
await this._conn.authenticate();
|
||||||
|
await this.migrations._runMigrationsBeforeSync();
|
||||||
await this._conn.sync({ force: !!force });
|
await this._conn.sync({ force: !!force });
|
||||||
await this.migrations._runMigrations();
|
await this.migrations._runMigrationsAfterSync();
|
||||||
|
|
||||||
await this.routes.afterConnect();
|
await this.routes.afterConnect();
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,7 +49,7 @@ export default class DatabaseMaps {
|
||||||
legend2: { type: DataTypes.TEXT, allowNull: false, defaultValue: "" }
|
legend2: { type: DataTypes.TEXT, allowNull: false, defaultValue: "" }
|
||||||
}, {
|
}, {
|
||||||
sequelize: this._db._conn,
|
sequelize: this._db._conn,
|
||||||
modelName: "Pad"
|
modelName: "Map"
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -187,7 +187,7 @@ export default class DatabaseMaps {
|
||||||
const { count, rows } = await this.MapModel.findAndCountAll({
|
const { count, rows } = await this.MapModel.findAndCountAll({
|
||||||
where: Sequelize.and(
|
where: Sequelize.and(
|
||||||
{ searchEngines: true },
|
{ 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,
|
offset: query.start ?? 0,
|
||||||
...(query.limit != null ? { limit: query.limit } : {}),
|
...(query.limit != null ? { limit: query.limit } : {}),
|
||||||
|
|
|
@ -17,7 +17,11 @@ export default class DatabaseMigrations {
|
||||||
this._db = database;
|
this._db = database;
|
||||||
}
|
}
|
||||||
|
|
||||||
async _runMigrations(): Promise<void> {
|
async _runMigrationsBeforeSync(): Promise<void> {
|
||||||
|
await this._renamePadsTableMigration();
|
||||||
|
}
|
||||||
|
|
||||||
|
async _runMigrationsAfterSync(): Promise<void> {
|
||||||
await this._renameColMigrations();
|
await this._renameColMigrations();
|
||||||
await this._changeColMigrations();
|
await this._changeColMigrations();
|
||||||
await this._addColMigrations();
|
await this._addColMigrations();
|
||||||
|
@ -41,6 +45,17 @@ export default class DatabaseMigrations {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Rename Pads table to Maps */
|
||||||
|
async _renamePadsTableMigration(): Promise<void> {
|
||||||
|
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 */
|
/** Run any migrations that rename columns */
|
||||||
async _renameColMigrations(): Promise<void> {
|
async _renameColMigrations(): Promise<void> {
|
||||||
const queryInterface = this._db._conn.getQueryInterface();
|
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
|
// Rename writeId to adminId
|
||||||
if(!padAttrs.adminId) {
|
if(!mapAttrs.adminId) {
|
||||||
console.log("DB migration: Rename pad writeId to adminId");
|
console.log("DB migration: Rename map writeId to adminId");
|
||||||
const Pad = this._db.maps.MapModel;
|
const MapModel = this._db.maps.MapModel;
|
||||||
await queryInterface.renameColumn('Pads', 'writeId', 'adminId');
|
await queryInterface.renameColumn('Maps', 'writeId', 'adminId');
|
||||||
await queryInterface.addColumn('Pads', 'writeId', Pad.rawAttributes.writeId);
|
await queryInterface.addColumn('Maps', 'writeId', MapModel.rawAttributes.writeId);
|
||||||
|
|
||||||
const pads = await Pad.findAll<MapModel>();
|
const maps = await MapModel.findAll<MapModel>();
|
||||||
for(const pad of pads) {
|
for(const map of maps) {
|
||||||
let writeId;
|
let writeId;
|
||||||
do {
|
do {
|
||||||
writeId = generateRandomId(14);
|
writeId = generateRandomId(14);
|
||||||
} while (await this._db.maps.mapIdExists(writeId));
|
} 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();
|
const queryInterface = this._db._conn.getQueryInterface();
|
||||||
|
|
||||||
//////////
|
//////////
|
||||||
// Pads //
|
// Maps //
|
||||||
//////////
|
//////////
|
||||||
|
|
||||||
const padsAttributes = await queryInterface.describeTable("Pads");
|
const mapsAttributes = await queryInterface.describeTable("Maps");
|
||||||
|
|
||||||
// Forbid null pad name
|
// Forbid null map name
|
||||||
if (padsAttributes.name.allowNull) {
|
if (mapsAttributes.name.allowNull) {
|
||||||
console.log("DB migration: Change null pad names to \"\"");
|
console.log("DB migration: Change null map names to \"\"");
|
||||||
await this._db.maps.MapModel.update({ name: "" }, { where: { name: null as any } });
|
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
|
// Change description type from STRING to TEXT
|
||||||
if (padsAttributes.description.type !== "TEXT") {
|
if (mapsAttributes.description.type !== "TEXT") {
|
||||||
console.log("DB migration: Change Pads.description from STRING to TEXT");
|
console.log("DB migration: Change Maps.description from STRING to TEXT");
|
||||||
await queryInterface.changeColumn("Pads", "description", this._db.maps.MapModel.getAttributes().description);
|
await queryInterface.changeColumn("Maps", "description", this._db.maps.MapModel.getAttributes().description);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change legend1 type from STRING to TEXT
|
// Change legend1 type from STRING to TEXT
|
||||||
if (padsAttributes.legend1.type !== "TEXT") {
|
if (mapsAttributes.legend1.type !== "TEXT") {
|
||||||
console.log("DB migration: Change Pads.legend1 from STRING to TEXT");
|
console.log("DB migration: Change Maps.legend1 from STRING to TEXT");
|
||||||
await queryInterface.changeColumn("Pads", "legend1", this._db.maps.MapModel.getAttributes().legend1);
|
await queryInterface.changeColumn("Maps", "legend1", this._db.maps.MapModel.getAttributes().legend1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Change legend2 type from STRING to TEXT
|
// Change legend2 type from STRING to TEXT
|
||||||
if (padsAttributes.legend2.type !== "TEXT") {
|
if (mapsAttributes.legend2.type !== "TEXT") {
|
||||||
console.log("DB migration: Change Pads.legend2 from STRING to TEXT");
|
console.log("DB migration: Change Maps.legend2 from STRING to TEXT");
|
||||||
await queryInterface.changeColumn("Pads", "legend2", this._db.maps.MapModel.getAttributes().legend2);
|
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']
|
['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 model = this._db._conn.model(table);
|
||||||
const attributes = await queryInterface.describeTable(model.getTableName());
|
const attributes = await queryInterface.describeTable(model.getTableName());
|
||||||
const rawAttributes = model.getAttributes();
|
const rawAttributes = model.getAttributes();
|
||||||
|
@ -375,7 +390,7 @@ export default class DatabaseMigrations {
|
||||||
if(newVal)
|
if(newVal)
|
||||||
newData[dropdown.name] = newVal.value;
|
newData[dropdown.name] = newVal.value;
|
||||||
else if(newData[dropdown.name])
|
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))
|
if(!isEqual(newData, object.data))
|
||||||
|
@ -388,7 +403,7 @@ export default class DatabaseMigrations {
|
||||||
if(newDefault)
|
if(newDefault)
|
||||||
dropdown.default = newDefault.value;
|
dropdown.default = newDefault.value;
|
||||||
else
|
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) => {
|
dropdown.options?.forEach((option: any) => {
|
||||||
|
@ -548,7 +563,7 @@ export default class DatabaseMigrations {
|
||||||
if(await this._db.meta.getMeta("untitledMigrationCompleted") == "1")
|
if(await this._db.meta.getMeta("untitledMigrationCompleted") == "1")
|
||||||
return;
|
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.markers.MarkerModel.update({ name: "" }, { where: { name: "Untitled marker" } });
|
||||||
await this._db.lines.LineModel.update({ name: "" }, { where: { name: "Untitled line" } });
|
await this._db.lines.LineModel.update({ name: "" }, { where: { name: "Untitled line" } });
|
||||||
|
|
Ładowanie…
Reference in New Issue