rake-db 2.0.36 → 2.1.1

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 ADDED
@@ -0,0 +1,12 @@
1
+ # rake-db
2
+
3
+ ## 2.1.0
4
+
5
+ ### Minor Changes
6
+
7
+ - 883c3e4: Add changeset
8
+
9
+ ### Patch Changes
10
+
11
+ - Updated dependencies [883c3e4]
12
+ - pqb@0.7.0
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 Roman Kushyn
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as pqb from 'pqb';
2
- import { QueryLogOptions, MaybeArray, AdapterOptions, ColumnTypes, EmptyObject, RawExpression, ColumnType, raw, ColumnsShape, IndexColumnOptions, IndexOptions, TransactionAdapter, QueryLogObject, QueryResultRow, QueryInput, TypeParsers, QueryResult, QueryArraysResult, ForeignKeyOptions } from 'pqb';
2
+ import { QueryLogOptions, MaybeArray, AdapterOptions, ColumnTypes, EmptyObject, RawExpression, ColumnType, ForeignKey, SingleColumnIndexOptions, raw, ColumnsShape, IndexColumnOptions, IndexOptions, TransactionAdapter, QueryLogObject, QueryResultRow, QueryInput, TypeParsers, QueryResult, QueryArraysResult, ForeignKeyOptions } from 'pqb';
3
3
 
