martinjw/dbschemareader

MSSQL: Add option to add a name for implicit default constraints

Arnagos opened this issue · 1 comments

Currently, the SQL generated for implicit default constraints is something like this:
ALTER TABLE TableName ADD NewColumn Type NOT NULL DEFAULT (Value)

This creates an anonym default constraint in MSSQL, which makes the management of the constraint very unpleasant and forces you to reload the schema from the DB to get the generated name.

Instead, I'd like to just set the name it uses for the constraint like this:
ALTER TABLE TableName ADD NewColumn Type NOT NULL CONSTRAINT ConstraintName DEFAULT (Value). This way it's possible to add the constraint to the DatabaseTable instance as well. Currently, it's not possible to add a new column with default x and then replace the default value with y for future inserts without reloading the schema.

Note: I'm using IMigrationGenerator.AddColumn() but AddTable() would also be nice.

Another note: It's not really necessary that I can define the name of the constraint. It just needs a name and has to be added to the table so that further operations with the default are possible. So a default DF_TableName_ColumnName is perfectly fine.

Normally I would just write my own method that just minimally changes your code but all the utils, etc. are marked as internal, so I can't use them.

I just ran into the problem in Oracle. You'll need to define a check constraint which has the expression 'NewColumn IS NOT NULL', which is what the automatic constraint will look like anyway. The AddColumn migration checks the CheckConstraints for this pattern when it's not nullable- when it's a match, he constraint is named in the ALTER TABLE x ADD y