mirror of
https://github.com/arthur-pbty/flint.git
synced 2026-08-06 04:29:43 +02:00
nettoyage du code
This commit is contained in:
@@ -10,12 +10,21 @@ const LOCK_CLASS_ID = 7813;
|
||||
const LOCK_OBJECT_ID = 4312;
|
||||
|
||||
const parseBoolean = (value, fallback = false) => {
|
||||
if (value === undefined || value === null || String(value).trim().length === 0) {
|
||||
if (
|
||||
value === undefined ||
|
||||
value === null ||
|
||||
String(value).trim().length === 0
|
||||
) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
const normalized = String(value).trim().toLowerCase();
|
||||
return normalized === "true" || normalized === "1" || normalized === "yes" || normalized === "on";
|
||||
return (
|
||||
normalized === "true" ||
|
||||
normalized === "1" ||
|
||||
normalized === "yes" ||
|
||||
normalized === "on"
|
||||
);
|
||||
};
|
||||
|
||||
const sha256 = (content) => createHash("sha256").update(content).digest("hex");
|
||||
@@ -33,7 +42,9 @@ const loadMigrations = async (migrationsDir) => {
|
||||
const files = await listMigrationFiles(migrationsDir);
|
||||
|
||||
if (files.length === 0) {
|
||||
throw new Error(`[migrate] no SQL migration files found in ${migrationsDir}`);
|
||||
throw new Error(
|
||||
`[migrate] no SQL migration files found in ${migrationsDir}`,
|
||||
);
|
||||
}
|
||||
|
||||
const migrations = [];
|
||||
@@ -63,9 +74,13 @@ const ensureMigrationTable = async (client) => {
|
||||
};
|
||||
|
||||
const applyMigrations = async (client, migrations) => {
|
||||
const applied = await client.query("SELECT version, checksum FROM schema_migrations ORDER BY version ASC");
|
||||
const applied = await client.query(
|
||||
"SELECT version, checksum FROM schema_migrations ORDER BY version ASC",
|
||||
);
|
||||
|
||||
const appliedByVersion = new Map(applied.rows.map((row) => [String(row.version), String(row.checksum)]));
|
||||
const appliedByVersion = new Map(
|
||||
applied.rows.map((row) => [String(row.version), String(row.checksum)]),
|
||||
);
|
||||
|
||||
for (const migration of migrations) {
|
||||
const existingChecksum = appliedByVersion.get(migration.fileName);
|
||||
@@ -91,7 +106,9 @@ const applyMigrations = async (client, migrations) => {
|
||||
console.log(`[migrate] applied ${migration.fileName}`);
|
||||
} catch (error) {
|
||||
await client.query("ROLLBACK").catch(() => undefined);
|
||||
throw new Error(`[migrate] failed while applying ${migration.fileName}`, { cause: error });
|
||||
throw new Error(`[migrate] failed while applying ${migration.fileName}`, {
|
||||
cause: error,
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -111,8 +128,8 @@ const main = async () => {
|
||||
connectionString: process.env.DATABASE_URL,
|
||||
ssl: parseBoolean(process.env.DATABASE_SSL, false)
|
||||
? {
|
||||
rejectUnauthorized: true,
|
||||
}
|
||||
rejectUnauthorized: true,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
|
||||
@@ -121,12 +138,20 @@ const main = async () => {
|
||||
try {
|
||||
const migrations = await loadMigrations(migrationsDir);
|
||||
|
||||
await client.query("SELECT pg_advisory_lock($1, $2)", [LOCK_CLASS_ID, LOCK_OBJECT_ID]);
|
||||
await client.query("SELECT pg_advisory_lock($1, $2)", [
|
||||
LOCK_CLASS_ID,
|
||||
LOCK_OBJECT_ID,
|
||||
]);
|
||||
await ensureMigrationTable(client);
|
||||
await applyMigrations(client, migrations);
|
||||
console.log("[migrate] completed");
|
||||
} finally {
|
||||
await client.query("SELECT pg_advisory_unlock($1, $2)", [LOCK_CLASS_ID, LOCK_OBJECT_ID]).catch(() => undefined);
|
||||
await client
|
||||
.query("SELECT pg_advisory_unlock($1, $2)", [
|
||||
LOCK_CLASS_ID,
|
||||
LOCK_OBJECT_ID,
|
||||
])
|
||||
.catch(() => undefined);
|
||||
client.release();
|
||||
await pool.end();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user