GitHub

migrator

API reference for migrator.

Migrations for PostgreSQL (Effect's @effect/sql-pg PgMigrator): Migrator's runner, dumping the schema with pg_dump.

pg_dump connects with the client's own libpq parameters (the URL and explicit fields, socket, user, database, application_name, options, TLS, connect timeout); the password goes through PGPASSWORD and nothing else of the environment but PATH. A dump that fails is logged, as for every driver.

Re-exports

Values

AttributeTypeDescription
runRunner[DumpServices]

Run the new migrations of loader (see migrator.make); with a schema_directory, dump the schema there with pg_dump.

Types

TypeDefinitionDescription
DumpServicestype DumpServices = PgClient | ChildProcessSpawner | FileSystem | Path

Functions

dump_schema

dump_schema(path, table) -> Effect[None, MigrationError, DumpServices]

Write the schema and the migrations table's rows to path.

ParameterTypeDescription
pathstr
tablestr

Returns Effect[None, MigrationError, DumpServices]

def dump_schema(path: str, table: str) -> Effect[None, MigrationError, DumpServices]:
    """Write the schema and the migrations table's rows to ``path``."""

    def body():
        fs = yield from FileSystem()
        paths = yield from Path()
        dump = yield from _dump_all(table)
        yield from fs.make_directory(paths.dirname(path), recursive=True)
        yield from fs.write_file_string(path, dump)

    def failed(error: Exception) -> Effect[Never, MigrationError]:
        message = getattr(error, "message", None) or str(error)
        return fail(MigrationError(kind="Failed", message=str(message), cause=error))

    written: Effect[None, Exception, DumpServices] = gen(body)
    return written.catch_all(failed)

layer

layer[R = Never](*, loader, schema_directory=None, table='effect_sql_migrations') -> Layer[Never, MigrationError | SqlError, SqlClient | DumpServices | R]

Runs the migrations when built.

ParameterTypeDescription
loaderLoader[R]
schema_directorystr | None = None
tablestr = 'effect_sql_migrations'

Returns Layer[Never, MigrationError | SqlError, SqlClient | DumpServices | R]

def layer[R = Never](
    *,
    loader: Loader[R],
    schema_directory: str | None = None,
    table: str = "effect_sql_migrations",
) -> Layer[Never, MigrationError | SqlError, SqlClient | DumpServices | R]:
    """Runs the migrations when built."""
    return layer_effect_discard(run(loader=loader, schema_directory=schema_directory, table=table))