Skip to content

feat(sql): @effect/sql-pg client, Effect Migrator and test layer #72

Closed

Description

@allisonmahmood

Problem

packages/db hand-rolls the Postgres client, the migration runner and a text-id ledger. The port replaces all three with @effect/sql-pg and Effect's Migrator, owned by a sql package that holds no tables.

Do

  • New packages/sql: PgClient.layer from a Config slice (DATABASE_URL as Redacted; a prepare: false knob reserved for the Neon pooler); Migrator.make({}) from effect/unstable/sql (not PgMigrator.layer) with fromRecord, composing per-package migration records { ...auth, ...patches } into one global integer id sequence — duplicates fail; the ledger is Effect's schema_migrations(migration_id integer, name, created_at); all pending steps run in one transaction under ACCESS EXCLUSIVE. Both behaviour changes from today are named in the PR.
  • Test layer: Layer.unwrap + acquireRelease(createEmptyPostgresDatabase, drop) around PgClient.layer + the migrator, so an it.layer block gets a fresh, migrated database torn down with the layer. Copy from packages/effect-slice/src/slice.test.ts and test/postgres.ts on prototype/effect-slice.
  • Interim: today's six SCHEMA_MIGRATIONS (five after reports) move under fromRecord unchanged (sql.unsafe for multi-statement strings; drop the IF NOT EXISTS idempotency — the ledger is the guard). packages/db's migrate.ts becomes a thin Effect.runPromise seam over the migrator; the dev runner and vitest globalSetup migrate through it. packages/db's own runner and schema_migrations text ledger are deleted. No prod database exists, so no rename or mark-applied step.
  • Row decoding convention documented in packages/sql/README.md: capabilities decode rows through SqlSchema with a Schema.Class result, in one module per capability; SchemaError on a row → Effect.die; SqlError stays in the channel.
  • packages/sql/CONTEXT.md (migration, ledger) and promote the language-service warnings to error for the package.

Done when

Server and tests migrate through Effect's Migrator; a duplicate id fails a test; it.layer template works from an empty database; packages/db keeps only the query code the auth and patches tickets will take.

Decisions implemented

Spec §2 (SQL, Migrator), §3 (Postgres only), §7 (tests); vertical slice.


Filed from the Effect v4 port spec (#68) by the map task Write the port spec and file the build tickets.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:serverready-for-agent

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions