From 566fa537ff93fabcd1777fd7232191d0139f1c4a Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Fri, 9 May 2025 07:54:23 +0200 Subject: [PATCH] make removed js migration run compatible with older setups --- database/package.json | 9 +++------ database/src/prepare.ts | 30 ++++++++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 6 deletions(-) diff --git a/database/package.json b/database/package.json index 999792eb3..be28a80c6 100644 --- a/database/package.json +++ b/database/package.json @@ -17,15 +17,12 @@ "scripts": { "build": "mkdirp build/src/config/ && ncp src/config build/src/config && tsc --build", "clean": "tsc --build --clean", - "up": "cross-env TZ=UTC node build/src/index.js up", - "down": "cross-env TZ=UTC node build/src/index.js down", - "reset": "cross-env TZ=UTC node build/src/index.js reset", "lint": "biome check --error-on-warnings .", "lint:fix": "biome check --error-on-warnings . --write", "clear": "cross-env TZ=UTC tsx src/index.ts clear", - "dev_up": "cross-env TZ=UTC tsx src/index.ts up", - "dev_down": "cross-env TZ=UTC tsx src/index.ts down", - "dev_reset": "cross-env TZ=UTC tsx src/index.ts reset", + "up": "cross-env TZ=UTC tsx src/index.ts up", + "down": "cross-env TZ=UTC tsx src/index.ts down", + "reset": "cross-env TZ=UTC tsx src/index.ts reset", "up:backend_test": "cross-env TZ=UTC DB_DATABASE=gradido_test_backend tsx src/index.ts up", "up:federation_test": "cross-env TZ=UTC DB_DATABASE=gradido_test_federation tsx src/index.ts up", "up:dht_test": "cross-env TZ=UTC DB_DATABASE=gradido_test_dht tsx src/index.ts up" diff --git a/database/src/prepare.ts b/database/src/prepare.ts index 0ad0f9ba9..325c18b0e 100644 --- a/database/src/prepare.ts +++ b/database/src/prepare.ts @@ -25,6 +25,16 @@ async function connectToDatabaseServer(): Promise { } } +async function convertJsToTsInMigrations(connection: Connection): Promise { + const [result] = await connection.query(` + UPDATE ${CONFIG.MIGRATIONS_TABLE} + SET fileName = REPLACE(fileName, '.js', '.ts') + WHERE fileName LIKE '%.js' + `) + + return result.affectedRows +} + export const getDatabaseState = async (): Promise => { const connection = await connectToDatabaseServer() if (!connection) { @@ -59,6 +69,26 @@ export const getDatabaseState = async (): Promise => { await connection.query(`USE ${CONFIG.DB_DATABASE}`) + // check structure of fileNames, normally they should all ends with .ts + // but from older version they can end all on .js, that we need to fix + // they can even be mixed, but this we cannot easily fix automatic, so we must throw an error + const [counts] = await connection.query(` + SELECT + SUM(fileName LIKE '%.js') AS jsCount, + SUM(fileName LIKE '%.ts') AS tsCount + FROM ${CONFIG.MIGRATIONS_TABLE} + `) + + if (counts[0].jsCount > 0 && counts[0].tsCount > 0) { + throw new Error('Mixed JS and TS migrations found, we cannot fix this automatically') + } + + if (counts[0].jsCount > 0) { + const converted = await convertJsToTsInMigrations(connection) + // biome-ignore lint/suspicious/noConsole: no logger present + console.log(`Converted ${converted} JS migrations to TS`) + } + // check if the database is up to date const [rows] = await connection.query( `SELECT * FROM ${CONFIG.MIGRATIONS_TABLE} ORDER BY version DESC LIMIT 1`,