rake-db 2.3.2 → 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,11 @@
1
1
  # rake-db
2
2
 
3
+ ## 2.3.3
4
+
5
+ ### Patch Changes
6
+
7
+ - Improve pullindg db structure
8
+
3
9
  ## 2.3.2
4
10
 
5
11
  ### Patch Changes
package/dist/index.esm.js CHANGED
@@ -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}` : ""}`,
@@ -1837,7 +1844,7 @@ const structureToAst = async (db) => {
1837
1844
  collate: options.collate,
1838
1845
  opclass: options.opclass,
1839
1846
  order: options.order,
1840
- name: index.name,
1847
+ name: index.name !== getIndexName(name, index.columns) ? index.name : void 0,
1841
1848
  using: index.using === "btree" ? void 0 : index.using,
1842
1849
  unique: index.isUnique,
1843
1850
  include: index.include,
@@ -1854,7 +1861,7 @@ const structureToAst = async (db) => {
1854
1861
  foreignKey.foreignTableName,
1855
1862
  foreignKey.foreignColumnNames[0],
1856
1863
  {
1857
- name: foreignKey.name,
1864
+ name: foreignKey.name && foreignKey.name !== getForeignKeyName(name, foreignKey.columnNames) ? foreignKey.name : void 0,
1858
1865
  match: matchMap[foreignKey.match],
1859
1866
  onUpdate: fkeyActionMap[foreignKey.onUpdate],
1860
1867
  onDelete: fkeyActionMap[foreignKey.onDelete]
@@ -1884,7 +1891,7 @@ const structureToAst = async (db) => {
1884
1891
  order: it.order
1885
1892
  })),
1886
1893
  options: {
1887
- name: index.name,
1894
+ name: index.name !== getIndexName(name, index.columns) ? index.name : void 0,
1888
1895
  using: index.using === "btree" ? void 0 : index.using,
1889
1896
  unique: index.isUnique,
1890
1897
  include: index.include,
@@ -1898,7 +1905,7 @@ const structureToAst = async (db) => {
1898
1905
  fnOrTable: it.foreignTableName,
1899
1906
  foreignColumns: it.foreignColumnNames,
1900
1907
  options: {
1901
- name: it.name,
1908
+ name: it.name && it.name !== getForeignKeyName(name, it.columnNames) ? it.name : void 0,
1902
1909
  match: matchMap[it.match],
1903
1910
  onUpdate: fkeyActionMap[it.onUpdate],
1904
1911
  onDelete: fkeyActionMap[it.onDelete]