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/dist/index.js CHANGED
@@ -293,8 +293,11 @@ const getForeignKeyTable = (fnOrTable) => {
293
293
  const item = new (fnOrTable())();
294
294
  return [item.schema, item.table];
295
295
  };
296
+ const getForeignKeyName = (table, columns) => {
297
+ return `${table}_${columns.join("_")}_fkey`;
298
+ };
296
299
  const constraintToSql = ({ name }, up, foreignKey) => {
297
- const constraintName = foreignKey.options.name || `${name}_${foreignKey.columns.join("_")}_fkey`;
300
+ const constraintName = foreignKey.options.name || getForeignKeyName(name, foreignKey.columns);
298
301
  if (!up) {
299
302
  const { dropMode } = foreignKey.options;
300
303
  return `CONSTRAINT "${constraintName}"${dropMode ? ` ${dropMode}` : ""}`;
@@ -326,13 +329,17 @@ const referencesToSql = (schema, table, columns, foreignKey) => {
326
329
  }
327
330
  return sql.join(" ");
328
331
  };
332
+ const getIndexName = (table, columns) => {
333
+ return `${table}_${columns.map(
334
+ (it) => {
335
+ var _a;
336
+ return "column" in it ? it.column : ((_a = it.expression.match(/\w+/g)) == null ? void 0 : _a.join("_")) || "expression";
337
+ }
338
+ ).join("_")}_idx`;
339
+ };
329
340
  const indexesToQuery = (up, { schema, name }, indexes) => {
330
341
  return indexes.map(({ columns, options }) => {
331
- const indexName = options.name || joinWords(
332
- name,
333
- ...columns.filter((it) => "column" in it).map((it) => it.column),
334
- "index"
335
- );
342
+ const indexName = options.name || getIndexName(name, columns);
336
343
  if (!up) {
337
344
  return {
338
345
  text: `DROP INDEX "${indexName}"${options.dropMode ? ` ${options.dropMode}` : ""}`,
@@ -1846,7 +1853,7 @@ const structureToAst = async (db) => {
1846
1853
  collate: options.collate,
1847
1854
  opclass: options.opclass,
1848
1855
  order: options.order,
1849
- name: index.name,
1856
+ name: index.name !== getIndexName(name, index.columns) ? index.name : void 0,
1850
1857
  using: index.using === "btree" ? void 0 : index.using,
1851
1858
  unique: index.isUnique,
1852
1859
  include: index.include,
@@ -1863,7 +1870,7 @@ const structureToAst = async (db) => {
1863
1870
  foreignKey.foreignTableName,
1864
1871
  foreignKey.foreignColumnNames[0],
1865
1872
  {
1866
- name: foreignKey.name,
1873
+ name: foreignKey.name && foreignKey.name !== getForeignKeyName(name, foreignKey.columnNames) ? foreignKey.name : void 0,
1867
1874
  match: matchMap[foreignKey.match],
1868
1875
  onUpdate: fkeyActionMap[foreignKey.onUpdate],
1869
1876
  onDelete: fkeyActionMap[foreignKey.onDelete]
@@ -1893,7 +1900,7 @@ const structureToAst = async (db) => {
1893
1900
  order: it.order
1894
1901
  })),
1895
1902
  options: {
1896
- name: index.name,
1903
+ name: index.name !== getIndexName(name, index.columns) ? index.name : void 0,
1897
1904
  using: index.using === "btree" ? void 0 : index.using,
1898
1905
  unique: index.isUnique,
1899
1906
  include: index.include,
@@ -1907,7 +1914,7 @@ const structureToAst = async (db) => {
1907
1914
  fnOrTable: it.foreignTableName,
1908
1915
  foreignColumns: it.foreignColumnNames,
1909
1916
  options: {
1910
- name: it.name,
1917
+ name: it.name && it.name !== getForeignKeyName(name, it.columnNames) ? it.name : void 0,
1911
1918
  match: matchMap[it.match],
1912
1919
  onUpdate: fkeyActionMap[it.onUpdate],
1913
1920
  onDelete: fkeyActionMap[it.onDelete]