@vibeorm/migrate 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,103 @@
1
+ /**
2
+ * Migration Runner — Apply SQL files with tracking
3
+ *
4
+ * Handles creating the migration tracking table, querying applied migrations,
5
+ * executing migration SQL files, and recording results.
6
+ */
7
+
8
+ import type { SqlExecutor } from "./types.ts";
9
+ import { splitSqlStatements } from "./sql-utils.ts";
10
+
11
+ // ─── Types ────────────────────────────────────────────────────────
12
+
13
+ export type AppliedMigration = {
14
+ id: number;
15
+ migrationName: string;
16
+ checksum: string;
17
+ appliedAt: Date;
18
+ executionTime: number | null;
19
+ };
20
+
21
+ // ─── Migration Table Management ───────────────────────────────────
22
+
23
+ const CREATE_TABLE_SQL = `
24
+ CREATE TABLE IF NOT EXISTS "_vibeorm_migrations" (
25
+ "id" SERIAL PRIMARY KEY,
26
+ "migration_name" TEXT NOT NULL UNIQUE,
27
+ "checksum" TEXT NOT NULL,
28
+ "applied_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
29
+ "execution_time" INTEGER
30
+ );
31
+ `;
32
+
33
+ export async function ensureMigrationTable(params: { executor: SqlExecutor }): Promise<void> {
34
+ await params.executor({ text: CREATE_TABLE_SQL });
35
+ }
36
+
37
+ export async function getAppliedMigrations(params: { executor: SqlExecutor }): Promise<AppliedMigration[]> {
38
+ const rows = await params.executor({
39
+ text: `SELECT id, migration_name, checksum, applied_at, execution_time FROM "_vibeorm_migrations" ORDER BY id;`,
40
+ });
41
+
42
+ return rows.map((row) => ({
43
+ id: row.id as number,
44
+ migrationName: row.migration_name as string,
45
+ checksum: row.checksum as string,
46
+ appliedAt: new Date(row.applied_at as string),
47
+ executionTime: row.execution_time as number | null,
48
+ }));
49
+ }
50
+
51
+ // ─── Apply Migration ──────────────────────────────────────────────
52
+
53
+ export async function applyMigration(params: {
54
+ executor: SqlExecutor;
55
+ migrationName: string;
56
+ sql: string;
57
+ checksum: string;
58
+ }): Promise<{ executionTime: number }> {
59
+ const { executor, migrationName, sql, checksum } = params;
60
+ const start = Date.now();
61
+
62
+ // Split migration SQL into individual statements to handle
63
+ // DO $$ blocks and multi-statement migrations correctly
64
+ const statements = splitSqlStatements({ sql });
65
+ for (const stmt of statements) {
66
+ await executor({ text: stmt });
67
+ }
68
+
69
+ const executionTime = Date.now() - start;
70
+
71
+ // Record the applied migration
72
+ await executor({
73
+ text: `INSERT INTO "_vibeorm_migrations" ("migration_name", "checksum", "execution_time") VALUES ($1, $2, $3);`,
74
+ values: [migrationName, checksum, executionTime],
75
+ });
76
+
77
+ return { executionTime };
78
+ }
79
+
80
+ // ─── Repair Tools ─────────────────────────────────────────────────
81
+
82
+ export async function markMigrationApplied(params: {
83
+ executor: SqlExecutor;
84
+ migrationName: string;
85
+ checksum: string;
86
+ }): Promise<void> {
87
+ const { executor, migrationName, checksum } = params;
88
+ await executor({
89
+ text: `INSERT INTO "_vibeorm_migrations" ("migration_name", "checksum", "execution_time") VALUES ($1, $2, $3) ON CONFLICT ("migration_name") DO NOTHING;`,
90
+ values: [migrationName, checksum, 0],
91
+ });
92
+ }
93
+
94
+ export async function removeMigrationRecord(params: {
95
+ executor: SqlExecutor;
96
+ migrationName: string;
97
+ }): Promise<void> {
98
+ const { executor, migrationName } = params;
99
+ await executor({
100
+ text: `DELETE FROM "_vibeorm_migrations" WHERE "migration_name" = $1;`,
101
+ values: [migrationName],
102
+ });
103
+ }