({
dumpSchema = () => Effect.void
}: {
dumpSchema?: (
path: string,
migrationsTable: string
) => Effect.Effect<void, MigrationError, RD>
})
| 71 | * @since 1.0.0 |
| 72 | */ |
| 73 | export const make = <RD = never>({ |
| 74 | dumpSchema = () => Effect.void |
| 75 | }: { |
| 76 | dumpSchema?: ( |
| 77 | path: string, |
| 78 | migrationsTable: string |
| 79 | ) => Effect.Effect<void, MigrationError, RD> |
| 80 | }) => |
| 81 | <R2 = never>({ |
| 82 | loader, |
| 83 | schemaDirectory, |
| 84 | table = "effect_sql_migrations" |
| 85 | }: MigratorOptions<R2>): Effect.Effect< |
| 86 | ReadonlyArray<readonly [id: number, name: string]>, |
| 87 | MigrationError | SqlError, |
| 88 | Client.SqlClient | RD | R2 |
| 89 | > => |
| 90 | Effect.gen(function*() { |
| 91 | const sql = yield* Client.SqlClient |
| 92 | const ensureMigrationsTable = sql.onDialectOrElse({ |
| 93 | mssql: () => |
| 94 | sql`IF OBJECT_ID(N'${sql.literal(table)}', N'U') IS NULL |
| 95 | CREATE TABLE ${sql(table)} ( |
| 96 | migration_id INT NOT NULL PRIMARY KEY, |
| 97 | name VARCHAR(255) NOT NULL, |
| 98 | created_at DATETIME NOT NULL DEFAULT GETDATE() |
| 99 | )`, |
| 100 | mysql: () => |
| 101 | sql`CREATE TABLE IF NOT EXISTS ${sql(table)} ( |
| 102 | migration_id INTEGER UNSIGNED NOT NULL, |
| 103 | created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 104 | name VARCHAR(255) NOT NULL, |
| 105 | PRIMARY KEY (migration_id) |
| 106 | )`, |
| 107 | pg: () => |
| 108 | Effect.catchAll( |
| 109 | sql`select ${table}::regclass`, |
| 110 | () => |
| 111 | sql`CREATE TABLE ${sql(table)} ( |
| 112 | migration_id integer primary key, |
| 113 | created_at timestamp with time zone not null default now(), |
| 114 | name text not null |
| 115 | )` |
| 116 | ), |
| 117 | orElse: () => |
| 118 | sql`CREATE TABLE IF NOT EXISTS ${sql(table)} ( |
| 119 | migration_id integer PRIMARY KEY NOT NULL, |
| 120 | created_at datetime NOT NULL DEFAULT current_timestamp, |
| 121 | name VARCHAR(255) NOT NULL |
| 122 | )` |
| 123 | }) |
| 124 | |
| 125 | const insertMigrations = ( |
| 126 | rows: ReadonlyArray<[id: number, name: string]> |
| 127 | ) => |
| 128 | sql`INSERT INTO ${sql(table)} ${ |
| 129 | sql.insert( |
| 130 | rows.map(([migration_id, name]) => ({ migration_id, name })) |
no test coverage detected