make removed js migration run compatible with older setups

This commit is contained in:
einhornimmond 2025-05-09 07:54:23 +02:00
parent b9ea2a9888
commit 566fa537ff
2 changed files with 33 additions and 6 deletions

View File

@ -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"

View File

@ -25,6 +25,16 @@ async function connectToDatabaseServer(): Promise<Connection | null> {
}
}
async function convertJsToTsInMigrations(connection: Connection): Promise<number> {
const [result] = await connection.query<ResultSetHeader>(`
UPDATE ${CONFIG.MIGRATIONS_TABLE}
SET fileName = REPLACE(fileName, '.js', '.ts')
WHERE fileName LIKE '%.js'
`)
return result.affectedRows
}
export const getDatabaseState = async (): Promise<DatabaseState> => {
const connection = await connectToDatabaseServer()
if (!connection) {
@ -59,6 +69,26 @@ export const getDatabaseState = async (): Promise<DatabaseState> => {
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<RowDataPacket[]>(`
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<RowDataPacket[]>(
`SELECT * FROM ${CONFIG.MIGRATIONS_TABLE} ORDER BY version DESC LIMIT 1`,