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/CHANGELOG.md CHANGED
@@ -1,5 +1,18 @@
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
+
3
16
  ## 2.2.6
4
17
 
5
18
  ### Patch Changes
package/dist/index.d.ts CHANGED
@@ -156,11 +156,11 @@ declare namespace RakeDbAst {
156
156
  comment?: string | null;
157
157
  compression?: string;
158
158
  primaryKey?: boolean;
159
- foreignKey?: {
159
+ foreignKeys?: ({
160
160
  table: string;
161
161
  columns: string[];
162
- } & ForeignKeyOptions;
163
- index?: Omit<SingleColumnIndexOptions, 'column'>;
162
+ } & ForeignKeyOptions)[];
163
+ indexes?: Omit<SingleColumnIndexOptions, 'column' | 'expression'>[];
164
164
  };
165
165
  type RenameTable = {
166
166
  type: 'renameTable';
package/dist/index.esm.js CHANGED
@@ -242,26 +242,28 @@ const columnToSql = (key, item, values, hasMultiplePrimaryKeys) => {
242
242
  line.push(`DEFAULT ${quote(item.data.default)}`);
243
243
  }
244
244
  }
245
- const { foreignKey } = item.data;
246
- if (foreignKey) {
247
- const [schema, table] = getForeignKeyTable(
248
- "fn" in foreignKey ? foreignKey.fn : foreignKey.table
249
- );
250
- if (foreignKey.name) {
251
- 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));
252
255
  }
253
- line.push(referencesToSql(schema, table, foreignKey.columns, foreignKey));
254
256
  }
255
257
  return line.join(" ");
256
258
  };
257
259
  const addColumnIndex = (indexes, key, item) => {
258
- if (item.data) {
259
- if (item.data.index) {
260
- indexes.push({
261
- columns: [__spreadProps$4(__spreadValues$5({}, item.data.index), { column: key })],
262
- options: item.data.index
263
- });
264
- }
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
+ );
265
267
  }
266
268
  };
267
269
  const addColumnComment = (comments, key, item) => {
@@ -311,7 +313,11 @@ const referencesToSql = (schema, table, columns, foreignKey) => {
311
313
  };
312
314
  const indexesToQuery = (up, { schema, name }, indexes) => {
313
315
  return indexes.map(({ columns, options }) => {
314
- const indexName = options.name || joinWords(name, ...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
+ );
315
321
  if (!up) {
316
322
  return {
317
323
  text: `DROP INDEX "${indexName}"${options.dropMode ? ` ${options.dropMode}` : ""}`,
@@ -330,13 +336,13 @@ const indexesToQuery = (up, { schema, name }, indexes) => {
330
336
  const columnsSql = [];
331
337
  columns.forEach((column) => {
332
338
  const columnSql = [
333
- `"${column.column}"${column.expression ? `(${column.expression})` : ""}`
339
+ "column" in column ? `"${column.column}"` : `(${column.expression})`
334
340
  ];
335
341
  if (column.collate) {
336
342
  columnSql.push(`COLLATE '${column.collate}'`);
337
343
  }
338
- if (column.operator) {
339
- columnSql.push(column.operator);
344
+ if (column.opclass) {
345
+ columnSql.push(column.opclass);
340
346
  }
341
347
  if (column.order) {
342
348
  columnSql.push(column.order);
@@ -612,8 +618,8 @@ const drop = (item, options) => {
612
618
  };
613
619
  const columnTypeToColumnChange = (item) => {
614
620
  if (item instanceof ColumnType) {
615
- const foreignKey = item.data.foreignKey;
616
- if (foreignKey && "fn" in foreignKey) {
621
+ const foreignKeys = item.data.foreignKeys;
622
+ if (foreignKeys == null ? void 0 : foreignKeys.some((it) => "fn" in it)) {
617
623
  throw new Error("Callback in foreignKey is not allowed in migration");
618
624
  }
619
625
  return __spreadProps$2(__spreadValues$3({
@@ -622,7 +628,7 @@ const columnTypeToColumnChange = (item) => {
622
628
  nullable: item.data.isNullable,
623
629
  primaryKey: item.isPrimaryKey
624
630
  }, item.data), {
625
- foreignKey
631
+ foreignKeys
626
632
  });
627
633
  }
628
634
  return item.to;
@@ -702,7 +708,7 @@ const makeAst = (up, name, changeData, changeTableData2, options) => {
702
708
  }, up ? changeTableData2 : { add: changeTableData2.drop, drop: changeTableData2.add });
703
709
  };
704
710
  const astToQueries = (ast) => {
705
- var _a;
711
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i;
706
712
  const result = [];
707
713
  if (ast.comment !== void 0) {
708
714
  result.push({
@@ -784,48 +790,60 @@ const astToQueries = (ast) => {
784
790
  `ALTER COLUMN "${key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
785
791
  );
786
792
  }
787
- const fromFkey = from.foreignKey;
788
- const toFkey = to.foreignKey;
789
- 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(","))) {
790
- if (fromFkey) {
791
- dropForeignKeys.push({
792
- columns: [key],
793
- fnOrTable: fromFkey.table,
794
- foreignColumns: fromFkey.columns,
795
- options: fromFkey
796
- });
797
- }
798
- if (toFkey) {
799
- addForeignKeys.push({
800
- columns: [key],
801
- fnOrTable: toFkey.table,
802
- foreignColumns: toFkey.columns,
803
- options: toFkey
804
- });
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
+ }
805
817
  }
806
818
  }
807
- const fromIndex = from.index;
808
- const toIndex = to.index;
809
- 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)) {
810
- if (fromIndex) {
811
- dropIndexes.push({
812
- columns: [
813
- __spreadValues$3({
814
- column: key
815
- }, fromIndex)
816
- ],
817
- options: fromIndex
818
- });
819
- }
820
- if (toIndex) {
821
- addIndexes.push({
822
- columns: [
823
- __spreadValues$3({
824
- column: key
825
- }, toIndex)
826
- ],
827
- options: toIndex
828
- });
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
+ }
829
847
  }
830
848
  }
831
849
  if (from.comment !== to.comment) {
@@ -837,7 +855,7 @@ const astToQueries = (ast) => {
837
855
  }
838
856
  const prependAlterTable = [];
839
857
  if (ast.drop.primaryKey || dropPrimaryKeys.change || dropPrimaryKeys.columns.length > 1) {
840
- 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`;
841
859
  prependAlterTable.push(`DROP CONSTRAINT "${name}"`);
842
860
  }
843
861
  prependAlterTable.push(