mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
fix migration table, js -> ts
This commit is contained in:
parent
611328e7af
commit
3e96a1ab68
@ -1,4 +1,3 @@
|
||||
/* eslint-disable no-unused-vars */
|
||||
import { Connection, ResultSetHeader, RowDataPacket, createConnection } from 'mysql2/promise'
|
||||
|
||||
import { CONFIG } from './config'
|
||||
@ -32,6 +31,16 @@ export async function connectToDatabaseServer(
|
||||
return 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) {
|
||||
@ -66,9 +75,29 @@ 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`,
|
||||
`SELECT fileName FROM ${CONFIG.MIGRATIONS_TABLE} ORDER BY version DESC LIMIT 1`,
|
||||
)
|
||||
if (rows.length === 0) {
|
||||
return DatabaseState.LOWER_VERSION
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user