rake-db 2.3.1 → 2.3.2

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,13 @@
1
1
  # rake-db
2
2
 
3
+ ## 2.3.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Improve pulling db structure
8
+ - Updated dependencies
9
+ - pqb@0.9.2
10
+
3
11
  ## 2.3.1
4
12
 
5
13
  ### Patch Changes
package/dist/index.esm.js CHANGED
@@ -1,4 +1,4 @@
1
- import { singleQuote, quote, isRaw, getRaw, toArray, columnTypes, raw, getColumnTypes, getTableData, resetTableData, ColumnType, emptyObject, Operators, TransactionAdapter, logParamToLogObject, Adapter, columnsByType, instantiateColumn, codeToString, addCode, quoteObjectKey, primaryKeyToCode, indexToCode, foreignKeyToCode } from 'pqb';
1
+ import { singleQuote, quote, isRaw, getRaw, toArray, columnTypes, raw, getColumnTypes, getTableData, resetTableData, ColumnType, emptyObject, Operators, TransactionAdapter, logParamToLogObject, Adapter, columnsByType, instantiateColumn, codeToString, addCode, quoteObjectKey, primaryKeyToCode, indexToCode, foreignKeyToCode, TimestampColumn } from 'pqb';
2
2
  import Enquirer from 'enquirer';
3
3
  import path from 'path';
4
4
  import { readdir, mkdir, writeFile } from 'fs/promises';
@@ -1771,8 +1771,8 @@ const matchMap = {
1771
1771
  p: "PARTIAL"
1772
1772
  };
1773
1773
  const fkeyActionMap = {
1774
- a: "NO ACTION",
1775
- r: void 0,
1774
+ a: void 0,
1775
+ r: "RESTRICT",
1776
1776
  c: "CASCADE",
1777
1777
  n: "SET NULL",
1778
1778
  d: "SET DEFAULT"
@@ -1807,6 +1807,8 @@ const structureToAst = async (db) => {
1807
1807
  }
1808
1808
  for (const table of tables) {
1809
1809
  const { schemaName, name } = table;
1810
+ if (name === "schemaMigrations")
1811
+ continue;
1810
1812
  const belongsToTable = makeBelongsToTable(schemaName, name);
1811
1813
  const columns = allColumns.filter(belongsToTable);
1812
1814
  const primaryKey = allPrimaryKeys.find(belongsToTable);
@@ -1815,13 +1817,13 @@ const structureToAst = async (db) => {
1815
1817
  const shape = {};
1816
1818
  for (let item of columns) {
1817
1819
  const isSerial = getIsSerial(item);
1820
+ if (isSerial) {
1821
+ item = __spreadProps(__spreadValues({}, item), { default: void 0 });
1822
+ }
1818
1823
  const klass = columnsByType[getColumnType(item, isSerial)];
1819
1824
  if (!klass) {
1820
1825
  throw new Error(`Column type \`${item.type}\` is not supported`);
1821
1826
  }
1822
- if (isSerial) {
1823
- item = __spreadProps(__spreadValues({}, item), { default: void 0 });
1824
- }
1825
1827
  let column = instantiateColumn(klass, item);
1826
1828
  if ((primaryKey == null ? void 0 : primaryKey.columnNames.length) === 1 && (primaryKey == null ? void 0 : primaryKey.columnNames[0]) === item.name) {
1827
1829
  column = column.primaryKey();
@@ -1959,12 +1961,20 @@ const createSchema = (ast) => {
1959
1961
  const createTable = (ast) => {
1960
1962
  const code = [];
1961
1963
  addCode(code, `await db.createTable(${quoteSchemaTable(ast)}, (t) => ({`);
1964
+ const hasTimestamps = isTimestamp(ast.shape.createdAt) && isTimestamp(ast.shape.updatedAt);
1962
1965
  for (const key in ast.shape) {
1966
+ if (hasTimestamps && (key === "createdAt" || key === "updatedAt"))
1967
+ continue;
1963
1968
  const line = [`${quoteObjectKey(key)}: `];
1964
- addCode(line, ast.shape[key].toCode("t"));
1969
+ for (const part of ast.shape[key].toCode("t")) {
1970
+ addCode(line, part);
1971
+ }
1965
1972
  addCode(line, ",");
1966
1973
  code.push(line);
1967
1974
  }
1975
+ if (hasTimestamps) {
1976
+ code.push(["...t.timestamps(),"]);
1977
+ }
1968
1978
  if (ast.primaryKey) {
1969
1979
  code.push([primaryKeyToCode(ast.primaryKey, "t")]);
1970
1980
  }
@@ -1977,10 +1987,18 @@ const createTable = (ast) => {
1977
1987
  addCode(code, "}));");
1978
1988
  return code;
1979
1989
  };
1990
+ const isTimestamp = (column) => {
1991
+ if (!column)
1992
+ return false;
1993
+ const { default: def } = column.data;
1994
+ return column instanceof TimestampColumn && !column.data.isNullable && def && typeof def === "object" && isRaw(def) && def.__raw === "now()";
1995
+ };
1980
1996
 
1981
1997
  const pullDbStructure = async (options, config) => {
1982
- const db = new DbStructure(new Adapter(options));
1998
+ const adapter = new Adapter(options);
1999
+ const db = new DbStructure(adapter);
1983
2000
  const ast = await structureToAst(db);
2001
+ await adapter.close();
1984
2002
  const result = astToMigration(ast);
1985
2003
  if (!result)
1986
2004
  return;