Docker images / prepare-release (push) Successful in 10s
Docker images / backend-test (push) Successful in 2m2s
Docker images / frontend-image (push) Successful in 3m23s
Docker images / updater-image (push) Successful in 44s
Docker images / backend-image (push) Successful in 2m47s
Docker images / notify-webhook (push) Skipped
Docker images / publish-release (push) Successful in 11s
При первом рестарте wipe всех таблиц и импорт из SQLite в компактную схему. Retention через DROP PARTITION, lz4 и AIO worker. Co-authored-by: Cursor <cursoragent@cursor.com>
43 lines
1.6 KiB
TypeScript
43 lines
1.6 KiB
TypeScript
import { pool } from "./index.js"
|
|
import { applySqlMigrations } from "./migrate.js"
|
|
import { dropExpiredPartitions, ensurePartitionsAround } from "./partitions.js"
|
|
import { importSqliteToPostgres, shouldImportSqlite, sqliteFileLooksPresent } from "./sqlite-import.js"
|
|
import { env } from "../config.js"
|
|
|
|
const ETL_LOCK = 8723101
|
|
|
|
export async function initDatabase(): Promise<void> {
|
|
await pool.query("SELECT 1")
|
|
await applySqlMigrations(pool)
|
|
await ensurePartitionsAround(pool)
|
|
const client = await pool.connect()
|
|
try {
|
|
await client.query("SELECT pg_advisory_lock($1)", [ETL_LOCK])
|
|
if (await shouldImportSqlite(pool, env.DATABASE_PATH)) {
|
|
console.log(`SQLite → PostgreSQL: импорт ${env.DATABASE_PATH}`)
|
|
const report = await importSqliteToPostgres(pool, env.DATABASE_PATH, { strict: true })
|
|
console.log(
|
|
`SQLite → PostgreSQL: готово за ${report.durationMs}ms, таблиц ${Object.keys(report.tables).length}`,
|
|
)
|
|
} else {
|
|
const marker = await pool.query<{ sqlite_imported_at: string | null }>(
|
|
`SELECT sqlite_imported_at FROM data_migration WHERE id = 1`,
|
|
)
|
|
if (!marker.rows[0]?.sqlite_imported_at && !sqliteFileLooksPresent(env.DATABASE_PATH)) {
|
|
console.warn(
|
|
`SQLite → PostgreSQL: файл ${env.DATABASE_PATH} не найден, база после wipe остаётся пустой (defaults settings)`,
|
|
)
|
|
}
|
|
}
|
|
} finally {
|
|
try {
|
|
await client.query("SELECT pg_advisory_unlock($1)", [ETL_LOCK])
|
|
} catch {
|
|
/* ignore */
|
|
}
|
|
client.release()
|
|
}
|
|
await dropExpiredPartitions(pool)
|
|
await ensurePartitionsAround(pool)
|
|
}
|