mirror of
https://github.com/whyour/qinglong.git
synced 2026-09-21 00:38:14 +08:00
35 lines
993 B
TypeScript
35 lines
993 B
TypeScript
import { createHash } from 'crypto';
|
|
import type { MigrationStreamStep } from '../core/migrationStream';
|
|
import type { PostgresMigrationContext } from '../adapters/postgresMigrationStreamStore';
|
|
|
|
export interface PostgresSqlMigrationDefinition {
|
|
readonly id: string;
|
|
readonly statements: readonly string[];
|
|
}
|
|
|
|
export function checksumPostgresStatements(
|
|
statements: readonly string[],
|
|
): string {
|
|
return createHash('sha256')
|
|
.update(
|
|
JSON.stringify({
|
|
format: 1,
|
|
statements,
|
|
}),
|
|
)
|
|
.digest('hex');
|
|
}
|
|
|
|
export function definePostgresSqlMigration(
|
|
definition: PostgresSqlMigrationDefinition,
|
|
): MigrationStreamStep<PostgresMigrationContext> {
|
|
const statements = Object.freeze([...definition.statements]);
|
|
return Object.freeze({
|
|
id: definition.id,
|
|
checksum: checksumPostgresStatements(statements),
|
|
async up(context: PostgresMigrationContext) {
|
|
for (const statement of statements) await context.query(statement);
|
|
},
|
|
});
|
|
}
|