rake-db 2.29.9 → 2.30.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.mjs CHANGED
@@ -181,7 +181,7 @@ const promptText = ({
181
181
  const getMaybeTransactionAdapter = (db) => "$getAdapter" in db ? db.$getAdapter() : db;
182
182
  const ensureTransaction = (db, fn) => {
183
183
  const adapter = getMaybeTransactionAdapter(db);
184
- return adapter.isInTransaction() ? fn(adapter) : adapter.transaction(void 0, fn);
184
+ return adapter.isInTransaction() ? fn(adapter) : adapter.transaction(fn);
185
185
  };
186
186
  const runSqlInSavePoint = async (db, sql, code) => {
187
187
  const adapter = getMaybeTransactionAdapter(db);
@@ -281,19 +281,16 @@ const quoteWithSchema = ({
281
281
  name
282
282
  }) => quoteTable(schema, name);
283
283
  const quoteTable = (schema, table) => schema ? `"${schema}"."${table}"` : `"${table}"`;
284
- const getSchemaAndTableFromName = (config, name) => {
284
+ const getSchemaAndTableFromName = (schema, name) => {
285
285
  const i = name.indexOf(".");
286
- return i !== -1 ? [name.slice(0, i), name.slice(i + 1)] : [
287
- typeof config.schema === "function" ? config.schema() : config.schema,
288
- name
289
- ];
286
+ return i !== -1 ? [name.slice(0, i), name.slice(i + 1)] : [typeof schema === "function" ? schema() : schema, name];
290
287
  };
291
- const quoteNameFromString = (config, string) => {
292
- return quoteTable(...getSchemaAndTableFromName(config, string));
288
+ const quoteNameFromString = (schema, string) => {
289
+ return quoteTable(...getSchemaAndTableFromName(schema, string));
293
290
  };
294
- const quoteCustomType = (config, s) => {
295
- const [schema, type] = getSchemaAndTableFromName(config, s);
296
- return schema ? '"' + schema + '".' + type : type;
291
+ const quoteCustomType = (schema, type) => {
292
+ const [s, t] = getSchemaAndTableFromName(schema, type);
293
+ return s ? '"' + s + '".' + t : t;
297
294
  };
298
295
  const quoteSchemaTable = (arg, excludeCurrentSchema) => {
299
296
  return singleQuote(concatSchemaAndName(arg, excludeCurrentSchema));
@@ -304,17 +301,23 @@ const concatSchemaAndName = ({
304
301
  }, excludeCurrentSchema) => {
305
302
  return schema && schema !== excludeCurrentSchema ? `${schema}.${name}` : name;
306
303
  };
307
- const makePopulateEnumQuery = (config, item) => {
308
- const [schema, name] = getSchemaAndTableFromName(config, item.enumName);
304
+ const makePopulateEnumQuery = (schema, item) => {
305
+ const [s, name] = getSchemaAndTableFromName(schema, item.enumName);
309
306
  return {
310
- text: `SELECT unnest(enum_range(NULL::${quoteTable(schema, name)}))::text`,
307
+ text: `SELECT unnest(enum_range(NULL::${quoteTable(s, name)}))::text`,
311
308
  then(result) {
312
309
  item.options.push(...result.rows.map(([value]) => value));
313
310
  }
314
311
  };
315
312
  };
316
- const transaction = (adapter, fn) => {
317
- return adapter.transaction(void 0, fn);
313
+ const transaction = (adapter, config, fn) => {
314
+ const searchPath = config.transactionSearchPath;
315
+ return adapter.transaction(
316
+ fn,
317
+ searchPath ? {
318
+ searchPath: typeof searchPath === "function" ? searchPath() : searchPath
319
+ } : void 0
320
+ );
318
321
  };
319
322
  const queryLock = (trx) => trx.query(`SELECT pg_advisory_xact_lock('${RAKE_DB_LOCK_KEY}')`);
320
323
  const getCliParam = (args, name) => {
@@ -342,22 +345,22 @@ const getCurrentChanges = () => currentChanges;
342
345
  const pushChange = (change) => currentChanges.push(change);
343
346
 
344
347
  const versionToString = (config, version) => config.migrationId === "timestamp" ? `${version}` : `${version}`.padStart(config.migrationId.serial, "0");
345
- const columnTypeToSql = (config, item) => {
346
- return item.data.isOfCustomType ? item instanceof DomainColumn ? quoteNameFromString(config, item.dataType) : quoteCustomType(config, item.toSQL()) : item.toSQL();
348
+ const columnTypeToSql = (schema, item) => {
349
+ return item.data.isOfCustomType ? item instanceof DomainColumn ? quoteNameFromString(schema, item.dataType) : quoteCustomType(schema, item.toSQL()) : item.toSQL();
347
350
  };
348
351
  const getColumnName = (item, key, snakeCase) => {
349
352
  return item.data.name || (snakeCase ? toSnakeCase(key) : key);
350
353
  };
351
- const columnToSql = (config, name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
352
- const line = [`"${name}" ${columnTypeToSql(config, item)}`];
354
+ const columnToSql = (schema, name, item, values, hasMultiplePrimaryKeys, snakeCase) => {
355
+ const line = [`"${name}" ${columnTypeToSql(schema, item)}`];
353
356
  if (item.data.compression) {
354
357
  line.push(`COMPRESSION ${item.data.compression}`);
355
358
  }
356
359
  if (item.data.collate) {
357
- line.push(`COLLATE ${quoteNameFromString(config, item.data.collate)}`);
360
+ line.push(`COLLATE ${quoteNameFromString(schema, item.data.collate)}`);
358
361
  }
359
362
  if (item.data.identity) {
360
- line.push(identityToSql(config, item.data.identity));
363
+ line.push(identityToSql(schema, item.data.identity));
361
364
  } else if (item.data.generated) {
362
365
  line.push(
363
366
  `GENERATED ALWAYS AS (${item.data.generated.toSQL({
@@ -391,7 +394,7 @@ const columnToSql = (config, name, item, values, hasMultiplePrimaryKeys, snakeCa
391
394
  }
392
395
  line.push(
393
396
  referencesToSql(
394
- config,
397
+ schema,
395
398
  {
396
399
  columns: [name],
397
400
  ...foreignKey
@@ -415,11 +418,11 @@ const encodeColumnDefault = (def, values, column) => {
415
418
  }
416
419
  return null;
417
420
  };
418
- const identityToSql = (config, identity) => {
419
- const options = sequenceOptionsToSql(config, identity);
421
+ const identityToSql = (schema, identity) => {
422
+ const options = sequenceOptionsToSql(schema, identity);
420
423
  return `GENERATED ${identity.always ? "ALWAYS" : "BY DEFAULT"} AS IDENTITY${options ? ` (${options})` : ""}`;
421
424
  };
422
- const sequenceOptionsToSql = (config, item) => {
425
+ const sequenceOptionsToSql = (schema, item) => {
423
426
  const line = [];
424
427
  if (item.dataType) line.push(`AS ${item.dataType}`);
425
428
  if (item.increment !== void 0) line.push(`INCREMENT BY ${item.increment}`);
@@ -429,8 +432,8 @@ const sequenceOptionsToSql = (config, item) => {
429
432
  if (item.cache !== void 0) line.push(`CACHE ${item.cache}`);
430
433
  if (item.cycle) line.push(`CYCLE`);
431
434
  if (item.ownedBy) {
432
- const [schema, table] = getSchemaAndTableFromName(config, item.ownedBy);
433
- line.push(`OWNED BY ${quoteTable(schema, table)}`);
435
+ const [s, table] = getSchemaAndTableFromName(schema, item.ownedBy);
436
+ line.push(`OWNED BY ${quoteTable(s, table)}`);
434
437
  }
435
438
  return line.join(" ");
436
439
  };
@@ -459,9 +462,9 @@ const addColumnComment = (comments, name, item) => {
459
462
  comments.push({ column: name, comment: item.data.comment });
460
463
  }
461
464
  };
462
- const getForeignKeyTable = (config, fnOrTable) => {
465
+ const getForeignKeyTable = (schema, fnOrTable) => {
463
466
  if (typeof fnOrTable === "string") {
464
- return getSchemaAndTableFromName(config, fnOrTable);
467
+ return getSchemaAndTableFromName(schema, fnOrTable);
465
468
  }
466
469
  const item = new (fnOrTable())();
467
470
  return [item.schema, item.table];
@@ -478,7 +481,7 @@ const getConstraintName = (table, constraint, snakeCase) => {
478
481
  if (constraint.identity) return `${table}_identity`;
479
482
  return `${table}_constraint`;
480
483
  };
481
- const constraintToSql = (config, { name }, up, constraint, values, snakeCase) => {
484
+ const constraintToSql = (schema, { name }, up, constraint, values, snakeCase) => {
482
485
  const constraintName = constraint.name || getConstraintName(name, constraint, snakeCase);
483
486
  if (!up) {
484
487
  const { dropMode } = constraint;
@@ -486,7 +489,7 @@ const constraintToSql = (config, { name }, up, constraint, values, snakeCase) =>
486
489
  }
487
490
  const sql = [`CONSTRAINT "${constraintName}"`];
488
491
  if (constraint.references) {
489
- sql.push(foreignKeyToSql(config, constraint.references, snakeCase));
492
+ sql.push(foreignKeyToSql(schema, constraint.references, snakeCase));
490
493
  }
491
494
  if (constraint.check) {
492
495
  sql.push(checkToSql(constraint.check, values));
@@ -496,15 +499,15 @@ const constraintToSql = (config, { name }, up, constraint, values, snakeCase) =>
496
499
  const checkToSql = (check, values) => {
497
500
  return `CHECK (${check.toSQL({ values })})`;
498
501
  };
499
- const foreignKeyToSql = (config, item, snakeCase) => {
502
+ const foreignKeyToSql = (schema, item, snakeCase) => {
500
503
  return `FOREIGN KEY (${joinColumns(
501
504
  snakeCase ? item.columns.map(toSnakeCase) : item.columns
502
- )}) ${referencesToSql(config, item, snakeCase)}`;
505
+ )}) ${referencesToSql(schema, item, snakeCase)}`;
503
506
  };
504
- const referencesToSql = (config, references, snakeCase) => {
505
- const [schema, table] = getForeignKeyTable(config, references.fnOrTable);
507
+ const referencesToSql = (schema, references, snakeCase) => {
508
+ const [s, table] = getForeignKeyTable(schema, references.fnOrTable);
506
509
  const sql = [
507
- `REFERENCES ${quoteTable(schema, table)}(${joinColumns(
510
+ `REFERENCES ${quoteTable(s, table)}(${joinColumns(
508
511
  snakeCase ? references.foreignColumns.map(toSnakeCase) : references.foreignColumns
509
512
  )})`
510
513
  ];
@@ -553,7 +556,7 @@ const getIndexOrExcludeName = (table, columns, suffix) => makeConstraintName(
553
556
  );
554
557
  const getIndexName = (table, columns) => getIndexOrExcludeName(table, columns, "idx");
555
558
  const getExcludeName = (table, columns) => getIndexOrExcludeName(table, columns, "exclude");
556
- const indexesToQuery = (config, up, { schema, name: tableName }, indexes, snakeCase, language) => {
559
+ const indexesToQuery = (up, { schema, name: tableName }, indexes, snakeCase, language) => {
557
560
  return indexes.map((index) => {
558
561
  const { options } = index;
559
562
  const { columns, include, name } = getIndexOrExcludeMainOptions(
@@ -582,7 +585,7 @@ const indexesToQuery = (config, up, { schema, name: tableName }, indexes, snakeC
582
585
  const columnsSql = columns.map((column) => {
583
586
  let sql2 = [
584
587
  "expression" in column ? `(${column.expression})` : `"${column.column}"`,
585
- column.collate && `COLLATE ${quoteNameFromString(config, column.collate)}`,
588
+ column.collate && `COLLATE ${quoteNameFromString(schema, column.collate)}`,
586
589
  column.opclass,
587
590
  column.order
588
591
  ].filter((x) => !!x).join(" ");
@@ -626,7 +629,7 @@ const indexesToQuery = (config, up, { schema, name: tableName }, indexes, snakeC
626
629
  return { text: sql.join(" "), values };
627
630
  });
628
631
  };
629
- const excludesToQuery = (config, up, { schema, name: tableName }, excludes, snakeCase) => {
632
+ const excludesToQuery = (up, { schema, name: tableName }, excludes, snakeCase) => {
630
633
  return excludes.map((exclude) => {
631
634
  const { options } = exclude;
632
635
  const { columns, include, name } = getIndexOrExcludeMainOptions(
@@ -646,7 +649,7 @@ const excludesToQuery = (config, up, { schema, name: tableName }, excludes, snak
646
649
  const columnList = columns.map(
647
650
  (column) => [
648
651
  "expression" in column ? `(${column.expression})` : `"${column.column}"`,
649
- column.collate && `COLLATE ${quoteNameFromString(config, column.collate)}`,
652
+ column.collate && `COLLATE ${quoteNameFromString(schema, column.collate)}`,
650
653
  column.opclass,
651
654
  column.order,
652
655
  `WITH ${column.with}`
@@ -713,22 +716,23 @@ const cmpRawSql = (a, b) => {
713
716
  const bValues = JSON.stringify(values);
714
717
  return aSql === bSql && aValues === bValues;
715
718
  };
716
- const getMigrationsSchemaAndTable = (config) => {
719
+ const getMigrationsSchemaAndTable = (adapter, config) => {
720
+ const schema = adapter.getSchema();
717
721
  const [tableSchema, table] = getSchemaAndTableFromName(
718
- config,
722
+ schema,
719
723
  config.migrationsTable
720
724
  );
721
- let schema = tableSchema;
722
- if (!schema) {
723
- schema = typeof config.schema === "function" ? config.schema() : config.schema;
724
- if (schema === "public") {
725
- schema = void 0;
725
+ let s = tableSchema;
726
+ if (!s) {
727
+ s = typeof schema === "function" ? schema() : schema;
728
+ if (s === "public") {
729
+ s = void 0;
726
730
  }
727
731
  }
728
- return { schema, table };
732
+ return { schema: s, table };
729
733
  };
730
- const migrationsSchemaTableSql = (config) => {
731
- const { schema, table } = getMigrationsSchemaAndTable(config);
734
+ const migrationsSchemaTableSql = (adapter, config) => {
735
+ const { schema, table } = getMigrationsSchemaAndTable(adapter, config);
732
736
  return `${schema ? `"${schema}".` : ""}"${table}"`;
733
737
  };
734
738
 
@@ -785,8 +789,9 @@ const createTable = async (migration, up, tableName, first, second, third) => {
785
789
  } else {
786
790
  shape = tableData = emptyObject;
787
791
  }
792
+ const schema = migration.adapter.getSchema();
788
793
  const ast = makeAst$2(
789
- migration.options,
794
+ schema,
790
795
  up,
791
796
  tableName,
792
797
  shape,
@@ -795,7 +800,7 @@ const createTable = async (migration, up, tableName, first, second, third) => {
795
800
  migration.options.noPrimaryKey
796
801
  );
797
802
  fn && validatePrimaryKey(ast);
798
- const queries = astToQueries$1(migration.options, ast, snakeCase, language);
803
+ const queries = astToQueries$1(schema, ast, snakeCase, language);
799
804
  for (const { then, ...query } of queries) {
800
805
  const result = await migration.adapter.arrays(interpolateSqlValues(query));
801
806
  then?.(result);
@@ -815,7 +820,7 @@ const createTable = async (migration, up, tableName, first, second, third) => {
815
820
  }
816
821
  };
817
822
  };
818
- const makeAst$2 = (config, up, tableName, shape, tableData, options, noPrimaryKey) => {
823
+ const makeAst$2 = (schema, up, tableName, shape, tableData, options, noPrimaryKey) => {
819
824
  const shapePKeys = [];
820
825
  for (const key in shape) {
821
826
  const column = shape[key];
@@ -824,11 +829,11 @@ const makeAst$2 = (config, up, tableName, shape, tableData, options, noPrimaryKe
824
829
  }
825
830
  }
826
831
  const { primaryKey } = tableData;
827
- const [schema, table] = getSchemaAndTableFromName(config, tableName);
832
+ const [s, table] = getSchemaAndTableFromName(schema, tableName);
828
833
  return {
829
834
  type: "table",
830
835
  action: up ? "create" : "drop",
831
- schema,
836
+ schema: s,
832
837
  name: table,
833
838
  shape,
834
839
  ...tableData,
@@ -864,13 +869,13 @@ You can suppress this error by setting { noPrimaryKey: true } after a table name
864
869
  }
865
870
  }
866
871
  };
867
- const astToQueries$1 = (config, ast, snakeCase, language) => {
872
+ const astToQueries$1 = (schema, ast, snakeCase, language) => {
868
873
  const queries = [];
869
874
  const { shape } = ast;
870
875
  for (const key in shape) {
871
876
  const item = shape[key];
872
877
  if (!(item instanceof EnumColumn)) continue;
873
- queries.push(makePopulateEnumQuery(config, item));
878
+ queries.push(makePopulateEnumQuery(schema, item));
874
879
  }
875
880
  if (ast.action === "drop") {
876
881
  queries.push({
@@ -892,7 +897,7 @@ const astToQueries$1 = (config, ast, snakeCase, language) => {
892
897
  lines.push(
893
898
  `
894
899
  ${columnToSql(
895
- config,
900
+ schema,
896
901
  name,
897
902
  item,
898
903
  values,
@@ -916,7 +921,7 @@ const astToQueries$1 = (config, ast, snakeCase, language) => {
916
921
  lines.push(
917
922
  `
918
923
  ${constraintToSql(
919
- config,
924
+ schema,
920
925
  ast,
921
926
  true,
922
927
  {
@@ -941,8 +946,8 @@ const astToQueries$1 = (config, ast, snakeCase, language) => {
941
946
  )`,
942
947
  values
943
948
  },
944
- ...indexesToQuery(config, true, ast, indexes, snakeCase, language),
945
- ...excludesToQuery(config, true, ast, excludes, snakeCase),
949
+ ...indexesToQuery(true, ast, indexes, snakeCase, language),
950
+ ...excludesToQuery(true, ast, excludes, snakeCase),
946
951
  ...commentsToQuery(ast, comments)
947
952
  );
948
953
  if (ast.comment) {
@@ -1164,21 +1169,22 @@ const changeTable = async (migration, up, tableName, options, fn) => {
1164
1169
  tableChanger[snakeCaseKey] = snakeCase;
1165
1170
  addOrDropChanges.length = 0;
1166
1171
  const changeData = fn?.(tableChanger) || {};
1172
+ const schema = migration.adapter.getSchema();
1167
1173
  const ast = makeAst$1(
1168
- migration.options,
1174
+ schema,
1169
1175
  up,
1170
1176
  tableName,
1171
1177
  changeData,
1172
1178
  changeTableData,
1173
1179
  options
1174
1180
  );
1175
- const queries = astToQueries(migration.options, ast, snakeCase, language);
1181
+ const queries = astToQueries(schema, ast, snakeCase, language);
1176
1182
  for (const query of queries) {
1177
1183
  const result = await migration.adapter.arrays(interpolateSqlValues(query));
1178
1184
  query.then?.(result);
1179
1185
  }
1180
1186
  };
1181
- const makeAst$1 = (config, up, name, changeData, changeTableData2, options) => {
1187
+ const makeAst$1 = (schema, up, name, changeData, changeTableData2, options) => {
1182
1188
  const { comment } = options;
1183
1189
  const shape = {};
1184
1190
  const consumedChanges = {};
@@ -1223,17 +1229,17 @@ const makeAst$1 = (config, up, name, changeData, changeTableData2, options) => {
1223
1229
  );
1224
1230
  shape[name2] = arr;
1225
1231
  }
1226
- const [schema, table] = getSchemaAndTableFromName(config, name);
1232
+ const [s, table] = getSchemaAndTableFromName(schema, name);
1227
1233
  return {
1228
1234
  type: "changeTable",
1229
- schema,
1235
+ schema: s,
1230
1236
  name: table,
1231
1237
  comment: comment ? up ? Array.isArray(comment) ? comment[1] : comment : Array.isArray(comment) ? comment[0] : null : void 0,
1232
1238
  shape,
1233
1239
  ...up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add }
1234
1240
  };
1235
1241
  };
1236
- const astToQueries = (config, ast, snakeCase, language) => {
1242
+ const astToQueries = (schema, ast, snakeCase, language) => {
1237
1243
  const queries = [];
1238
1244
  if (ast.comment !== void 0) {
1239
1245
  queries.push({
@@ -1253,7 +1259,7 @@ const astToQueries = (config, ast, snakeCase, language) => {
1253
1259
  if (Array.isArray(item)) {
1254
1260
  for (const it of item) {
1255
1261
  handlePrerequisitesForTableItem(
1256
- config,
1262
+ schema,
1257
1263
  key,
1258
1264
  it,
1259
1265
  queries,
@@ -1264,7 +1270,7 @@ const astToQueries = (config, ast, snakeCase, language) => {
1264
1270
  }
1265
1271
  } else {
1266
1272
  handlePrerequisitesForTableItem(
1267
- config,
1273
+ schema,
1268
1274
  key,
1269
1275
  item,
1270
1276
  queries,
@@ -1303,7 +1309,7 @@ const astToQueries = (config, ast, snakeCase, language) => {
1303
1309
  if (Array.isArray(item)) {
1304
1310
  for (const it of item) {
1305
1311
  handleTableItemChange(
1306
- config,
1312
+ schema,
1307
1313
  key,
1308
1314
  it,
1309
1315
  ast,
@@ -1323,7 +1329,7 @@ const astToQueries = (config, ast, snakeCase, language) => {
1323
1329
  }
1324
1330
  } else {
1325
1331
  handleTableItemChange(
1326
- config,
1332
+ schema,
1327
1333
  key,
1328
1334
  item,
1329
1335
  ast,
@@ -1357,7 +1363,7 @@ const astToQueries = (config, ast, snakeCase, language) => {
1357
1363
  ...dropConstraints.map(
1358
1364
  (foreignKey) => `
1359
1365
  DROP ${constraintToSql(
1360
- config,
1366
+ schema,
1361
1367
  ast,
1362
1368
  false,
1363
1369
  foreignKey,
@@ -1382,7 +1388,7 @@ const astToQueries = (config, ast, snakeCase, language) => {
1382
1388
  ...addConstraints.map(
1383
1389
  (foreignKey) => `
1384
1390
  ADD ${constraintToSql(
1385
- config,
1391
+ schema,
1386
1392
  ast,
1387
1393
  true,
1388
1394
  foreignKey,
@@ -1404,14 +1410,10 @@ const astToQueries = (config, ast, snakeCase, language) => {
1404
1410
  if (alterTable.length) {
1405
1411
  queries.push(alterTableSql(tableName, alterTable, values));
1406
1412
  }
1407
- queries.push(
1408
- ...indexesToQuery(config, false, ast, dropIndexes, snakeCase, language)
1409
- );
1410
- queries.push(
1411
- ...indexesToQuery(config, true, ast, addIndexes, snakeCase, language)
1412
- );
1413
- queries.push(...excludesToQuery(config, false, ast, dropExcludes, snakeCase));
1414
- queries.push(...excludesToQuery(config, true, ast, addExcludes, snakeCase));
1413
+ queries.push(...indexesToQuery(false, ast, dropIndexes, snakeCase, language));
1414
+ queries.push(...indexesToQuery(true, ast, addIndexes, snakeCase, language));
1415
+ queries.push(...excludesToQuery(false, ast, dropExcludes, snakeCase));
1416
+ queries.push(...excludesToQuery(true, ast, addExcludes, snakeCase));
1415
1417
  queries.push(...commentsToQuery(ast, comments));
1416
1418
  return queries;
1417
1419
  };
@@ -1420,11 +1422,11 @@ const alterTableSql = (tableName, lines, values) => ({
1420
1422
  ${lines.join(",\n ")}`,
1421
1423
  values
1422
1424
  });
1423
- const handlePrerequisitesForTableItem = (config, key, item, queries, addPrimaryKeys, dropPrimaryKeys, snakeCase) => {
1425
+ const handlePrerequisitesForTableItem = (schema, key, item, queries, addPrimaryKeys, dropPrimaryKeys, snakeCase) => {
1424
1426
  if ("item" in item) {
1425
1427
  const { item: column } = item;
1426
1428
  if (column instanceof EnumColumn) {
1427
- queries.push(makePopulateEnumQuery(config, column));
1429
+ queries.push(makePopulateEnumQuery(schema, column));
1428
1430
  }
1429
1431
  }
1430
1432
  if (item.type === "add") {
@@ -1437,10 +1439,10 @@ const handlePrerequisitesForTableItem = (config, key, item, queries, addPrimaryK
1437
1439
  }
1438
1440
  } else if (item.type === "change") {
1439
1441
  if (item.from.column instanceof EnumColumn) {
1440
- queries.push(makePopulateEnumQuery(config, item.from.column));
1442
+ queries.push(makePopulateEnumQuery(schema, item.from.column));
1441
1443
  }
1442
1444
  if (item.to.column instanceof EnumColumn) {
1443
- queries.push(makePopulateEnumQuery(config, item.to.column));
1445
+ queries.push(makePopulateEnumQuery(schema, item.to.column));
1444
1446
  }
1445
1447
  if (item.from.primaryKey) {
1446
1448
  dropPrimaryKeys.columns.push(
@@ -1456,7 +1458,7 @@ const handlePrerequisitesForTableItem = (config, key, item, queries, addPrimaryK
1456
1458
  }
1457
1459
  }
1458
1460
  };
1459
- const handleTableItemChange = (config, key, item, ast, alterTable, renameItems, values, addPrimaryKeys, addIndexes, dropIndexes, addExcludes, dropExcludes, addConstraints, dropConstraints, comments, snakeCase) => {
1461
+ const handleTableItemChange = (schema, key, item, ast, alterTable, renameItems, values, addPrimaryKeys, addIndexes, dropIndexes, addExcludes, dropExcludes, addConstraints, dropConstraints, comments, snakeCase) => {
1460
1462
  if (item.type === "add") {
1461
1463
  const column = item.item;
1462
1464
  const name = getColumnName(column, key, snakeCase);
@@ -1465,7 +1467,7 @@ const handleTableItemChange = (config, key, item, ast, alterTable, renameItems,
1465
1467
  addColumnComment(comments, name, column);
1466
1468
  alterTable.push(
1467
1469
  `ADD COLUMN ${columnToSql(
1468
- config,
1470
+ schema,
1469
1471
  name,
1470
1472
  column,
1471
1473
  values,
@@ -1488,10 +1490,10 @@ const handleTableItemChange = (config, key, item, ast, alterTable, renameItems,
1488
1490
  let changeType = false;
1489
1491
  if (to.type && (from.type !== to.type || from.collate !== to.collate)) {
1490
1492
  changeType = true;
1491
- const type = !to.column || to.column.data.isOfCustomType ? to.column && to.column instanceof DomainColumn ? quoteNameFromString(config, to.type) : quoteCustomType(config, to.type) : to.type;
1493
+ const type = !to.column || to.column.data.isOfCustomType ? to.column && to.column instanceof DomainColumn ? quoteNameFromString(schema, to.type) : quoteCustomType(schema, to.type) : to.type;
1492
1494
  const using = item.using?.usingUp ? ` USING ${item.using.usingUp.toSQL({ values })}` : to.column instanceof EnumColumn ? ` USING "${name}"::text::${type}` : to.column instanceof ArrayColumn ? ` USING "${name}"::text[]::${type}` : "";
1493
1495
  alterTable.push(
1494
- `ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quoteNameFromString(config, to.collate)}` : ""}${using}`
1496
+ `ALTER COLUMN "${name}" TYPE ${type}${to.collate ? ` COLLATE ${quoteNameFromString(schema, to.collate)}` : ""}${using}`
1495
1497
  );
1496
1498
  }
1497
1499
  if (typeof from.identity !== typeof to.identity || !deepCompare(from.identity, to.identity)) {
@@ -1500,7 +1502,7 @@ const handleTableItemChange = (config, key, item, ast, alterTable, renameItems,
1500
1502
  }
1501
1503
  if (to.identity) {
1502
1504
  alterTable.push(
1503
- `ALTER COLUMN "${name}" ADD ${identityToSql(config, to.identity)}`
1505
+ `ALTER COLUMN "${name}" ADD ${identityToSql(schema, to.identity)}`
1504
1506
  );
1505
1507
  }
1506
1508
  }
@@ -1633,19 +1635,20 @@ const renameColumnSql = (from, to) => {
1633
1635
  };
1634
1636
 
1635
1637
  const createView = async (migration, up, name, options, sql) => {
1636
- const ast = makeAst(migration.options, up, name, options, sql);
1638
+ const schema = migration.adapter.getSchema();
1639
+ const ast = makeAst(schema, up, name, options, sql);
1637
1640
  const query = astToQuery(ast);
1638
1641
  await migration.adapter.arrays(interpolateSqlValues(query));
1639
1642
  };
1640
- const makeAst = (config, up, fullName, options, sql) => {
1643
+ const makeAst = (schema, up, fullName, options, sql) => {
1641
1644
  if (typeof sql === "string") {
1642
1645
  sql = raw({ raw: sql });
1643
1646
  }
1644
- const [schema, name] = getSchemaAndTableFromName(config, fullName);
1647
+ const [s, name] = getSchemaAndTableFromName(schema, fullName);
1645
1648
  return {
1646
1649
  type: "view",
1647
1650
  action: up ? "create" : "drop",
1648
- schema,
1651
+ schema: s,
1649
1652
  name,
1650
1653
  shape: {},
1651
1654
  sql,
@@ -2233,7 +2236,10 @@ class Migration {
2233
2236
  * @param values - object where keys are for old names, values are for new names
2234
2237
  */
2235
2238
  async renameEnumValues(enumName, values) {
2236
- const [schema, name] = getSchemaAndTableFromName(this.options, enumName);
2239
+ const [schema, name] = getSchemaAndTableFromName(
2240
+ this.adapter.getSchema(),
2241
+ enumName
2242
+ );
2237
2243
  const ast = {
2238
2244
  type: "renameEnumValues",
2239
2245
  schema,
@@ -2498,7 +2504,10 @@ class Migration {
2498
2504
  */
2499
2505
  async tableExists(tableName) {
2500
2506
  let text = `SELECT 1 FROM "information_schema"."tables" WHERE "table_name" = $1`;
2501
- const [schema, table] = getSchemaAndTableFromName(this.options, tableName);
2507
+ const [schema, table] = getSchemaAndTableFromName(
2508
+ this.adapter.getSchema(),
2509
+ tableName
2510
+ );
2502
2511
  const values = [table];
2503
2512
  if (schema) {
2504
2513
  text += ' AND "table_schema" = $2';
@@ -2529,7 +2538,10 @@ class Migration {
2529
2538
  */
2530
2539
  async columnExists(tableName, columnName) {
2531
2540
  let text = `SELECT 1 FROM "information_schema"."columns" WHERE "table_name" = $1 AND "column_name" = $2`;
2532
- const [schema, table] = getSchemaAndTableFromName(this.options, tableName);
2541
+ const [schema, table] = getSchemaAndTableFromName(
2542
+ this.adapter.getSchema(),
2543
+ tableName
2544
+ );
2533
2545
  const values = [
2534
2546
  table,
2535
2547
  this.options.snakeCase ? toSnakeCase(columnName) : columnName
@@ -2620,7 +2632,10 @@ const createSchema = async (migration, up, name) => {
2620
2632
  );
2621
2633
  };
2622
2634
  const createExtension = async (migration, up, fullName, options) => {
2623
- const [schema, name] = getSchemaAndTableFromName(migration.options, fullName);
2635
+ const [schema, name] = getSchemaAndTableFromName(
2636
+ migration.adapter.getSchema(),
2637
+ fullName
2638
+ );
2624
2639
  const ast = {
2625
2640
  type: "extension",
2626
2641
  action: up ? "create" : "drop",
@@ -2637,7 +2652,10 @@ const createExtension = async (migration, up, fullName, options) => {
2637
2652
  await migration.adapter.query(query);
2638
2653
  };
2639
2654
  const createEnum = async (migration, up, name, values, options = {}) => {
2640
- const [schema, enumName] = getSchemaAndTableFromName(migration.options, name);
2655
+ const [schema, enumName] = getSchemaAndTableFromName(
2656
+ migration.adapter.getSchema(),
2657
+ name
2658
+ );
2641
2659
  const ast = {
2642
2660
  type: "enum",
2643
2661
  action: up ? "create" : "drop",
@@ -2657,7 +2675,7 @@ const createEnum = async (migration, up, name, values, options = {}) => {
2657
2675
  };
2658
2676
  const createDomain = async (migration, up, name, fn) => {
2659
2677
  const [schema, domainName] = getSchemaAndTableFromName(
2660
- migration.options,
2678
+ migration.adapter.getSchema(),
2661
2679
  name
2662
2680
  );
2663
2681
  const ast = {
@@ -2672,10 +2690,7 @@ const createDomain = async (migration, up, name, fn) => {
2672
2690
  const quotedName = quoteWithSchema(ast);
2673
2691
  if (ast.action === "create") {
2674
2692
  const column = ast.baseType;
2675
- query = `CREATE DOMAIN ${quotedName} AS ${columnTypeToSql(
2676
- migration.options,
2677
- column
2678
- )}${column.data.collate ? `
2693
+ query = `CREATE DOMAIN ${quotedName} AS ${columnTypeToSql(schema, column)}${column.data.collate ? `
2679
2694
  COLLATE "${column.data.collate}"` : ""}${column.data.default !== void 0 ? `
2680
2695
  DEFAULT ${encodeColumnDefault(column.data.default, values)}` : ""}${!column.data.isNullable || column.data.checks ? "\n" : ""}${[
2681
2696
  !column.data.isNullable && "NOT NULL",
@@ -2693,7 +2708,7 @@ DEFAULT ${encodeColumnDefault(column.data.default, values)}` : ""}${!column.data
2693
2708
  };
2694
2709
  const createCollation = async (migration, up, name, options) => {
2695
2710
  const [schema, collationName] = getSchemaAndTableFromName(
2696
- migration.options,
2711
+ migration.adapter.getSchema(),
2697
2712
  name
2698
2713
  );
2699
2714
  const ast = {
@@ -2709,7 +2724,7 @@ const createCollation = async (migration, up, name, options) => {
2709
2724
  query = `CREATE COLLATION${ast.createIfNotExists ? " IF NOT EXISTS" : ""} ${quotedName} `;
2710
2725
  if (ast.fromExisting) {
2711
2726
  query += `FROM ${quoteNameFromString(
2712
- migration.options,
2727
+ migration.adapter.getSchema(),
2713
2728
  ast.fromExisting
2714
2729
  )}`;
2715
2730
  } else {
@@ -2735,11 +2750,11 @@ const queryExists = (db, sql) => {
2735
2750
  };
2736
2751
  const renameType = async (migration, from, to, kind) => {
2737
2752
  const [fromSchema, f] = getSchemaAndTableFromName(
2738
- migration.options,
2753
+ migration.adapter.getSchema(),
2739
2754
  migration.up ? from : to
2740
2755
  );
2741
2756
  const [toSchema, t] = getSchemaAndTableFromName(
2742
- migration.options,
2757
+ migration.adapter.getSchema(),
2743
2758
  migration.up ? to : from
2744
2759
  );
2745
2760
  const ast = {
@@ -2756,14 +2771,15 @@ const renameType = async (migration, from, to, kind) => {
2756
2771
  );
2757
2772
  }
2758
2773
  if (ast.fromSchema !== ast.toSchema) {
2774
+ const schema = migration.adapter.getSchema();
2759
2775
  await migration.adapter.query(
2760
- `ALTER ${ast.kind} ${quoteTable(ast.fromSchema, ast.to)} SET SCHEMA "${ast.toSchema ?? migration.options.schema ?? "public"}"`
2776
+ `ALTER ${ast.kind} ${quoteTable(ast.fromSchema, ast.to)} SET SCHEMA "${ast.toSchema ?? (typeof schema === "function" ? schema() : schema) ?? "public"}"`
2761
2777
  );
2762
2778
  }
2763
2779
  };
2764
2780
  const renameTableItem = async (migration, tableName, from, to, kind) => {
2765
2781
  const [schema, table] = getSchemaAndTableFromName(
2766
- migration.options,
2782
+ migration.adapter.getSchema(),
2767
2783
  tableName
2768
2784
  );
2769
2785
  const [f, t] = migration.up ? [from, to] : [to, from];
@@ -2775,7 +2791,10 @@ const renameTableItem = async (migration, tableName, from, to, kind) => {
2775
2791
  );
2776
2792
  };
2777
2793
  const addOrDropEnumValues = async (migration, up, enumName, values, options) => {
2778
- const [schema, name] = getSchemaAndTableFromName(migration.options, enumName);
2794
+ const [schema, name] = getSchemaAndTableFromName(
2795
+ migration.adapter.getSchema(),
2796
+ enumName
2797
+ );
2779
2798
  const quotedName = quoteTable(schema, name);
2780
2799
  const ast = {
2781
2800
  type: "enumValues",
@@ -2811,7 +2830,10 @@ const addOrDropEnumValues = async (migration, up, enumName, values, options) =>
2811
2830
  );
2812
2831
  };
2813
2832
  const changeEnumValues = async (migration, enumName, fromValues, toValues) => {
2814
- const [schema, name] = getSchemaAndTableFromName(migration.options, enumName);
2833
+ const [schema, name] = getSchemaAndTableFromName(
2834
+ migration.adapter.getSchema(),
2835
+ enumName
2836
+ );
2815
2837
  if (!migration.up) {
2816
2838
  const values = fromValues;
2817
2839
  fromValues = toValues;
@@ -2834,7 +2856,7 @@ const changeEnumValues = async (migration, enumName, fromValues, toValues) => {
2834
2856
  );
2835
2857
  };
2836
2858
  const recreateEnum = async (migration, { schema, name }, values, errorMessage) => {
2837
- const configSchema = migration.options.schema;
2859
+ const configSchema = migration.adapter.getSchema();
2838
2860
  const defaultSchema = (typeof configSchema === "function" ? configSchema() : void 0) ?? "public";
2839
2861
  const quotedName = quoteTable(schema, name);
2840
2862
  const relKinds = ["r", "m"];
@@ -3059,6 +3081,7 @@ ${format === "timestamp" || digits !== 4 ? `Set \`migrationId\`: ${format === "t
3059
3081
  const renameMigrationVersionsInDb = async (config, adapter, values) => {
3060
3082
  await adapter.arrays(
3061
3083
  `UPDATE ${migrationsSchemaTableSql(
3084
+ adapter,
3062
3085
  config
3063
3086
  )} AS t SET version = v.version FROM (VALUES ${values.map(
3064
3087
  ([oldVersion, , newVersion], i) => `('${oldVersion}', $${i + 1}, '${newVersion}')`
@@ -3238,13 +3261,15 @@ function getDigitsPrefix(name) {
3238
3261
  const saveMigratedVersion = async (db, version, name, config) => {
3239
3262
  await db.silentArrays(
3240
3263
  `INSERT INTO ${migrationsSchemaTableSql(
3264
+ db,
3241
3265
  config
3242
3266
  )}(version, name) VALUES ($1, $2)`,
3243
3267
  [version, name]
3244
3268
  );
3245
3269
  };
3246
3270
  const createMigrationsSchemaAndTable = async (db, config) => {
3247
- const { schema, table } = getMigrationsSchemaAndTable(config);
3271
+ const adapter = getMaybeTransactionAdapter(db);
3272
+ const { schema, table } = getMigrationsSchemaAndTable(adapter, config);
3248
3273
  if (schema) {
3249
3274
  const res2 = await createSchema$1(db, schema);
3250
3275
  if (res2 === "done") {
@@ -3264,6 +3289,7 @@ const createMigrationsSchemaAndTable = async (db, config) => {
3264
3289
  const deleteMigratedVersion = async (adapter, version, name, config) => {
3265
3290
  const res = await adapter.silentArrays(
3266
3291
  `DELETE FROM ${migrationsSchemaTableSql(
3292
+ adapter,
3267
3293
  config
3268
3294
  )} WHERE version = $1 AND name = $2`,
3269
3295
  [version, name]
@@ -3276,7 +3302,7 @@ class NoMigrationsTableError extends Error {
3276
3302
  }
3277
3303
  const getMigratedVersionsMap = async (ctx, adapter, config, renameTo) => {
3278
3304
  try {
3279
- const table = migrationsSchemaTableSql(config);
3305
+ const table = migrationsSchemaTableSql(adapter, config);
3280
3306
  const result = await adapter.arrays(
3281
3307
  `SELECT * FROM ${table} ORDER BY version`
3282
3308
  );
@@ -3353,7 +3379,7 @@ async function renameMigrations(config, trx, versions, renameTo) {
3353
3379
  }
3354
3380
 
3355
3381
  const transactionIfSingle = (adapter, config, fn) => {
3356
- return !adapter.isInTransaction() && config.transaction === "single" ? transaction(adapter, fn) : fn(adapter);
3382
+ return !adapter.isInTransaction() && config.transaction === "single" ? transaction(adapter, config, fn) : fn(adapter);
3357
3383
  };
3358
3384
  function makeMigrateFn(up, defaultCount, fn) {
3359
3385
  return async (db, config, params) => {
@@ -3570,8 +3596,12 @@ const getChanges = async (file, config) => {
3570
3596
  }
3571
3597
  return changes;
3572
3598
  };
3573
- const runMigrationInOwnTransaction = (adapter, ...rest) => {
3574
- return transaction(adapter, (trx) => applyMigration(trx, ...rest));
3599
+ const runMigrationInOwnTransaction = (adapter, up, changes, config) => {
3600
+ return transaction(
3601
+ adapter,
3602
+ config,
3603
+ (trx) => applyMigration(trx, up, changes, config)
3604
+ );
3575
3605
  };
3576
3606
  const applyMigration = async (trx, up, changes, config) => {
3577
3607
  const db = createMigrationInterface(trx, up, config);
@@ -3655,9 +3685,10 @@ const createOrDropDatabase = async (action, adapters, config, dontClose) => {
3655
3685
  );
3656
3686
  if (!res) continue;
3657
3687
  if (action === "create") {
3658
- await adapter.transaction(void 0, async (tx) => {
3659
- if (config.schema) {
3660
- const quoted = `"${config.schema}"`;
3688
+ await adapter.transaction(async (tx) => {
3689
+ const schema = tx.getSchema();
3690
+ if (schema) {
3691
+ const quoted = `"${typeof schema === "function" ? schema() : schema}"`;
3661
3692
  const res2 = await createSchema$1(tx, quoted);
3662
3693
  if (res2 === "done") {
3663
3694
  config.logger?.log(`Created schema ${quoted}`);
@@ -3769,7 +3800,7 @@ const astToGenerateItem = (config, ast, currentSchema) => {
3769
3800
  let dep = typeSchemaCache.get(type);
3770
3801
  if (!dep) {
3771
3802
  const [schema = currentSchema, name] = getSchemaAndTableFromName(
3772
- config,
3803
+ currentSchema,
3773
3804
  type
3774
3805
  );
3775
3806
  dep = `${schema}.${name}`;
@@ -3962,7 +3993,7 @@ const analyzeTableColumns = (config, currentSchema, schema, table, deps, resolve
3962
3993
  )
3963
3994
  );
3964
3995
  const [s = currentSchema, t] = getForeignKeyTable(
3965
- config,
3996
+ currentSchema,
3966
3997
  fkey.fnOrTable
3967
3998
  );
3968
3999
  const foreignTable = `${s}.${t}`;
@@ -4009,7 +4040,7 @@ const analyzeTableData = (config, currentSchema, schema, table, keys, deps, data
4009
4040
  );
4010
4041
  if (constraint.references) {
4011
4042
  const [s = currentSchema, t] = getForeignKeyTable(
4012
- config,
4043
+ currentSchema,
4013
4044
  constraint.references.fnOrTable
4014
4045
  );
4015
4046
  deps.push(`${s}.${t}`);
@@ -4146,7 +4177,7 @@ const astEncoders = {
4146
4177
  const isShifted = hasOptions || hasTableData;
4147
4178
  if (isShifted) {
4148
4179
  addCode(code, `await db.${ast.action}Table(`);
4149
- const inner = [`${quoteSchemaTable(ast)},`];
4180
+ const inner = [`${quoteSchemaTable(ast, currentSchema)},`];
4150
4181
  code.push(inner);
4151
4182
  code = inner;
4152
4183
  if (hasOptions) {
@@ -4160,7 +4191,10 @@ const astEncoders = {
4160
4191
  } else {
4161
4192
  addCode(
4162
4193
  code,
4163
- `await db.${ast.action}Table(${quoteSchemaTable(ast)}, (t) => ({`
4194
+ `await db.${ast.action}Table(${quoteSchemaTable(
4195
+ ast,
4196
+ currentSchema
4197
+ )}, (t) => ({`
4164
4198
  );
4165
4199
  }
4166
4200
  const timestamps = getHasTimestamps(
@@ -4371,9 +4405,9 @@ const astEncoders = {
4371
4405
  ast.to
4372
4406
  )});`;
4373
4407
  },
4374
- extension(ast) {
4408
+ extension(ast, _, currentSchema) {
4375
4409
  const code = [
4376
- `await db.${ast.action}Extension(${quoteSchemaTable(ast)}`
4410
+ `await db.${ast.action}Extension(${quoteSchemaTable(ast, currentSchema)}`
4377
4411
  ];
4378
4412
  if (ast.version) {
4379
4413
  addCode(code, ", {");
@@ -4407,13 +4441,14 @@ const astEncoders = {
4407
4441
  },
4408
4442
  domain(ast, _, currentSchema) {
4409
4443
  return `await db.${ast.action}Domain(${quoteSchemaTable(
4410
- ast
4444
+ ast,
4445
+ currentSchema
4411
4446
  )}, (t) => ${ast.baseType.toCode(
4412
4447
  { t: "t", table: ast.name, currentSchema },
4413
4448
  ast.baseType.data.name ?? ""
4414
4449
  )});`;
4415
4450
  },
4416
- collation(ast) {
4451
+ collation(ast, _, currentSchema) {
4417
4452
  const params = [];
4418
4453
  if (ast.locale) params.push(`locale: '${ast.locale}',`);
4419
4454
  if (ast.lcCollate) params.push(`lcCollate: '${ast.lcCollate}',`);
@@ -4422,7 +4457,7 @@ const astEncoders = {
4422
4457
  if (ast.deterministic) params.push(`deterministic: ${ast.deterministic},`);
4423
4458
  if (ast.version) params.push(`version: '${ast.version}',`);
4424
4459
  return [
4425
- `await db.createCollation(${quoteSchemaTable(ast)}, {`,
4460
+ `await db.createCollation(${quoteSchemaTable(ast, currentSchema)}, {`,
4426
4461
  params,
4427
4462
  "});"
4428
4463
  ];
@@ -4452,8 +4487,10 @@ const astEncoders = {
4452
4487
  })}, ${singleQuote(ast.from)}, ${singleQuote(ast.to)});`
4453
4488
  ];
4454
4489
  },
4455
- view(ast) {
4456
- const code = [`await db.createView(${quoteSchemaTable(ast)}`];
4490
+ view(ast, _, currentSchema) {
4491
+ const code = [
4492
+ `await db.createView(${quoteSchemaTable(ast, currentSchema)}`
4493
+ ];
4457
4494
  const options = [];
4458
4495
  if (ast.options.recursive) options.push("recursive: true,");
4459
4496
  const w = ast.options.with;
@@ -5044,7 +5081,7 @@ const structureToAst = async (ctx, adapter, config) => {
5044
5081
  });
5045
5082
  }
5046
5083
  const domains = makeDomainsMap(ctx, data);
5047
- const { schema: migrationsSchema = "public", table: migrationsTable } = getMigrationsSchemaAndTable(config);
5084
+ const { schema: migrationsSchema = "public", table: migrationsTable } = getMigrationsSchemaAndTable(adapter, config);
5048
5085
  for (const table of data.tables) {
5049
5086
  if (table.name === migrationsTable && table.schemaName === migrationsSchema)
5050
5087
  continue;