Open ab-pm opened 5 years ago
Sounds good, but I would rather implement it after typescript rewrite ... https://github.com/salsita/node-pg-migrate/pull/502
Cool, good I waited before rushing to the implementation :-) I should find some time to do it this or next week.
This is a follow-up to #467. I've had a use case now where I wanted to create a primary key constraint with a comment, and it's not easily possible. For foreign keys we now have the
referencesConstraintComment
, but adding more options in this scheme would be quite ugly. Also I noticed that constraint options likedeferrable
anddeferred
are placed next to the constraints, not related to the constraint itself.I would recommend a complete redesign, with one object per constraint definition following the SQL data model. My idea:
(maybe more detailed wrt exclude constraints - but for now a simple string should suffice)
I will ignore
NOT NULL
,NULL
,DEFAULT
andIDENTITY
constraints as these seem more like column options, and although technically they seem to be constraints and could be named, that seems to be ignored anyway, and they have their ownALTER COLUMN
syntax.So each column definition would simply get a
constraints
array property similar to what table options already have. For ease of use, we'd get the following shorthands:{check: expression}
becomes{type: 'CHECK', expression}
, same forexclude
.{unique: columns}
becomes{type: 'UNIQUE', columns}
, same forprimaryKey
{references}
becomes{type: 'FOREIGN', references}
type
, an error is thrown - any constraint object can only represent a single constraintFor backwards compatibility (and ease of use), the column definition current format is interpreted as a shorthand as well:
check
,unique
,references
,primaryKey
properties that containConstraint
objects automatically get theirtype
setcheck
,unique
,references
,primaryKey
properties that contain primitives, arrays or Names will get expanded into the respectiveConstraint
objectdeferrable
/deferred
and theRefConstraint
ones get propagated down to all generated/expanded constraint objectsforeignKeys
array would become deprecated, it would be interpreted just like theconstraints
array.What do you think about this? Does it sound reasonable? Are there any backward-compatibility concerns?