diff --git a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java index b4dbfd1..d7a6c63 100644 --- a/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java +++ b/src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java @@ -611,11 +611,13 @@ private static void addColumnDiffs( } // Not null or type length limit change. + boolean notNullOrTypeChanged; if (columnDiff.leftValue().isNotNull() != columnDiff.rightValue().isNotNull() || !columnDiff .leftValue() .getColumnTypeString() .equals(columnDiff.rightValue().getColumnTypeString())) { + notNullOrTypeChanged = true; alterStatements.add( Joiner.on(" ") .skipNulls() @@ -625,7 +627,10 @@ private static void addColumnDiffs( "ALTER COLUMN", columnDiff.rightValue().getColumnName(), columnDiff.rightValue().getColumnTypeString(), - (columnDiff.rightValue().isNotNull() ? "NOT NULL" : null))); + (columnDiff.rightValue().isNotNull() ? "NOT NULL" : null), + columnDiff.rightValue().getColumnDefaultClause())); + } else { + notNullOrTypeChanged = false; } // Update options. @@ -649,7 +654,7 @@ private static void addColumnDiffs( columnDiff.leftValue().getColumnDefaultClause(); final ASTcolumn_default_clause newDefaultValue = columnDiff.rightValue().getColumnDefaultClause(); - if (!Objects.equals(oldDefaultValue, newDefaultValue)) { + if (!notNullOrTypeChanged && !Objects.equals(oldDefaultValue, newDefaultValue)) { if (newDefaultValue == null) { alterStatements.add( "ALTER TABLE " diff --git a/src/test/resources/expectedDdlDiff.txt b/src/test/resources/expectedDdlDiff.txt index 7027453..3e4d663 100644 --- a/src/test/resources/expectedDdlDiff.txt +++ b/src/test/resources/expectedDdlDiff.txt @@ -379,5 +379,14 @@ ALTER TABLE mytable SET OPTIONS (locality_group='lg2') ALTER TABLE mytable SET OPTIONS (droppedKey=NULL,newKey='value2') +== test 75 column changing not null with default value +ALTER TABLE test ALTER COLUMN add_nn BOOL NOT NULL DEFAULT (FALSE) +ALTER TABLE test ALTER COLUMN add_both BOOL NOT NULL DEFAULT (FALSE) +ALTER TABLE test ALTER COLUMN remove_NN BOOL DEFAULT (FALSE) +ALTER TABLE test ALTER COLUMN remove_def DROP DEFAULT +ALTER TABLE test ALTER COLUMN remove_both BOOL +ALTER TABLE test ALTER COLUMN change_both BOOL NOT NULL DEFAULT (TRUE) +ALTER TABLE test ALTER COLUMN change_def SET DEFAULT (TRUE) + == diff --git a/src/test/resources/newDdl.txt b/src/test/resources/newDdl.txt index 907fb82..b977317 100644 --- a/src/test/resources/newDdl.txt +++ b/src/test/resources/newDdl.txt @@ -592,4 +592,18 @@ create table mytable (keycol int64) primary key(keycol), OPTIONS(locality_group create table mytable (keycol int64) primary key(keycol), OPTIONS(existingKey='value', newKey='value2') +== test 75 column changing not null with default value + +CREATE TABLE test ( + id STRING(36), + add_nn BOOL NOT NULL DEFAULT (FALSE), + add_both BOOL NOT NULL DEFAULT (FALSE), + remove_NN BOOL DEFAULT(FALSE), + remove_def BOOL NOT NULL, + remove_both BOOL, + change_def BOOL NOT NULL DEFAULT (TRUE), + change_both BOOL NOT NULL DEFAULT (TRUE), +) PRIMARY KEY (id) + + == diff --git a/src/test/resources/originalDdl.txt b/src/test/resources/originalDdl.txt index caf0af7..a5dced5 100644 --- a/src/test/resources/originalDdl.txt +++ b/src/test/resources/originalDdl.txt @@ -591,5 +591,18 @@ create table mytable (keycol int64) primary key(keycol), OPTIONS(locality_group create table mytable (keycol int64) primary key(keycol), OPTIONS(existingKey='value', droppedKey='value1') +== test 75 column changing not null with default value + +CREATE TABLE test ( + id STRING(36), + add_nn BOOL DEFAULT (FALSE), + add_both BOOL, + remove_NN BOOL NOT NULL DEFAULT(FALSE), + remove_def BOOL NOT NULL DEFAULT(FALSE), + remove_both BOOL NOT NULL DEFAULT(FALSE), + change_both BOOL DEFAULT (FALSE), + change_def BOOL NOT NULL DEFAULT (FALSE), +) PRIMARY KEY (id) + ==