rake-db 2.2.6 → 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/dist/index.js CHANGED
@@ -251,26 +251,28 @@ const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
251
251
  line.push(`DEFAULT ${pqb.quote(item.data.default)}`);
252
252
  }
253
253
  }
254
- const { foreignKey } = item.data;
255
- if (foreignKey) {
256
- const [schema, table] = getForeignKeyTable(
257
- "fn" in foreignKey ? foreignKey.fn : foreignKey.table
258
- );
259
- if (foreignKey.name) {
260
- line.push(`CONSTRAINT "${foreignKey.name}"`);
254
+ const { foreignKeys } = item.data;
255
+ if (foreignKeys) {
256
+ for (const foreignKey of foreignKeys) {
257
+ const [schema, table] = getForeignKeyTable(
258
+ "fn" in foreignKey ? foreignKey.fn : foreignKey.table
259
+ );
260
+ if (foreignKey.name) {
261
+ line.push(`CONSTRAINT "${foreignKey.name}"`);
262
+ }
263
+ line.push(referencesToSql(schema, table, foreignKey.columns, foreignKey));
261
264
  }
262
- line.push(referencesToSql(schema, table, foreignKey.columns, foreignKey));
263
265
  }
264
266
  return line.join(" ");
265
267
  };
266
268
  const addColumnIndex = (indexes, key, item) => {
267
- if (item.data) {
268
- if (item.data.index) {
269
- indexes.push({
270
- columns: [__spreadProps$4(__spreadValues$5({}, item.data.index), { column: key })],
271
- options: item.data.index
272
- });
273
- }
269
+ if (item.data.indexes) {
270
+ indexes.push(
271
+ ...item.data.indexes.map((index) => ({
272
+ columns: [__spreadProps$4(__spreadValues$5({}, index), { column: key })],
273
+ options: index
274
+ }))
275
+ );
274
276
  }
275
277
  };
276
278
  const addColumnComment = (comments, key, item) => {
@@ -320,7 +322,11 @@ const referencesToSql = (schema, table, columns, foreignKey) => {
320
322
  };
321
323
  const indexesToQuery = (up, { schema, name }, indexes) => {
322
324
  return indexes.map(({ columns, options }) => {
323
- const indexName = options.name || joinWords(name, ...columns.map(({ column }) => column), "index");
325
+ const indexName = options.name || joinWords(
326
+ name,
327
+ ...columns.filter((it) => "column" in it).map((it) => it.column),
328
+ "index"
329
+ );
324
330
  if (!up) {
325
331
  return {
326
332
  text: `DROP INDEX "${indexName}"${options.dropMode ? ` ${options.dropMode}` : ""}`,
@@ -339,13 +345,13 @@ const indexesToQuery = (up, { schema, name }, indexes) => {
339
345
  const columnsSql = [];
340
346
  columns.forEach((column) => {
341
347
  const columnSql = [
342
- `"${column.column}"${column.expression ? `(${column.expression})` : ""}`
348
+ "column" in column ? `"${column.column}"` : `(${column.expression})`
343
349
  ];
344
350
  if (column.collate) {
345
351
  columnSql.push(`COLLATE '${column.collate}'`);
346
352
  }
347
- if (column.operator) {
348
- columnSql.push(column.operator);
353
+ if (column.opclass) {
354
+ columnSql.push(column.opclass);
349
355
  }
350
356
  if (column.order) {
351
357
  columnSql.push(column.order);
@@ -621,8 +627,8 @@ const drop = (item, options) => {
621
627
  };
622
628
  const columnTypeToColumnChange = (item) => {
623
629
  if (item instanceof pqb.ColumnType) {
624
- const foreignKey = item.data.foreignKey;
625
- if (foreignKey && "fn" in foreignKey) {
630
+ const foreignKeys = item.data.foreignKeys;
631
+ if (foreignKeys == null ? void 0 : foreignKeys.some((it) => "fn" in it)) {
626
632
  throw new Error("Callback in foreignKey is not allowed in migration");
627
633
  }
628
634
  return __spreadProps$2(__spreadValues$3({
@@ -631,7 +637,7 @@ const columnTypeToColumnChange = (item) => {
631
637
  nullable: item.data.isNullable,
632
638
  primaryKey: item.isPrimaryKey
633
639
  }, item.data), {
634
- foreignKey
640
+ foreignKeys
635
641
  });
636
642
  }
637
643
  return item.to;
@@ -711,7 +717,7 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
711
717
  }, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
712
718
  };
713
719
  const astToQueries = (ast) => {
714
- var _a;
720
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
715
721
  const result = [];
716
722
  if (ast.comment !== void 0) {
717
723
  result.push({
@@ -793,48 +799,60 @@ const astToQueries = (ast) => {
793
799
  `ALTER COLUMN "${key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
794
800
  );
795
801
  }
796
- const fromFkey = from.foreignKey;
797
- const toFkey = to.foreignKey;
798
- 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(","))) {
799
- if (fromFkey) {
800
- dropForeignKeys.push({
801
- columns: [key],
802
- fnOrTable: fromFkey.table,
803
- foreignColumns: fromFkey.columns,
804
- options: fromFkey
805
- });
806
- }
807
- if (toFkey) {
808
- addForeignKeys.push({
809
- columns: [key],
810
- fnOrTable: toFkey.table,
811
- foreignColumns: toFkey.columns,
812
- options: toFkey
813
- });
802
+ const foreignKeysLen = Math.max(
803
+ ((_a = from.foreignKeys) == null ? void 0 : _a.length) || 0,
804
+ ((_b = to.foreignKeys) == null ? void 0 : _b.length) || 0
805
+ );
806
+ for (let i = 0; i < foreignKeysLen; i++) {
807
+ const fromFkey = (_c = from.foreignKeys) == null ? void 0 : _c[i];
808
+ const toFkey = (_d = to.foreignKeys) == null ? void 0 : _d[i];
809
+ 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(","))) {
810
+ if (fromFkey) {
811
+ dropForeignKeys.push({
812
+ columns: [key],
813
+ fnOrTable: fromFkey.table,
814
+ foreignColumns: fromFkey.columns,
815
+ options: fromFkey
816
+ });
817
+ }
818
+ if (toFkey) {
819
+ addForeignKeys.push({
820
+ columns: [key],
821
+ fnOrTable: toFkey.table,
822
+ foreignColumns: toFkey.columns,
823
+ options: toFkey
824
+ });
825
+ }
814
826
  }
815
827
  }
816
- const fromIndex = from.index;
817
- const toIndex = to.index;
818
- 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)) {
819
- if (fromIndex) {
820
- dropIndexes.push({
821
- columns: [
822
- __spreadValues$3({
823
- column: key
824
- }, fromIndex)
825
- ],
826
- options: fromIndex
827
- });
828
- }
829
- if (toIndex) {
830
- addIndexes.push({
831
- columns: [
832
- __spreadValues$3({
833
- column: key
834
- }, toIndex)
835
- ],
836
- options: toIndex
837
- });
828
+ const indexesLen = Math.max(
829
+ ((_e = from.indexes) == null ? void 0 : _e.length) || 0,
830
+ ((_f = to.indexes) == null ? void 0 : _f.length) || 0
831
+ );
832
+ for (let i = 0; i < indexesLen; i++) {
833
+ const fromIndex = (_g = from.indexes) == null ? void 0 : _g[i];
834
+ const toIndex = (_h = to.indexes) == null ? void 0 : _h[i];
835
+ 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)) {
836
+ if (fromIndex) {
837
+ dropIndexes.push({
838
+ columns: [
839
+ __spreadValues$3({
840
+ column: key
841
+ }, fromIndex)
842
+ ],
843
+ options: fromIndex
844
+ });
845
+ }
846
+ if (toIndex) {
847
+ addIndexes.push({
848
+ columns: [
849
+ __spreadValues$3({
850
+ column: key
851
+ }, toIndex)
852
+ ],
853
+ options: toIndex
854
+ });
855
+ }
838
856
  }
839
857
  }
840
858
  if (from.comment !== to.comment) {
@@ -846,7 +864,7 @@ const astToQueries = (ast) => {
846
864
  }
847
865
  const prependAlterTable = [];
848
866
  if (ast.drop.primaryKey || dropPrimaryKeys.change || dropPrimaryKeys.columns.length > 1) {
849
- const name = ((_a = dropPrimaryKeys.options) == null ? void 0 : _a.name) || `${ast.name}_pkey`;
867
+ const name = ((_i = dropPrimaryKeys.options) == null ? void 0 : _i.name) || `${ast.name}_pkey`;
850
868
  prependAlterTable.push(`DROP CONSTRAINT "${name}"`);
851
869
  }
852
870
  prependAlterTable.push(