-
Notifications
You must be signed in to change notification settings - Fork 695
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: EXPOSED-85 Add support for changing default value in SQL Server #1812
feat: EXPOSED-85 Add support for changing default value in SQL Server #1812
Conversation
affc799
to
6c1e4c1
Compare
de0c2d7
to
308528a
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joc-a The test file UnsignedColumnTypeTests uses Column.modifyStatement()
a few times (which in turn uses modifyColumn(this, ColumnDiff.AllChanged)
).
Could you confirm if those tests are now failing on SQL Server? The tests that use it may need to replace it with a raw exec("""ALTER TABLE ... ALTER COLUMN""")
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm
ecf3c6a
to
618c13d
Compare
618c13d
to
5f5225a
Compare
val defaultValue = column.dbDefaultValue | ||
val isPKColumn = column.table.primaryKey?.columns?.contains(column) == true | ||
|
||
if (column.columnType.nullable || | ||
(defaultValue != null && column.defaultValueFun == null && ! currentDialect.isAllowedAsColumnDefault(defaultValue)) | ||
) { | ||
append(" NULL") | ||
} else if (!isPKColumn) { | ||
append(" NOT NULL") | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This logic was copied from Column.descriptionDdl
function, because this is what modifyColumn
in SQLServerDialect
was using before, when it was invoking super.modifyColumn
:
override fun modifyColumn(column: Column<*>, columnDiff: ColumnDiff): List<String> =
listOf("ALTER TABLE ${TransactionManager.current().identity(column.table)} MODIFY COLUMN ${column.descriptionDdl(true)}")
cad293d
to
1533901
Compare
When creating a column with a DEFAULT value in SQL Server, a DEFAULT constraint is created under the hood. To modify the default value of a column, the constraint has to be dropped first. To do that, the name of the constraint is needed. To be able to obtain its name easily, the way the DEFAULT constraint is created was modified to give it an explicit name with the format DF_TableName_ColumnName. So when adding a new DEFAULT value to a column or modifying an existing one, it's necessary to drop the existing DEFAULT constraint first, and then add a new one.
…e previous version was not including all necessary modifications Every alter statement will be a separate item in the list returned by the modifyColumn function. The statement to alter the default value of a column will combine both the DROP CONSTRAINT and ADD CONSTRAINT statements in one String item, separated by a semicolon. The statement to alter the type and nullability will be part of one String item since that syntax is allowed in SQL Server, but it seems like it's not allowed for adding and dropping constraints.
1533901
to
ed02776
Compare
…JetBrains#1812) * feat: Add support for changing default value in SQL Server When creating a column with a DEFAULT value in SQL Server, a DEFAULT constraint is created under the hood. To modify the default value of a column, the constraint has to be dropped first. To do that, the name of the constraint is needed. To be able to obtain its name easily, the way the DEFAULT constraint is created was modified to give it an explicit name with the format DF_TableName_ColumnName. So when adding a new DEFAULT value to a column or modifying an existing one, it's necessary to drop the existing DEFAULT constraint first, and then add a new one. * fix: Change the way modifyColumn constructs its statements because the previous version was not including all necessary modifications Every alter statement will be a separate item in the list returned by the modifyColumn function. The statement to alter the default value of a column will combine both the DROP CONSTRAINT and ADD CONSTRAINT statements in one String item, separated by a semicolon. The statement to alter the type and nullability will be part of one String item since that syntax is allowed in SQL Server, but it seems like it's not allowed for adding and dropping constraints.
When creating a column with a DEFAULT value in SQL Server, a DEFAULT constraint is created under the hood. To modify the default value of a column, the constraint has to be dropped first. To do that, the name of the constraint is needed. To be able to obtain its name easily, the way the DEFAULT constraint is created was modified to give it an explicit name with the format
DF_TableName_ColumnName
. So when adding a new DEFAULT value to a column or modifying an existing one, it's necessary to drop the existing DEFAULT constraint first (if it exists), and then add a new one.Every alter statement will be a separate item in the list returned by the
modifyColumn
function. The statement to alter the default value of a column will combine both the DROP CONSTRAINT and ADD CONSTRAINT statements in one String item, separated by a semicolon. The statement to alter the type and nullability will be part of one String item.Example:
Expected output:
[ALTER TABLE foo ALTER COLUMN COL INT NULL, ALTER TABLE foo DROP CONSTRAINT IF EXISTS DF_foo_COL; ALTER TABLE foo ADD CONSTRAINT DF_foo_COL DEFAULT 1 for COL]
It's possible to put everything in a single String item without any commas or semicolons. This works:
exec("ALTER TABLE foo ALTER COLUMN COL INT NULL ALTER TABLE foo DROP CONSTRAINT IF EXISTS DF_foo_COL ALTER TABLE foo ADD CONSTRAINT DF_foo_COL DEFAULT 1 for COL")
But I thought it would be neater to separate them.