rake-db 2.23.13 → 2.23.14

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
@@ -207,10 +207,11 @@ const getForeignKeyTable = (fnOrTable) => {
207
207
  };
208
208
  const getConstraintName = (table, constraint, snakeCase) => {
209
209
  if (constraint.references) {
210
- const { columns } = constraint.references;
211
- return `${table}_${(snakeCase ? columns.map(orchidCore.toSnakeCase) : columns).join(
212
- "_"
213
- )}_fkey`;
210
+ let { columns } = constraint.references;
211
+ if (snakeCase) {
212
+ columns = columns.map(orchidCore.toSnakeCase);
213
+ }
214
+ return makeConstraintName(table, columns, "fkey");
214
215
  }
215
216
  if (constraint.check) return `${table}_check`;
216
217
  if (constraint.identity) return `${table}_identity`;
@@ -258,8 +259,38 @@ const referencesToSql = (references, snakeCase) => {
258
259
  }
259
260
  return sql.join(" ");
260
261
  };
262
+ const MAX_CONSTRAINT_NAME_LEN = 63;
263
+ const makeConstraintName = (table, columns, suffix) => {
264
+ const long = `${table}_${columns.join("_")}_${suffix}`;
265
+ if (long.length <= MAX_CONSTRAINT_NAME_LEN) return long;
266
+ for (let partLen = 3; partLen > 0; partLen--) {
267
+ const shorter = `${orchidCore.toCamelCase(
268
+ orchidCore.toSnakeCase(table).split("_").map((p) => p.slice(0, partLen)).join("_")
269
+ )}_${columns.map(
270
+ (c) => orchidCore.toCamelCase(
271
+ c.split("_").map((p) => p.slice(0, partLen)).join("_")
272
+ )
273
+ ).join("_")}_${suffix}`;
274
+ if (shorter.length <= MAX_CONSTRAINT_NAME_LEN) return shorter;
275
+ }
276
+ const short = `${table}_${columns.length}columns_${suffix}`;
277
+ if (short.length <= MAX_CONSTRAINT_NAME_LEN) return short;
278
+ for (let partLen = 3; partLen > 0; partLen--) {
279
+ const short2 = `${orchidCore.toCamelCase(
280
+ orchidCore.toSnakeCase(table).split("_").map((p) => p.slice(0, partLen)).join("_")
281
+ )}_${columns.length}columns_${suffix}`;
282
+ if (short2.length <= MAX_CONSTRAINT_NAME_LEN) return short2;
283
+ }
284
+ return `long_ass_table_${suffix}`;
285
+ };
261
286
  const getIndexName = (table, columns) => {
262
- return `${table}_${columns.map((it) => "column" in it ? it.column : "expression").join("_")}_idx`;
287
+ return makeConstraintName(
288
+ table,
289
+ columns.map(
290
+ (it) => "column" in it ? it.column : "expression"
291
+ ),
292
+ "idx"
293
+ );
263
294
  };
264
295
  const indexesToQuery = (up, { schema, name: tableName }, indexes, snakeCase, language) => {
265
296
  return indexes.map(({ columns, options, name }) => {