rake-db 2.2.5 → 2.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,26 @@
1
1
  # rake-db
2
2
 
3
+ ## 2.3.0
4
+
5
+ ### Minor Changes
6
+
7
+ - Change index options: column or expression is required, operator renamed to opclass
8
+
9
+ ### Patch Changes
10
+
11
+ - f1cd5db: Handle multiple indexes and foreignKeys of the column
12
+ - Updated dependencies
13
+ - Updated dependencies [f1cd5db]
14
+ - pqb@0.9.0
15
+
16
+ ## 2.2.6
17
+
18
+ ### Patch Changes
19
+
20
+ - Change inner aspects of columns
21
+ - Updated dependencies
22
+ - pqb@0.8.5
23
+
3
24
  ## 2.2.5
4
25
 
5
26
  ### Patch Changes
package/db.ts CHANGED
@@ -29,5 +29,5 @@ rakeDb(options, {
29
29
  baseTableName: 'BaseTable',
30
30
  mainFilePath: 'app/db.ts',
31
31
  }),
32
- useCodeUpdaterByDefault: false,
32
+ useCodeUpdater: false,
33
33
  });
package/dist/index.d.ts CHANGED
@@ -113,6 +113,7 @@ declare namespace RakeDbAst {
113
113
  type Table = {
114
114
  type: 'table';
115
115
  action: 'create' | 'drop';
116
+ schema?: string;
116
117
  name: string;
117
118
  shape: ColumnsShape;
118
119
  noPrimaryKey: NoPrimaryKeyOption;
@@ -121,6 +122,7 @@ declare namespace RakeDbAst {
121
122
  } & TableData;
122
123
  type ChangeTable = {
123
124
  type: 'changeTable';
125
+ schema?: string;
124
126
  name: string;
125
127
  comment?: string | null;
126
128
  shape: Record<string, ChangeTableItem>;
@@ -154,15 +156,17 @@ declare namespace RakeDbAst {
154
156
  comment?: string | null;
155
157
  compression?: string;
156
158
  primaryKey?: boolean;
157
- foreignKey?: {
159
+ foreignKeys?: ({
158
160
  table: string;
159
161
  columns: string[];
160
- } & ForeignKeyOptions;
161
- index?: Omit<SingleColumnIndexOptions, 'column'>;
162
+ } & ForeignKeyOptions)[];
163
+ indexes?: Omit<SingleColumnIndexOptions, 'column' | 'expression'>[];
162
164
  };
163
165
  type RenameTable = {
164
166
  type: 'renameTable';
167
+ fromSchema?: string;
165
168
  from: string;
169
+ toSchema?: string;
166
170
  to: string;
167
171
  };
168
172
  type Schema = {
package/dist/index.esm.js CHANGED
@@ -95,9 +95,9 @@ const setAdminCredentialsToOptions = async (options) => {
95
95
  const createSchemaMigrations = async (db, config) => {
96
96
  try {
97
97
  await db.query(
98
- `CREATE TABLE ${quoteTable(
99
- config.migrationsTable
100
- )} ( version TEXT NOT NULL )`
98
+ `CREATE TABLE ${quoteWithSchema({
99
+ name: config.migrationsTable
100
+ })} ( version TEXT NOT NULL )`
101
101
  );
102
102
  console.log("Created versions table");
103
103
  } catch (err) {
@@ -173,13 +173,15 @@ const joinWords = (...words) => {
173
173
  const joinColumns = (columns) => {
174
174
  return columns.map((column) => `"${column}"`).join(", ");
175
175
  };
176
- const quoteTable = (table) => {
177
- const index = table.indexOf(".");
178
- if (index !== -1) {
179
- return `"${table.slice(0, index)}"."${table.slice(index + 1)}"`;
180
- } else {
181
- return `"${table}"`;
182
- }
176
+ const quoteWithSchema = ({
177
+ schema,
178
+ name
179
+ }) => {
180
+ return schema ? `"${schema}"."${name}"` : `"${name}"`;
181
+ };
182
+ const getSchemaAndTableFromName = (name) => {
183
+ const index = name.indexOf(".");
184
+ return index !== -1 ? [name.slice(0, index), name.slice(index + 1)] : [void 0, name];
183
185
  };
184
186
 
185
187
  let currentMigration;
@@ -230,7 +232,7 @@ const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
230
232
  }
231
233
  if (item.isPrimaryKey && !hasMultiplePrimaryKeys) {
232
234
  line.push("PRIMARY KEY");
233
- } else if (!item.isNullable) {
235
+ } else if (!item.data.isNullable) {
234
236
  line.push("NOT NULL");
235
237
  }
236
238
  if (item.data.default !== void 0) {
@@ -240,26 +242,28 @@ const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
240
242
  line.push(`DEFAULT ${quote(item.data.default)}`);
241
243
  }
242
244
  }
243
- const { foreignKey } = item.data;
244
- if (foreignKey) {
245
- const table = getForeignKeyTable(
246
- "fn" in foreignKey ? foreignKey.fn : foreignKey.table
247
- );
248
- if (foreignKey.name) {
249
- line.push(`CONSTRAINT "${foreignKey.name}"`);
245
+ const { foreignKeys } = item.data;
246
+ if (foreignKeys) {
247
+ for (const foreignKey of foreignKeys) {
248
+ const [schema, table] = getForeignKeyTable(
249
+ "fn" in foreignKey ? foreignKey.fn : foreignKey.table
250
+ );
251
+ if (foreignKey.name) {
252
+ line.push(`CONSTRAINT "${foreignKey.name}"`);
253
+ }
254
+ line.push(referencesToSql(schema, table, foreignKey.columns, foreignKey));
250
255
  }
251
- line.push(referencesToSql(table, foreignKey.columns, foreignKey));
252
256
  }
253
257
  return line.join(" ");
254
258
  };
255
259
  const addColumnIndex = (indexes, key, item) => {
256
- if (item.data) {
257
- if (item.data.index) {
258
- indexes.push({
259
- columns: [__spreadProps$4(__spreadValues$5({}, item.data.index), { column: key })],
260
- options: item.data.index
261
- });
262
- }
260
+ if (item.data.indexes) {
261
+ indexes.push(
262
+ ...item.data.indexes.map((index) => ({
263
+ columns: [__spreadProps$4(__spreadValues$5({}, index), { column: key })],
264
+ options: index
265
+ }))
266
+ );
263
267
  }
264
268
  };
265
269
  const addColumnComment = (comments, key, item) => {
@@ -269,25 +273,32 @@ const addColumnComment = (comments, key, item) => {
269
273
  };
270
274
  const getForeignKeyTable = (fnOrTable) => {
271
275
  if (typeof fnOrTable === "string") {
272
- return fnOrTable;
276
+ return getSchemaAndTableFromName(fnOrTable);
273
277
  }
274
- const klass = fnOrTable();
275
- return new klass().table;
278
+ const item = new (fnOrTable())();
279
+ return [item.schema, item.table];
276
280
  };
277
- const constraintToSql = (tableName, up, foreignKey) => {
278
- const constraintName = foreignKey.options.name || `${tableName}_${foreignKey.columns.join("_")}_fkey`;
281
+ const constraintToSql = ({ name }, up, foreignKey) => {
282
+ const constraintName = foreignKey.options.name || `${name}_${foreignKey.columns.join("_")}_fkey`;
279
283
  if (!up) {
280
284
  const { dropMode } = foreignKey.options;
281
285
  return `CONSTRAINT "${constraintName}"${dropMode ? ` ${dropMode}` : ""}`;
282
286
  }
283
- const table = getForeignKeyTable(foreignKey.fnOrTable);
287
+ const [schema, table] = getForeignKeyTable(foreignKey.fnOrTable);
284
288
  return `CONSTRAINT "${constraintName}" FOREIGN KEY (${joinColumns(
285
289
  foreignKey.columns
286
- )}) ${referencesToSql(table, foreignKey.foreignColumns, foreignKey.options)}`;
287
- };
288
- const referencesToSql = (table, columns, foreignKey) => {
290
+ )}) ${referencesToSql(
291
+ schema,
292
+ table,
293
+ foreignKey.foreignColumns,
294
+ foreignKey.options
295
+ )}`;
296
+ };
297
+ const referencesToSql = (schema, table, columns, foreignKey) => {
289
298
  const sql = [
290
- `REFERENCES ${quoteTable(table)}(${joinColumns(columns)})`
299
+ `REFERENCES ${quoteWithSchema({ schema, name: table })}(${joinColumns(
300
+ columns
301
+ )})`
291
302
  ];
292
303
  if (foreignKey.match) {
293
304
  sql.push(`MATCH ${foreignKey.match.toUpperCase()}`);
@@ -300,9 +311,13 @@ const referencesToSql = (table, columns, foreignKey) => {
300
311
  }
301
312
  return sql.join(" ");
302
313
  };
303
- const indexesToQuery = (up, tableName, indexes) => {
314
+ const indexesToQuery = (up, { schema, name }, indexes) => {
304
315
  return indexes.map(({ columns, options }) => {
305
- const indexName = options.name || joinWords(tableName, ...columns.map(({ column }) => column), "index");
316
+ const indexName = options.name || joinWords(
317
+ name,
318
+ ...columns.filter((it) => "column" in it).map((it) => it.column),
319
+ "index"
320
+ );
306
321
  if (!up) {
307
322
  return {
308
323
  text: `DROP INDEX "${indexName}"${options.dropMode ? ` ${options.dropMode}` : ""}`,
@@ -314,20 +329,20 @@ const indexesToQuery = (up, tableName, indexes) => {
314
329
  if (options.unique) {
315
330
  sql.push("UNIQUE");
316
331
  }
317
- sql.push(`INDEX "${indexName}" ON ${quoteTable(tableName)}`);
332
+ sql.push(`INDEX "${indexName}" ON ${quoteWithSchema({ schema, name })}`);
318
333
  if (options.using) {
319
334
  sql.push(`USING ${options.using}`);
320
335
  }
321
336
  const columnsSql = [];
322
337
  columns.forEach((column) => {
323
338
  const columnSql = [
324
- `"${column.column}"${column.expression ? `(${column.expression})` : ""}`
339
+ "column" in column ? `"${column.column}"` : `(${column.expression})`
325
340
  ];
326
341
  if (column.collate) {
327
342
  columnSql.push(`COLLATE '${column.collate}'`);
328
343
  }
329
- if (column.operator) {
330
- columnSql.push(column.operator);
344
+ if (column.opclass) {
345
+ columnSql.push(column.opclass);
331
346
  }
332
347
  if (column.order) {
333
348
  columnSql.push(column.order);
@@ -354,11 +369,11 @@ const indexesToQuery = (up, tableName, indexes) => {
354
369
  return { text: sql.join(" "), values };
355
370
  });
356
371
  };
357
- const commentsToQuery = (tableName, comments) => {
372
+ const commentsToQuery = (schemaTable, comments) => {
358
373
  return comments.map(({ column, comment }) => ({
359
- text: `COMMENT ON COLUMN ${quoteTable(tableName)}."${column}" IS ${quote(
360
- comment
361
- )}`,
374
+ text: `COMMENT ON COLUMN ${quoteWithSchema(
375
+ schemaTable
376
+ )}."${column}" IS ${quote(comment)}`,
362
377
  values: []
363
378
  }));
364
379
  };
@@ -440,10 +455,12 @@ const makeAst$1 = (up, tableName, shape, tableData, options, noPrimaryKey) => {
440
455
  }
441
456
  }
442
457
  const primaryKey = tableData.primaryKey;
458
+ const [schema, table] = getSchemaAndTableFromName(tableName);
443
459
  return __spreadProps$3(__spreadValues$4(__spreadProps$3(__spreadValues$4({
444
460
  type: "table",
445
461
  action: up ? "create" : "drop",
446
- name: tableName,
462
+ schema,
463
+ name: table,
447
464
  shape
448
465
  }, tableData), {
449
466
  primaryKey: shapePKeys.length <= 1 ? primaryKey : primaryKey ? __spreadProps$3(__spreadValues$4({}, primaryKey), { columns: [...shapePKeys, ...primaryKey.columns] }) : { columns: shapePKeys }
@@ -477,7 +494,7 @@ const astToQueries$1 = (ast) => {
477
494
  if (ast.action === "drop") {
478
495
  return [
479
496
  {
480
- text: `DROP TABLE ${quoteTable(ast.name)}${ast.dropMode ? ` ${ast.dropMode}` : ""}`,
497
+ text: `DROP TABLE ${quoteWithSchema(ast)}${ast.dropMode ? ` ${ast.dropMode}` : ""}`,
481
498
  values: []
482
499
  }
483
500
  ];
@@ -499,21 +516,21 @@ const astToQueries$1 = (ast) => {
499
516
  }
500
517
  ast.foreignKeys.forEach((foreignKey) => {
501
518
  lines.push(`
502
- ${constraintToSql(ast.name, true, foreignKey)}`);
519
+ ${constraintToSql(ast, true, foreignKey)}`);
503
520
  });
504
521
  indexes.push(...ast.indexes);
505
522
  const result = [
506
523
  {
507
- text: `CREATE TABLE ${quoteTable(ast.name)} (${lines.join(",")}
524
+ text: `CREATE TABLE ${quoteWithSchema(ast)} (${lines.join(",")}
508
525
  )`,
509
526
  values
510
527
  },
511
- ...indexesToQuery(true, ast.name, indexes),
512
- ...commentsToQuery(ast.name, comments)
528
+ ...indexesToQuery(true, ast, indexes),
529
+ ...commentsToQuery(ast, comments)
513
530
  ];
514
531
  if (ast.comment) {
515
532
  result.push({
516
- text: `COMMENT ON TABLE ${quoteTable(ast.name)} IS ${quote(ast.comment)}`,
533
+ text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${quote(ast.comment)}`,
517
534
  values: []
518
535
  });
519
536
  }
@@ -601,17 +618,17 @@ const drop = (item, options) => {
601
618
  };
602
619
  const columnTypeToColumnChange = (item) => {
603
620
  if (item instanceof ColumnType) {
604
- const foreignKey = item.data.foreignKey;
605
- if (foreignKey && "fn" in foreignKey) {
621
+ const foreignKeys = item.data.foreignKeys;
622
+ if (foreignKeys == null ? void 0 : foreignKeys.some((it) => "fn" in it)) {
606
623
  throw new Error("Callback in foreignKey is not allowed in migration");
607
624
  }
608
625
  return __spreadProps$2(__spreadValues$3({
609
626
  column: item,
610
627
  type: item.toSQL(),
611
- nullable: item.isNullable,
628
+ nullable: item.data.isNullable,
612
629
  primaryKey: item.isPrimaryKey
613
630
  }, item.data), {
614
- foreignKey
631
+ foreignKeys
615
632
  });
616
633
  }
617
634
  return item.to;
@@ -681,19 +698,21 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
681
698
  }
682
699
  }
683
700
  }
701
+ const [schema, table] = getSchemaAndTableFromName(name);
684
702
  return __spreadValues$3({
685
703
  type: "changeTable",
686
- name,
704
+ schema,
705
+ name: table,
687
706
  comment: comment ? up ? Array.isArray(comment) ? comment[1] : comment : Array.isArray(comment) ? comment[0] : null : void 0,
688
707
  shape
689
708
  }, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
690
709
  };
691
710
  const astToQueries = (ast) => {
692
- var _a;
711
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
693
712
  const result = [];
694
713
  if (ast.comment !== void 0) {
695
714
  result.push({
696
- text: `COMMENT ON TABLE ${quoteTable(ast.name)} IS ${quote(ast.comment)}`,
715
+ text: `COMMENT ON TABLE ${quoteWithSchema(ast)} IS ${quote(ast.comment)}`,
697
716
  values: []
698
717
  });
699
718
  }
@@ -771,48 +790,60 @@ const astToQueries = (ast) => {
771
790
  `ALTER COLUMN "${key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
772
791
  );
773
792
  }
774
- const fromFkey = from.foreignKey;
775
- const toFkey = to.foreignKey;
776
- if ((fromFkey || toFkey) && (!fromFkey || !toFkey || fromFkey.name !== toFkey.name || fromFkey.match !== toFkey.match || fromFkey.onUpdate !== toFkey.onUpdate || fromFkey.onDelete !== toFkey.onDelete || fromFkey.dropMode !== toFkey.dropMode || fromFkey.table !== toFkey.table || fromFkey.columns.join(",") !== toFkey.columns.join(","))) {
777
- if (fromFkey) {
778
- dropForeignKeys.push({
779
- columns: [key],
780
- fnOrTable: fromFkey.table,
781
- foreignColumns: fromFkey.columns,
782
- options: fromFkey
783
- });
784
- }
785
- if (toFkey) {
786
- addForeignKeys.push({
787
- columns: [key],
788
- fnOrTable: toFkey.table,
789
- foreignColumns: toFkey.columns,
790
- options: toFkey
791
- });
793
+ const foreignKeysLen = Math.max(
794
+ ((_a = from.foreignKeys) == null ? void 0 : _a.length) || 0,
795
+ ((_b = to.foreignKeys) == null ? void 0 : _b.length) || 0
796
+ );
797
+ for (let i = 0; i < foreignKeysLen; i++) {
798
+ const fromFkey = (_c = from.foreignKeys) == null ? void 0 : _c[i];
799
+ const toFkey = (_d = to.foreignKeys) == null ? void 0 : _d[i];
800
+ if ((fromFkey || toFkey) && (!fromFkey || !toFkey || fromFkey.name !== toFkey.name || fromFkey.match !== toFkey.match || fromFkey.onUpdate !== toFkey.onUpdate || fromFkey.onDelete !== toFkey.onDelete || fromFkey.dropMode !== toFkey.dropMode || fromFkey.table !== toFkey.table || fromFkey.columns.join(",") !== toFkey.columns.join(","))) {
801
+ if (fromFkey) {
802
+ dropForeignKeys.push({
803
+ columns: [key],
804
+ fnOrTable: fromFkey.table,
805
+ foreignColumns: fromFkey.columns,
806
+ options: fromFkey
807
+ });
808
+ }
809
+ if (toFkey) {
810
+ addForeignKeys.push({
811
+ columns: [key],
812
+ fnOrTable: toFkey.table,
813
+ foreignColumns: toFkey.columns,
814
+ options: toFkey
815
+ });
816
+ }
792
817
  }
793
818
  }
794
- const fromIndex = from.index;
795
- const toIndex = to.index;
796
- if ((fromIndex || toIndex) && (!fromIndex || !toIndex || fromIndex.expression !== toIndex.expression || fromIndex.collate !== toIndex.collate || fromIndex.operator !== toIndex.operator || fromIndex.order !== toIndex.order || fromIndex.name !== toIndex.name || fromIndex.unique !== toIndex.unique || fromIndex.using !== toIndex.using || fromIndex.include !== toIndex.include || Array.isArray(fromIndex.include) && Array.isArray(toIndex.include) && fromIndex.include.join(",") !== toIndex.include.join(",") || fromIndex.with !== toIndex.with || fromIndex.tablespace !== toIndex.tablespace || fromIndex.where !== toIndex.where || fromIndex.dropMode !== toIndex.dropMode)) {
797
- if (fromIndex) {
798
- dropIndexes.push({
799
- columns: [
800
- __spreadValues$3({
801
- column: key
802
- }, fromIndex)
803
- ],
804
- options: fromIndex
805
- });
806
- }
807
- if (toIndex) {
808
- addIndexes.push({
809
- columns: [
810
- __spreadValues$3({
811
- column: key
812
- }, toIndex)
813
- ],
814
- options: toIndex
815
- });
819
+ const indexesLen = Math.max(
820
+ ((_e = from.indexes) == null ? void 0 : _e.length) || 0,
821
+ ((_f = to.indexes) == null ? void 0 : _f.length) || 0
822
+ );
823
+ for (let i = 0; i < indexesLen; i++) {
824
+ const fromIndex = (_g = from.indexes) == null ? void 0 : _g[i];
825
+ const toIndex = (_h = to.indexes) == null ? void 0 : _h[i];
826
+ if ((fromIndex || toIndex) && (!fromIndex || !toIndex || fromIndex.collate !== toIndex.collate || fromIndex.opclass !== toIndex.opclass || fromIndex.order !== toIndex.order || fromIndex.name !== toIndex.name || fromIndex.unique !== toIndex.unique || fromIndex.using !== toIndex.using || fromIndex.include !== toIndex.include || Array.isArray(fromIndex.include) && Array.isArray(toIndex.include) && fromIndex.include.join(",") !== toIndex.include.join(",") || fromIndex.with !== toIndex.with || fromIndex.tablespace !== toIndex.tablespace || fromIndex.where !== toIndex.where || fromIndex.dropMode !== toIndex.dropMode)) {
827
+ if (fromIndex) {
828
+ dropIndexes.push({
829
+ columns: [
830
+ __spreadValues$3({
831
+ column: key
832
+ }, fromIndex)
833
+ ],
834
+ options: fromIndex
835
+ });
836
+ }
837
+ if (toIndex) {
838
+ addIndexes.push({
839
+ columns: [
840
+ __spreadValues$3({
841
+ column: key
842
+ }, toIndex)
843
+ ],
844
+ options: toIndex
845
+ });
846
+ }
816
847
  }
817
848
  }
818
849
  if (from.comment !== to.comment) {
@@ -824,13 +855,13 @@ const astToQueries = (ast) => {
824
855
  }
825
856
  const prependAlterTable = [];
826
857
  if (ast.drop.primaryKey || dropPrimaryKeys.change || dropPrimaryKeys.columns.length > 1) {
827
- const name = ((_a = dropPrimaryKeys.options) == null ? void 0 : _a.name) || `${ast.name}_pkey`;
858
+ const name = ((_i = dropPrimaryKeys.options) == null ? void 0 : _i.name) || `${ast.name}_pkey`;
828
859
  prependAlterTable.push(`DROP CONSTRAINT "${name}"`);
829
860
  }
830
861
  prependAlterTable.push(
831
862
  ...dropForeignKeys.map(
832
863
  (foreignKey) => `
833
- DROP ${constraintToSql(ast.name, false, foreignKey)}`
864
+ DROP ${constraintToSql(ast, false, foreignKey)}`
834
865
  )
835
866
  );
836
867
  alterTable.unshift(...prependAlterTable);
@@ -840,19 +871,19 @@ const astToQueries = (ast) => {
840
871
  alterTable.push(
841
872
  ...addForeignKeys.map(
842
873
  (foreignKey) => `
843
- ADD ${constraintToSql(ast.name, true, foreignKey)}`
874
+ ADD ${constraintToSql(ast, true, foreignKey)}`
844
875
  )
845
876
  );
846
877
  if (alterTable.length) {
847
878
  result.push({
848
- text: `ALTER TABLE ${quoteTable(ast.name)}
879
+ text: `ALTER TABLE ${quoteWithSchema(ast)}
849
880
  ${alterTable.join(",\n ")}`,
850
881
  values
851
882
  });
852
883
  }
853
- result.push(...indexesToQuery(false, ast.name, dropIndexes));
854
- result.push(...indexesToQuery(true, ast.name, addIndexes));
855
- result.push(...commentsToQuery(ast.name, comments));
884
+ result.push(...indexesToQuery(false, ast, dropIndexes));
885
+ result.push(...indexesToQuery(true, ast, addIndexes));
886
+ result.push(...commentsToQuery(ast, comments));
856
887
  return result;
857
888
  };
858
889
 
@@ -903,21 +934,28 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
903
934
  joinedName: joinWords(singular(table), item.name)
904
935
  }))