4
4
  declare type MigrationConfig = {
5
5
  migrationsPath: string;
@@ -27,7 +27,7 @@ declare type ChangeOptions = {
27
27
  usingUp?: RawExpression;
28
28
  usingDown?: RawExpression;
29
29
  };
30
- declare type ChangeArg = ColumnType | ['default', unknown | RawExpression] | ['nullable', boolean] | ['comment', string | null];
30
+ declare type ChangeArg = ColumnType | ['default', unknown | RawExpression] | ['nullable', boolean] | ['comment', string | null] | ['compression', string] | ['foreignKey', ForeignKey<string, string[]>] | ['index', Omit<SingleColumnIndexOptions, 'column'>];
31
31
  declare type TableChangeMethods = typeof tableChangeMethods;
32
32
  declare const tableChangeMethods: {
33
33
  raw: (sql: string, ...values: unknown[]) => RawExpression<ColumnType<unknown, pqb.Operators, unknown>>;
package/dist/index.esm.js CHANGED
@@ -1,24 +1,24 @@
1
- import { quote, isRaw, getRaw, toArray, columnTypes, raw, getColumnTypes, getTableData, ColumnType, Operators, resetTableData, emptyObject, TransactionAdapter, logParamToLogObject, Adapter } from 'pqb';
1
+ import { quote, isRaw, getRaw, toArray, columnTypes, raw, getColumnTypes, getTableData, ColumnType, Operators, resetTableData, newTableData, emptyObject, TransactionAdapter, logParamToLogObject, Adapter } from 'pqb';
2
2
  import Enquirer from 'enquirer';
3
3
  import path from 'path';
4
4
  import { readdir, mkdir, writeFile } from 'fs/promises';
5
5
  import { singular } from 'pluralize';
6
6
 
7
- var __defProp$3 = Object.defineProperty;
7
+ var __defProp$4 = Object.defineProperty;
8
8
  var __defProps$3 = Object.defineProperties;
9
9
  var __getOwnPropDescs$3 = Object.getOwnPropertyDescriptors;
10
- var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
11
- var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
12
- var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
13
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
- var __spreadValues$3 = (a, b) => {
10
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
11
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
12
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
13
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
14
+ var __spreadValues$4 = (a, b) => {
15
15
  for (var prop in b || (b = {}))
16
- if (__hasOwnProp$3.call(b, prop))
17
- __defNormalProp$3(a, prop, b[prop]);
18
- if (__getOwnPropSymbols$3)
19
- for (var prop of __getOwnPropSymbols$3(b)) {
20
- if (__propIsEnum$3.call(b, prop))
21
- __defNormalProp$3(a, prop, b[prop]);
16
+ if (__hasOwnProp$4.call(b, prop))
17
+ __defNormalProp$4(a, prop, b[prop]);
18
+ if (__getOwnPropSymbols$4)
19
+ for (var prop of __getOwnPropSymbols$4(b)) {
20
+ if (__propIsEnum$4.call(b, prop))
21
+ __defNormalProp$4(a, prop, b[prop]);
22
22
  }
23
23
  return a;
24
24
  };
@@ -31,7 +31,7 @@ const migrationConfigDefaults = {
31
31
  logger: console
32
32
  };
33
33
  const getMigrationConfigWithDefaults = (config) => {
34
- return __spreadValues$3(__spreadValues$3({}, migrationConfigDefaults), config);
34
+ return __spreadValues$4(__spreadValues$4({}, migrationConfigDefaults), config);
35
35
  };
36
36
  const getDatabaseAndUserFromOptions = (options) => {
37
37
  if (options.connectionString) {
@@ -59,9 +59,9 @@ const setAdapterOptions = (options, set) => {
59
59
  if (set.password !== void 0) {
60
60
  url.password = set.password;
61
61
  }
62
- return __spreadProps$3(__spreadValues$3({}, options), { connectionString: url.toString() });
62
+ return __spreadProps$3(__spreadValues$4({}, options), { connectionString: url.toString() });
63
63
  } else {
64
- return __spreadValues$3(__spreadValues$3({}, options), set);
64
+ return __spreadValues$4(__spreadValues$4({}, options), set);
65
65
  }
66
66
  };
67
67
  const askAdminCredentials = async () => {
@@ -200,21 +200,21 @@ const setCurrentMigrationUp = (up) => {
200
200
  const getCurrentPromise = () => currentPromise;
201
201
  const getCurrentChangeCallback = () => currentChangeCallback;
202
202
 
203
- var __defProp$2 = Object.defineProperty;
203
+ var __defProp$3 = Object.defineProperty;
204
204
  var __defProps$2 = Object.defineProperties;
205
205
  var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
206
- var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
207
- var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
208
- var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
209
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
210
- var __spreadValues$2 = (a, b) => {
206
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
207
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
208
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
209
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
210
+ var __spreadValues$3 = (a, b) => {
211
211
  for (var prop in b || (b = {}))
212
- if (__hasOwnProp$2.call(b, prop))
213
- __defNormalProp$2(a, prop, b[prop]);
214
- if (__getOwnPropSymbols$2)
215
- for (var prop of __getOwnPropSymbols$2(b)) {
216
- if (__propIsEnum$2.call(b, prop))
217
- __defNormalProp$2(a, prop, b[prop]);
212
+ if (__hasOwnProp$3.call(b, prop))
213
+ __defNormalProp$3(a, prop, b[prop]);
214
+ if (__getOwnPropSymbols$3)
215
+ for (var prop of __getOwnPropSymbols$3(b)) {
216
+ if (__propIsEnum$3.call(b, prop))
217
+ __defNormalProp$3(a, prop, b[prop]);
218
218
  }
219
219
  return a;
220
220
  };
@@ -255,7 +255,7 @@ const addColumnIndex = (indexes, key, item) => {
255
255
  if (item.data) {
256
256
  if (item.data.index) {
257
257
  indexes.push({
258
- columns: [__spreadProps$2(__spreadValues$2({}, item.data.index), { column: key })],
258
+ columns: [__spreadProps$2(__spreadValues$3({}, item.data.index), { column: key })],
259
259
  options: item.data.index
260
260
  });
261
261
  }
@@ -394,21 +394,21 @@ WHERE
394
394
  return rows;
395
395
  };
396
396
 
397
- var __defProp$1 = Object.defineProperty;
397
+ var __defProp$2 = Object.defineProperty;
398
398
  var __defProps$1 = Object.defineProperties;
399
399
  var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
400
- var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
401
- var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
402
- var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
403
- var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
404
- var __spreadValues$1 = (a, b) => {
400
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
401
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
402
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
403
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
404
+ var __spreadValues$2 = (a, b) => {
405
405
  for (var prop in b || (b = {}))
406
- if (__hasOwnProp$1.call(b, prop))
407
- __defNormalProp$1(a, prop, b[prop]);
408
- if (__getOwnPropSymbols$1)
409
- for (var prop of __getOwnPropSymbols$1(b)) {
410
- if (__propIsEnum$1.call(b, prop))
411
- __defNormalProp$1(a, prop, b[prop]);
406
+ if (__hasOwnProp$2.call(b, prop))
407
+ __defNormalProp$2(a, prop, b[prop]);
408
+ if (__getOwnPropSymbols$2)
409
+ for (var prop of __getOwnPropSymbols$2(b)) {
410
+ if (__propIsEnum$2.call(b, prop))
411
+ __defNormalProp$2(a, prop, b[prop]);
412
412
  }
413
413
  return a;
414
414
  };
@@ -428,7 +428,7 @@ const createJoinTable = async (migration, up, tables, options, fn) => {
428
428
  const tablesWithPrimaryKeys = await Promise.all(
429
429
  tables.map(async (table) => {
430
430
  const primaryKeys = await getPrimaryKeysOfTable(migration, table).then(
431
- (items) => items.map((item) => __spreadProps$1(__spreadValues$1({}, item), {
431
+ (items) => items.map((item) => __spreadProps$1(__spreadValues$2({}, item), {
432
432
  joinedName: joinWords(singular(table), item.name)
433
433
  }))
434
434
  );
@@ -520,6 +520,22 @@ const createTable = async (migration, up, tableName, options, fn) => {
520
520
  }
521
521
  };
522
522
 
523
+ var __defProp$1 = Object.defineProperty;
524
+ var __getOwnPropSymbols$1 = Object.getOwnPropertySymbols;
525
+ var __hasOwnProp$1 = Object.prototype.hasOwnProperty;
526
+ var __propIsEnum$1 = Object.prototype.propertyIsEnumerable;
527
+ var __defNormalProp$1 = (obj, key, value) => key in obj ? __defProp$1(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
528
+ var __spreadValues$1 = (a, b) => {
529
+ for (var prop in b || (b = {}))
530
+ if (__hasOwnProp$1.call(b, prop))
531
+ __defNormalProp$1(a, prop, b[prop]);
532
+ if (__getOwnPropSymbols$1)
533
+ for (var prop of __getOwnPropSymbols$1(b)) {
534
+ if (__propIsEnum$1.call(b, prop))
535
+ __defNormalProp$1(a, prop, b[prop]);
536
+ }
537
+ return a;
538
+ };
523
539
  const newChangeTableData = () => ({
524
540
  add: [],
525
541
  drop: []
@@ -686,6 +702,68 @@ const changeActions = {
686
702
  `ALTER COLUMN "${key}" ${to.nullable ? "DROP" : "SET"} NOT NULL`
687
703
  );
688
704
  }
705
+ if (from.compression !== to.compression) {
706
+ state.alterTable.push(
707
+ `ALTER COLUMN "${key}" SET COMPRESSION ${to.compression || "DEFAULT"}`
708
+ );
709
+ }
710
+ const fromFkey = from.foreignKey;
711
+ const toFkey = to.foreignKey;
712
+ if (fromFkey || toFkey) {
713
+ if (fromFkey && "fn" in fromFkey || toFkey && "fn" in toFkey) {
714
+ throw new Error("Callback in foreignKey is not allowed in migration");
715
+ }
716
+ if (checkIfForeignKeysAreDifferent(fromFkey, toFkey)) {
717
+ if (fromFkey) {
718
+ const data = newTableData();
719
+ data.foreignKeys.push({
720
+ columns: [key],
721
+ fnOrTable: fromFkey.table,
722
+ foreignColumns: fromFkey.columns,
723
+ options: fromFkey
724
+ });
725
+ changeTableData[up ? "drop" : "add"].push(data);
726
+ }
727
+ if (toFkey) {
728
+ const data = newTableData();
729
+ data.foreignKeys.push({
730
+ columns: [key],
731
+ fnOrTable: toFkey.table,
732
+ foreignColumns: toFkey.columns,
733
+ options: toFkey
734
+ });
735
+ changeTableData[up ? "add" : "drop"].push(data);
736
+ }
737
+ }
738
+ }
739
+ const fromIndex = from.index;
740
+ const toIndex = to.index;
741
+ if ((fromIndex || toIndex) && checkIfIndexesAreDifferent(fromIndex, toIndex)) {
742
+ if (fromIndex) {
743
+ const data = newTableData();
744
+ data.indexes.push({
745
+ columns: [
746
+ __spreadValues$1({
747
+ column: key
748
+ }, fromIndex)
749
+ ],
750
+ options: fromIndex
751
+ });
752
+ changeTableData[up ? "drop" : "add"].push(data);
753
+ }
754
+ if (toIndex) {
755
+ const data = newTableData();
756
+ data.indexes.push({
757
+ columns: [
758
+ __spreadValues$1({
759
+ column: key
760
+ }, toIndex)
761
+ ],
762
+ options: toIndex
763
+ });
764
+ changeTableData[up ? "add" : "drop"].push(data);
765
+ }
766
+ }
689
767
  if (from.comment !== to.comment) {
690
768
  state.comments.push({ column: key, comment: to.comment || null });
691
769
  }
@@ -695,6 +773,12 @@ const changeActions = {
695
773
  state.alterTable.push(`RENAME COLUMN "${from}" TO "${to}"`);
696
774
  }
697
775
  };
776
+ const checkIfForeignKeysAreDifferent = (from, to) => {
777
+ return !from || !to || from.name !== to.name || from.match !== to.match || from.onUpdate !== to.onUpdate || from.onDelete !== to.onDelete || from.dropMode !== to.dropMode || from.table !== to.table || from.columns.join(",") !== to.columns.join(",");
778
+ };
779
+ const checkIfIndexesAreDifferent = (from, to) => {
780
+ return !from || !to || from.expression !== to.expression || from.collate !== to.collate || from.operator !== to.operator || from.order !== to.order || from.name !== to.name || from.unique !== to.unique || from.using !== to.using || from.include !== to.include || Array.isArray(from.include) && Array.isArray(to.include) && from.include.join(",") !== to.include.join(",") || from.with !== to.with || from.tablespace !== to.tablespace || from.where !== to.where || from.dropMode !== to.dropMode;
781
+ };
698
782
  const getChangeProperties = (item) => {
699
783
  if (item instanceof ColumnType) {
700
784
  return {
@@ -702,7 +786,10 @@ const getChangeProperties = (item) => {
702
786
  collate: item.data.collate,
703
787
  default: item.data.default,
704
788
  nullable: item.isNullable,
705
- comment: item.data.comment
789
+ comment: item.data.comment,
790
+ compression: item.data.compression,
791
+ foreignKey: item.data.foreignKey,
792
+ index: item.data.index
706
793
  };
707
794
  } else {
708
795
  return {
@@ -710,7 +797,10 @@ const getChangeProperties = (item) => {
710
797
  collate: void 0,
711
798
  default: item[0] === "default" ? item[1] : void 0,
712
799
  nullable: item[0] === "nullable" ? item[1] : void 0,
713
- comment: item[0] === "comment" ? item[1] : void 0
800
+ comment: item[0] === "comment" ? item[1] : void 0,
801
+ compression: item[0] === "compression" ? item[1] : void 0,
802
+ foreignKey: item[0] === "foreignKey" ? item[1] : void 0,
803
+ index: item[0] === "index" ? item[1] : void 0
714
804
  };
715
805
  }
716
806
  };