Skip to content

Sync-in · Sovereign platform for file storage, sharing, synchronization, and collaboration.

fix(backend:database): align migration session settings and fix user creation exit code

johaven33e3047b09c5
diff --git a/backend/src/infrastructure/database/constants.ts b/backend/src/infrastructure/database/constants.ts
index 3dae7400..31234efd 100644
--- a/backend/src/infrastructure/database/constants.ts
+++ b/backend/src/infrastructure/database/constants.ts
@@ -3,6 +3,12 @@ import path from 'node:path'
export const DB_CHARSET = 'utf8mb4'
export const DB_TOKEN_PROVIDER = 'DB'
+export const DB_SESSION_INIT_QUERIES = [
+ // Use UTC for SQL date/time functions and TIMESTAMP values.
+ `SET time_zone = '+00:00'`,
+ // Use InnoDB for tables created without an explicit storage engine.
+ `SET SESSION default_storage_engine = 'InnoDB'`
+] as const
export const MIGRATIONS_PATH = path.relative(process.cwd(), path.join(__dirname, '../../../migrations'))
export function getSchemaPath(): string {
diff --git a/backend/src/infrastructure/database/database.module.ts b/backend/src/infrastructure/database/database.module.ts
index 9556d269..38a5db7d 100644
--- a/backend/src/infrastructure/database/database.module.ts
+++ b/backend/src/infrastructure/database/database.module.ts
@@ -3,7 +3,7 @@ import { BeforeApplicationShutdown, Global, Inject, Module, OnModuleInit } from
import { MySql2Client } from 'drizzle-orm/mysql2'
import { Connection, Pool } from 'mysql2'
import { configuration } from '../../configuration/config.environment'
-import { DB_TOKEN_PROVIDER } from './constants'
+import { DB_SESSION_INIT_QUERIES, DB_TOKEN_PROVIDER } from './constants'
import { DatabaseLogger } from './database.logger'
import type { DBSchema } from './interfaces/database.interface'
import * as schema from './schema'
@@ -33,10 +33,9 @@ export class DatabaseModule implements OnModuleInit, BeforeApplicationShutdown {
async onModuleInit() {
const pool: Pool = (this.db as any).$client
pool.on('connection', (conn: Connection) => {
- // Force UTC timezone for every new MySQL connection.
- conn.query(`SET time_zone = '+00:00'`)
- // Use InnoDB for tables created dynamically by the application.
- conn.query(`SET SESSION default_storage_engine = 'InnoDB'`)
+ for (const query of DB_SESSION_INIT_QUERIES) {
+ conn.query(query)
+ }
})
// Ensure MySQL connection is healthy
diff --git a/backend/src/infrastructure/database/scripts/create-user.ts b/backend/src/infrastructure/database/scripts/create-user.ts
index 8bce9c16..e9712f88 100644
--- a/backend/src/infrastructure/database/scripts/create-user.ts
+++ b/backend/src/infrastructure/database/scripts/create-user.ts
@@ -110,4 +110,7 @@ async function main() {
await createUserInDatabase(user)
}
-main().catch((e: Error) => console.error(e))
+main().catch((e: Error) => {
+ console.error(e)
+ process.exitCode = 1
+})
diff --git a/backend/src/infrastructure/database/scripts/migrate.ts b/backend/src/infrastructure/database/scripts/migrate.ts
index 2b45f922..0cadc358 100644
--- a/backend/src/infrastructure/database/scripts/migrate.ts
+++ b/backend/src/infrastructure/database/scripts/migrate.ts
@@ -1,14 +1,16 @@
import { sql } from 'drizzle-orm'
import { DrizzleQueryError } from 'drizzle-orm/errors'
import { migrate } from 'drizzle-orm/mysql2/migrator'
-import { MIGRATIONS_PATH } from '../constants'
+import { DB_SESSION_INIT_QUERIES, MIGRATIONS_PATH } from '../constants'
import { getDB } from './db'
async function applyMigrations(): Promise<void> {
const db = await getDB()
try {
- await db.execute(sql`SET SESSION default_storage_engine = 'InnoDB'`)
+ for (const query of DB_SESSION_INIT_QUERIES) {
+ await db.execute(sql.raw(query))
+ }
await migrate(db, { migrationsFolder: MIGRATIONS_PATH })
console.log('Database migrations applied successfully!')
} finally {