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/dist/index.js CHANGED
@@ -1780,8 +1780,8 @@ const matchMap = {
1780
1780
  p: "PARTIAL"
1781
1781
  };
1782
1782
  const fkeyActionMap = {
1783
- a: "NO ACTION",
1784
- r: void 0,
1783
+ a: void 0,
1784
+ r: "RESTRICT",
1785
1785
  c: "CASCADE",
1786
1786
  n: "SET NULL",
1787
1787
  d: "SET DEFAULT"
@@ -1816,6 +1816,8 @@ const structureToAst = async (db) => {
1816
1816
  }
1817
1817
  for (const table of tables) {
1818
1818
  const { schemaName, name } = table;
1819
+ if (name === "schemaMigrations")
1820
+ continue;
1819
1821
  const belongsToTable = makeBelongsToTable(schemaName, name);
1820
1822
  const columns = allColumns.filter(belongsToTable);
1821
1823
  const primaryKey = allPrimaryKeys.find(belongsToTable);
@@ -1824,13 +1826,13 @@ const structureToAst = async (db) => {
1824
1826
  const shape = {};
1825
1827
  for (let item of columns) {
1826
1828
  const isSerial = getIsSerial(item);
1829
+ if (isSerial) {
1830
+ item = __spreadProps(__spreadValues({}, item), { default: void 0 });
1831
+ }
1827
1832
  const klass = pqb.columnsByType[getColumnType(item, isSerial)];
1828
1833
  if (!klass) {
1829
1834
  throw new Error(`Column type \`${item.type}\` is not supported`);
1830
1835
  }
1831
- if (isSerial) {
1832
- item = __spreadProps(__spreadValues({}, item), { default: void 0 });
1833
- }
1834
1836
  let column = pqb.instantiateColumn(klass, item);
1835
1837
  if ((primaryKey == null ? void 0 : primaryKey.columnNames.length) === 1 && (primaryKey == null ? void 0 : primaryKey.columnNames[0]) === item.name) {
1836
1838
  column = column.primaryKey();
@@ -1968,12 +1970,20 @@ const createSchema = (ast) => {
1968
1970
  const createTable = (ast) => {
1969
1971
  const code = [];
1970
1972
  pqb.addCode(code, `await db.createTable(${quoteSchemaTable(ast)}, (t) => ({`);
1973
+ const hasTimestamps = isTimestamp(ast.shape.createdAt) && isTimestamp(ast.shape.updatedAt);
1971
1974
  for (const key in ast.shape) {
1975
+ if (hasTimestamps && (key === "createdAt" || key === "updatedAt"))
1976
+ continue;
1972
1977
  const line = [`${pqb.quoteObjectKey(key)}: `];
1973
- pqb.addCode(line, ast.shape[key].toCode("t"));
1978
+ for (const part of ast.shape[key].toCode("t")) {
1979
+ pqb.addCode(line, part);
1980
+ }
1974
1981
  pqb.addCode(line, ",");
1975
1982
  code.push(line);
1976
1983
  }
1984
+ if (hasTimestamps) {
1985
+ code.push(["...t.timestamps(),"]);
1986
+ }
1977
1987
  if (ast.primaryKey) {
1978
1988
  code.push([pqb.primaryKeyToCode(ast.primaryKey, "t")]);
1979
1989
  }
@@ -1986,10 +1996,18 @@ const createTable = (ast) => {
1986
1996
  pqb.addCode(code, "}));");
1987
1997
  return code;
1988
1998
  };
1999
+ const isTimestamp = (column) => {
2000
+ if (!column)
2001
+ return false;
2002
+ const { default: def } = column.data;
2003
+ return column instanceof pqb.TimestampColumn && !column.data.isNullable && def && typeof def === "object" && pqb.isRaw(def) && def.__raw === "now()";
2004
+ };
1989
2005
 
1990
2006
  const pullDbStructure = async (options, config) => {
1991
- const db = new DbStructure(new pqb.Adapter(options));
2007
+ const adapter = new pqb.Adapter(options);
2008
+ const db = new DbStructure(adapter);
1992
2009
  const ast = await structureToAst(db);
2010
+ await adapter.close();
1993
2011
  const result = astToMigration(ast);
1994
2012
  if (!result)
1995
2013
  return;