rake-db 2.3.1 → 2.3.3

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,19 @@
1
1
  # rake-db
2
2
 
3
+ ## 2.3.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Improve pullindg db structure
8
+
9
+ ## 2.3.2
10
+
11
+ ### Patch Changes
12
+
13
+ - Improve pulling db structure
14
+ - Updated dependencies
15
+ - pqb@0.9.2
16
+
3
17
  ## 2.3.1
4
18
 
5
19
  ### 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';
@@ -284,8 +284,11 @@ const getForeignKeyTable = (fnOrTable) => {
284
284
  const item = new (fnOrTable())();
285
285
  return [item.schema, item.table];
286
286
  };
287
+ const getForeignKeyName = (table, columns) => {
288
+ return `${table}_${columns.join("_")}_fkey`;
289
+ };
287
290
  const constraintToSql = ({ name }, up, foreignKey) => {
288
- const constraintName = foreignKey.options.name || `${name}_${foreignKey.columns.join("_")}_fkey`;
291
+ const constraintName = foreignKey.options.name || getForeignKeyName(name, foreignKey.columns);
289
292
  if (!up) {
290
293
  const { dropMode } = foreignKey.options;
291
294
  return `CONSTRAINT "${constraintName}"${dropMode ? ` ${dropMode}` : ""}`;
@@ -317,13 +320,17 @@ const referencesToSql = (schema, table, columns, foreignKey) => {
317
320
  }
318
321
  return sql.join(" ");
319
322
  };
323
+ const getIndexName = (table, columns) => {
324
+ return `${table}_${columns.map(
325
+ (it) => {
326
+ var _a;
327
+ return "column" in it ? it.column : ((_a = it.expression.match(/\w+/g)) == null ? void 0 : _a.join("_")) || "expression";
328
+ }
329
+ ).join("_")}_idx`;
330
+ };
320
331
  const indexesToQuery = (up, { schema, name }, indexes) => {
321
332
  return indexes.map(({ columns, options }) => {
322
- const indexName = options.name || joinWords(
323
- name,
324
- ...columns.filter((it) => "column" in it).map((it) => it.column),
325
- "index"
326
- );
333
+ const indexName = options.name || getIndexName(name, columns);
327
334
  if (!up) {
328
335
  return {
329
336
  text: `DROP INDEX "${indexName}"${options.dropMode ? ` ${options.dropMode}` : ""}`,
@@ -1771,8 +1778,8 @@ const matchMap = {
1771
1778
  p: "PARTIAL"
1772
1779
  };
1773
1780
  const fkeyActionMap = {
1774
- a: "NO ACTION",
1775
- r: void 0,
1781
+ a: void 0,
1782
+ r: "RESTRICT",
1776
1783
  c: "CASCADE",
1777
1784
  n: "SET NULL",
1778
1785
  d: "SET DEFAULT"
@@ -1807,6 +1814,8 @@ const structureToAst = async (db) => {
1807
1814
  }
1808
1815
  for (const table of tables) {
1809
1816
  const { schemaName, name } = table;
1817
+ if (name === "schemaMigrations")
1818
+ continue;
1810
1819
  const belongsToTable = makeBelongsToTable(schemaName, name);
1811
1820
  const columns = allColumns.filter(belongsToTable);
1812
1821
  const primaryKey = allPrimaryKeys.find(belongsToTable);
@@ -1815,13 +1824,13 @@ const structureToAst = async (db) => {
1815
1824
  const shape = {};
1816
1825
  for (let item of columns) {
1817
1826
  const isSerial = getIsSerial(item);
1827
+ if (isSerial) {
1828
+ item = __spreadProps(__spreadValues({}, item), { default: void 0 });
1829
+ }
1818
1830
  const klass = columnsByType[getColumnType(item, isSerial)];
1819
1831
  if (!klass) {
1820
1832
  throw new Error(`Column type \`${item.type}\` is not supported`);
1821
1833
  }
1822
- if (isSerial) {
1823
- item = __spreadProps(__spreadValues({}, item), { default: void 0 });
1824
- }
1825
1834
  let column = instantiateColumn(klass, item);
1826
1835
  if ((primaryKey == null ? void 0 : primaryKey.columnNames.length) === 1 && (primaryKey == null ? void 0 : primaryKey.columnNames[0]) === item.name) {
1827
1836
  column = column.primaryKey();
@@ -1835,7 +1844,7 @@ const structureToAst = async (db) => {
1835
1844
  collate: options.collate,
1836
1845
  opclass: options.opclass,
1837
1846
  order: options.order,
1838
- name: index.name,
1847
+ name: index.name !== getIndexName(name, index.columns) ? index.name : void 0,
1839
1848
  using: index.using === "btree" ? void 0 : index.using,
1840
1849
  unique: index.isUnique,
1841
1850
  include: index.include,
@@ -1852,7 +1861,7 @@ const structureToAst = async (db) => {
1852
1861
  foreignKey.foreignTableName,
1853
1862
  foreignKey.foreignColumnNames[0],
1854
1863
  {
1855
- name: foreignKey.name,
1864
+ name: foreignKey.name && foreignKey.name !== getForeignKeyName(name, foreignKey.columnNames) ? foreignKey.name : void 0,
1856
1865
  match: matchMap[foreignKey.match],
1857
1866
  onUpdate: fkeyActionMap[foreignKey.onUpdate],
1858
1867
  onDelete: fkeyActionMap[foreignKey.onDelete]
@@ -1882,7 +1891,7 @@ const structureToAst = async (db) => {
1882
1891
  order: it.order
1883
1892
  })),
1884
1893
  options: {
1885
- name: index.name,
1894
+ name: index.name !== getIndexName(name, index.columns) ? index.name : void 0,
1886
1895
  using: index.using === "btree" ? void 0 : index.using,
1887
1896
  unique: index.isUnique,
1888
1897
  include: index.include,
@@ -1896,7 +1905,7 @@ const structureToAst = async (db) => {
1896
1905
  fnOrTable: it.foreignTableName,
1897
1906
  foreignColumns: it.foreignColumnNames,
1898
1907
  options: {
1899
- name: it.name,
1908
+ name: it.name && it.name !== getForeignKeyName(name, it.columnNames) ? it.name : void 0,
1900
1909
  match: matchMap[it.match],
1901
1910
  onUpdate: fkeyActionMap[it.onUpdate],
1902
1911
  onDelete: fkeyActionMap[it.onDelete]
@@ -1959,12 +1968,20 @@ const createSchema = (ast) => {
1959
1968
  const createTable = (ast) => {
1960
1969
  const code = [];
1961
1970
  addCode(code, `await db.createTable(${quoteSchemaTable(ast)}, (t) => ({`);
1971
+ const hasTimestamps = isTimestamp(ast.shape.createdAt) && isTimestamp(ast.shape.updatedAt);
1962
1972
  for (const key in ast.shape) {
1973
+ if (hasTimestamps && (key === "createdAt" || key === "updatedAt"))
1974
+ continue;
1963
1975
  const line = [`${quoteObjectKey(key)}: `];
1964
- addCode(line, ast.shape[key].toCode("t"));
1976
+ for (const part of ast.shape[key].toCode("t")) {
1977
+ addCode(line, part);
1978
+ }
1965
1979
  addCode(line, ",");
1966
1980
  code.push(line);
1967
1981
  }
1982
+ if (hasTimestamps) {
1983
+ code.push(["...t.timestamps(),"]);
1984
+ }
1968
1985
  if (ast.primaryKey) {
1969
1986
  code.push([primaryKeyToCode(ast.primaryKey, "t")]);
1970
1987
  }
@@ -1977,10 +1994,18 @@ const createTable = (ast) => {
1977
1994
  addCode(code, "}));");
1978
1995
  return code;
1979
1996
  };
1997
+ const isTimestamp = (column) => {
1998
+ if (!column)
1999
+ return false;
2000
+ const { default: def } = column.data;
2001
+ return column instanceof TimestampColumn && !column.data.isNullable && def && typeof def === "object" && isRaw(def) && def.__raw === "now()";
2002
+ };
1980
2003
 
1981
2004
  const pullDbStructure = async (options, config) => {
1982
- const db = new DbStructure(new Adapter(options));
2005
+ const adapter = new Adapter(options);
2006
+ const db = new DbStructure(adapter);
1983
2007
  const ast = await structureToAst(db);
2008
+ await adapter.close();
1984
2009
  const result = astToMigration(ast);
1985
2010
  if (!result)
1986
2011
  return;