Skip to content

Commit cd1dabf

Browse files
gurminder71nielm
andauthored
feat: generate default clause when table column NULL is changed (#250)
If a column with a DEFAULT value has its NOT NULL clause added or removed, the DEFAULT clause also needs to be in the ALTER statement otherwise the DEFAULT clause is dropped. Co-authored-by: Niel Markwick <nielm@google.com>
1 parent 596afc1 commit cd1dabf

4 files changed

Lines changed: 43 additions & 2 deletions

File tree

src/main/java/com/google/cloud/solutions/spannerddl/diff/DdlDiff.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -611,11 +611,13 @@ private static void addColumnDiffs(
611611
}
612612

613613
// Not null or type length limit change.
614+
boolean notNullOrTypeChanged;
614615
if (columnDiff.leftValue().isNotNull() != columnDiff.rightValue().isNotNull()
615616
|| !columnDiff
616617
.leftValue()
617618
.getColumnTypeString()
618619
.equals(columnDiff.rightValue().getColumnTypeString())) {
620+
notNullOrTypeChanged = true;
619621
alterStatements.add(
620622
Joiner.on(" ")
621623
.skipNulls()
@@ -625,7 +627,10 @@ private static void addColumnDiffs(
625627
"ALTER COLUMN",
626628
columnDiff.rightValue().getColumnName(),
627629
columnDiff.rightValue().getColumnTypeString(),
628-
(columnDiff.rightValue().isNotNull() ? "NOT NULL" : null)));
630+
(columnDiff.rightValue().isNotNull() ? "NOT NULL" : null),
631+
columnDiff.rightValue().getColumnDefaultClause()));
632+
} else {
633+
notNullOrTypeChanged = false;
629634
}
630635

631636
// Update options.
@@ -649,7 +654,7 @@ private static void addColumnDiffs(
649654
columnDiff.leftValue().getColumnDefaultClause();
650655
final ASTcolumn_default_clause newDefaultValue =
651656
columnDiff.rightValue().getColumnDefaultClause();
652-
if (!Objects.equals(oldDefaultValue, newDefaultValue)) {
657+
if (!notNullOrTypeChanged && !Objects.equals(oldDefaultValue, newDefaultValue)) {
653658
if (newDefaultValue == null) {
654659
alterStatements.add(
655660
"ALTER TABLE "

src/test/resources/expectedDdlDiff.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,5 +379,14 @@ ALTER TABLE mytable SET OPTIONS (locality_group='lg2')
379379

380380
ALTER TABLE mytable SET OPTIONS (droppedKey=NULL,newKey='value2')
381381

382+
== test 75 column changing not null with default value
383+
ALTER TABLE test ALTER COLUMN add_nn BOOL NOT NULL DEFAULT (FALSE)
384+
ALTER TABLE test ALTER COLUMN add_both BOOL NOT NULL DEFAULT (FALSE)
385+
ALTER TABLE test ALTER COLUMN remove_NN BOOL DEFAULT (FALSE)
386+
ALTER TABLE test ALTER COLUMN remove_def DROP DEFAULT
387+
ALTER TABLE test ALTER COLUMN remove_both BOOL
388+
ALTER TABLE test ALTER COLUMN change_both BOOL NOT NULL DEFAULT (TRUE)
389+
ALTER TABLE test ALTER COLUMN change_def SET DEFAULT (TRUE)
390+
382391
==
383392

src/test/resources/newDdl.txt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -592,4 +592,18 @@ create table mytable (keycol int64) primary key(keycol), OPTIONS(locality_group
592592

593593
create table mytable (keycol int64) primary key(keycol), OPTIONS(existingKey='value', newKey='value2')
594594

595+
== test 75 column changing not null with default value
596+
597+
CREATE TABLE test (
598+
id STRING(36),
599+
add_nn BOOL NOT NULL DEFAULT (FALSE),
600+
add_both BOOL NOT NULL DEFAULT (FALSE),
601+
remove_NN BOOL DEFAULT(FALSE),
602+
remove_def BOOL NOT NULL,
603+
remove_both BOOL,
604+
change_def BOOL NOT NULL DEFAULT (TRUE),
605+
change_both BOOL NOT NULL DEFAULT (TRUE),
606+
) PRIMARY KEY (id)
607+
608+
595609
==

src/test/resources/originalDdl.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,5 +591,18 @@ create table mytable (keycol int64) primary key(keycol), OPTIONS(locality_group
591591

592592
create table mytable (keycol int64) primary key(keycol), OPTIONS(existingKey='value', droppedKey='value1')
593593

594+
== test 75 column changing not null with default value
595+
596+
CREATE TABLE test (
597+
id STRING(36),
598+
add_nn BOOL DEFAULT (FALSE),
599+
add_both BOOL,
600+
remove_NN BOOL NOT NULL DEFAULT(FALSE),
601+
remove_def BOOL NOT NULL DEFAULT(FALSE),
602+
remove_both BOOL NOT NULL DEFAULT(FALSE),
603+
change_both BOOL DEFAULT (FALSE),
604+
change_def BOOL NOT NULL DEFAULT (FALSE),
605+
) PRIMARY KEY (id)
606+
594607
==
595608

0 commit comments

Comments
 (0)