({
dumpSchema = () => Effect.void
}: {
dumpSchema?: (
path: string,
migrationsTable: string
) => Effect.Effect<void, MigrationError, RD>
})
| 98 | * @since 4.0.0 |
| 99 | */ |
| 100 | export const make = <RD = never>({ |
| 101 | dumpSchema = () => Effect.void |
| 102 | }: { |
| 103 | dumpSchema?: ( |
| 104 | path: string, |
| 105 | migrationsTable: string |
| 106 | ) => Effect.Effect<void, MigrationError, RD> |
| 107 | }) => |
| 108 | <R2 = never>({ |
| 109 | loader, |
| 110 | schemaDirectory, |
| 111 | table = "effect_sql_migrations" |
| 112 | }: MigratorOptions<R2>): Effect.Effect< |
| 113 | ReadonlyArray<readonly [id: number, name: string]>, |
| 114 | MigrationError | SqlError, |
| 115 | Client.SqlClient | RD | R2 |
| 116 | > => |
| 117 | Effect.gen(function*() { |
| 118 | const sql = yield* Client.SqlClient |
| 119 | |
| 120 | const ensureMigrationsTable = sql.onDialectOrElse({ |
| 121 | mssql: () => |
| 122 | sql`IF OBJECT_ID(N'${sql.literal(table)}', N'U') IS NULL |
| 123 | CREATE TABLE ${sql(table)} ( |
| 124 | migration_id INT NOT NULL PRIMARY KEY, |
| 125 | name VARCHAR(255) NOT NULL, |
| 126 | created_at DATETIME NOT NULL DEFAULT GETDATE() |
| 127 | )`, |
| 128 | mysql: () => |
| 129 | sql`CREATE TABLE IF NOT EXISTS ${sql(table)} ( |
| 130 | migration_id INTEGER UNSIGNED NOT NULL, |
| 131 | created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, |
| 132 | name VARCHAR(255) NOT NULL, |
| 133 | PRIMARY KEY (migration_id) |
| 134 | )`, |
| 135 | pg: () => |
| 136 | Effect.catch( |
| 137 | sql`select ${table}::regclass`, |
| 138 | () => |
| 139 | sql`CREATE TABLE ${sql(table)} ( |
| 140 | migration_id integer primary key, |
| 141 | created_at timestamp with time zone not null default now(), |
| 142 | name text not null |
| 143 | )` |
| 144 | ), |
| 145 | orElse: () => |
| 146 | sql`CREATE TABLE IF NOT EXISTS ${sql(table)} ( |
| 147 | migration_id integer PRIMARY KEY NOT NULL, |
| 148 | created_at datetime NOT NULL DEFAULT current_timestamp, |
| 149 | name VARCHAR(255) NOT NULL |
| 150 | )` |
| 151 | }) |
| 152 | |
| 153 | const insertMigrations = ( |
| 154 | rows: ReadonlyArray<[id: number, name: string]> |
| 155 | ) => |
| 156 | sql`INSERT INTO ${sql(table)} ${ |
| 157 | sql.insert( |
nothing calls this directly
no test coverage detected