905
936
  );
937
+ const [schema, name] = getSchemaAndTableFromName(table);
906
938
  if (!primaryKeys.length) {
907
939
  throw new Error(
908
- `Primary key for table ${quoteTable(table)} is not defined`
940
+ `Primary key for table ${quoteWithSchema({
941
+ schema,
942
+ name
943
+ })} is not defined`
909
944
  );
910
945
  }
911
- return [table, primaryKeys];
946
+ return [schema, table, primaryKeys];
912
947
  })
913
948
  );
914
949
  return createTable(migration, up, tableName, options, (t) => {
915
950
  const result = {};
916
- tablesWithPrimaryKeys.forEach(([table, primaryKeys]) => {
951
+ tablesWithPrimaryKeys.forEach(([schema, table, primaryKeys]) => {
917
952
  if (primaryKeys.length === 1) {
918
953
  const [{ type, joinedName, name }] = primaryKeys;
919
954
  const column = new UnknownColumn(type);
920
- result[joinedName] = column.foreignKey(table, name);
955
+ result[joinedName] = column.foreignKey(
956
+ schema ? `${schema}.${table}` : table,
957
+ name
958
+ );
921
959
  return;
922
960
  }
923
961
  primaryKeys.forEach(({ joinedName, type }) => {
@@ -934,7 +972,7 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
934
972
  }
935
973
  t.primaryKey(
936
974
  tablesWithPrimaryKeys.flatMap(
937
- ([, primaryKeys]) => primaryKeys.map((item) => item.joinedName)
975
+ ([, , primaryKeys]) => primaryKeys.map((item) => item.joinedName)
938
976
  )
939
977
  );
940
978
  return result;
@@ -1000,13 +1038,23 @@ class Migration extends TransactionAdapter {
1000
1038
  return changeTable(this, this.up, tableName, options, fn);
1001
1039
  }
1002
1040
  async renameTable(from, to) {
1041
+ const [fromSchema, f] = getSchemaAndTableFromName(this.up ? from : to);
1042
+ const [toSchema, t] = getSchemaAndTableFromName(this.up ? to : from);
1003
1043
  const ast = {
1004
1044
  type: "renameTable",
1005
- from: this.up ? from : to,
1006
- to: this.up ? to : from
1045
+ fromSchema,
1046
+ from: f,
1047
+ toSchema,
1048
+ to: t
1007
1049
  };
1008
1050
  await this.query(
1009
- `ALTER TABLE ${quoteTable(ast.from)} RENAME TO "${ast.to}"`
1051
+ `ALTER TABLE ${quoteWithSchema({
1052
+ schema: ast.fromSchema,
1053
+ name: ast.from
1054
+ })} RENAME TO ${quoteWithSchema({
1055
+ schema: ast.toSchema,
1056
+ name: ast.to
1057
+ })}`
1010
1058
  );
1011
1059
  await runCodeUpdater(this, ast);
1012
1060
  }
@@ -1230,20 +1278,22 @@ const processMigration = async (db, up, file, config, options, appCodeUpdaterCac
1230
1278
  };
1231
1279
  const saveMigratedVersion = async (db, version, config) => {
1232
1280
  await db.query(
1233
- `INSERT INTO ${quoteTable(config.migrationsTable)} VALUES ('${version}')`
1281
+ `INSERT INTO ${quoteWithSchema({
1282
+ name: config.migrationsTable
1283
+ })} VALUES ('${version}')`
1234
1284
  );
1235
1285
  };
1236
1286
  const removeMigratedVersion = async (db, version, config) => {
1237
1287
  await db.query(
1238
- `DELETE FROM ${quoteTable(
1239
- config.migrationsTable
1240
- )} WHERE version = '${version}'`
1288
+ `DELETE FROM ${quoteWithSchema({
1289
+ name: config.migrationsTable
1290
+ })} WHERE version = '${version}'`
1241
1291
  );
1242
1292
  };
1243
1293
  const getMigratedVersionsMap = async (db, config) => {
1244
1294
  try {
1245
1295
  const result = await db.arrays(
1246
- `SELECT * FROM ${quoteTable(config.migrationsTable)}`
1296
+ `SELECT * FROM ${quoteWithSchema({ name: config.migrationsTable })}`
1247
1297
  );
1248
1298
  return Object.fromEntries(result.rows.map((row) => [row[0], true]));
1249
1299
  } catch (err) {