Skip to content

Commit 0db7527

Browse files
committed
Fixed migration upgrade function
1 parent 7ef627b commit 0db7527

File tree

1 file changed

+36
-33
lines changed

1 file changed

+36
-33
lines changed

drizzle-orm/src/up-migrations/effect-pg.ts

Lines changed: 36 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Effect } from 'effect';
2-
import type { MigrationMeta } from '~/migrator';
3-
import type { PgEffectSession } from '~/pg-core/effect';
2+
import type { QueryEffectHKTBase } from '~/effect-core/query-effect.ts';
3+
import type { MigrationMeta } from '~/migrator.ts';
4+
import type { PgEffectSession } from '~/pg-core/effect/session.ts';
45
import { sql } from '~/sql/sql.ts';
56

67
const CURRENT_MIGRATION_TABLE_VERSION = 1;
@@ -22,12 +23,12 @@ function getVersion(columns: string[]) {
2223
*/
2324
const upgradeFunctions: Record<
2425
number,
25-
(
26+
<TEffectHKT extends QueryEffectHKTBase>(
2627
migrationsSchema: string,
2728
migrationsTable: string,
28-
session: PgEffectSession,
29+
session: PgEffectSession<TEffectHKT>,
2930
localMigrations: MigrationMeta[],
30-
) => Effect.Effect<void, unknown, unknown>
31+
) => Effect.Effect<void, TEffectHKT['error'], TEffectHKT['context']>
3132
> = {
3233
/**
3334
* Upgrade from version 0 to version 1:
@@ -103,31 +104,32 @@ const upgradeFunctions: Record<
103104
* Version 0: Original schema (id, hash, created_at)
104105
* Version 1: Extended schema (id, hash, created_at, name, applied_at)
105106
*/
106-
export const upgradeIfNeeded: (
107+
export const upgradeIfNeeded: <TEffectHKT extends QueryEffectHKTBase>(
107108
migrationsSchema: string,
108109
migrationsTable: string,
109-
session: PgEffectSession,
110+
session: PgEffectSession<TEffectHKT>,
110111
localMigrations: MigrationMeta[],
111-
) => Effect.Effect<UpgradeResult, unknown, unknown> = Effect.fn('upgradeIfNeeded')(function*(
112-
migrationsSchema: string,
113-
migrationsTable: string,
114-
session: PgEffectSession,
115-
localMigrations: MigrationMeta[],
116-
) {
117-
// Check if the table exists at all
118-
const result = yield* session.all(
119-
sql`SELECT 1 FROM information_schema.tables
112+
) => Effect.Effect<UpgradeResult, TEffectHKT['error'], TEffectHKT['context']> = Effect.fn('upgradeIfNeeded')(
113+
function*<TEffectHKT extends QueryEffectHKTBase>(
114+
migrationsSchema: string,
115+
migrationsTable: string,
116+
session: PgEffectSession<TEffectHKT>,
117+
localMigrations: MigrationMeta[],
118+
) {
119+
// Check if the table exists at all
120+
const result = yield* session.all(
121+
sql`SELECT 1 FROM information_schema.tables
120122
WHERE table_schema = ${migrationsSchema}
121123
AND table_name = ${migrationsTable}`,
122-
);
124+
);
123125

124-
if (result.length === 0) {
125-
return { newDb: true };
126-
}
126+
if (result.length === 0) {
127+
return { newDb: true };
128+
}
127129

128-
// Table exists, check table shape
129-
const rows = yield* session.all<{ schema: string; table_name: string; column_name: string; type: string }>(
130-
sql`SELECT
130+
// Table exists, check table shape
131+
const rows = yield* session.all<{ schema: string; table_name: string; column_name: string; type: string }>(
132+
sql`SELECT
131133
n.nspname AS "schema",
132134
c.relname AS "table_name",
133135
a.attname AS "column_name",
@@ -142,17 +144,18 @@ export const upgradeIfNeeded: (
142144
AND n.nspname = ${migrationsSchema}
143145
AND c.relname = ${migrationsTable}
144146
ORDER BY a.attnum;`,
145-
);
147+
);
146148

147-
const version = getVersion(rows.map((r) => r.column_name));
149+
const version = getVersion(rows.map((r) => r.column_name));
148150

149-
for (let v = version; v < CURRENT_MIGRATION_TABLE_VERSION; v++) {
150-
const upgradeFn = upgradeFunctions[v];
151-
if (!upgradeFn) {
152-
throw new Error(`No upgrade path from migration table version ${v} to ${v + 1}`);
151+
for (let v = version; v < CURRENT_MIGRATION_TABLE_VERSION; v++) {
152+
const upgradeFn = upgradeFunctions[v];
153+
if (!upgradeFn) {
154+
throw new Error(`No upgrade path from migration table version ${v} to ${v + 1}`);
155+
}
156+
yield* upgradeFn(migrationsSchema, migrationsTable, session, localMigrations);
153157
}
154-
yield* upgradeFn(migrationsSchema, migrationsTable, session, localMigrations);
155-
}
156158

157-
return { prevVersion: version, currentVersion: CURRENT_MIGRATION_TABLE_VERSION };
158-
});
159+
return { prevVersion: version, currentVersion: CURRENT_MIGRATION_TABLE_VERSION };
160+
},
161+
);

0 commit comments

Comments
 (0)