From eed96ca517f3f41a731d07fd4c0f120d960ffec5 Mon Sep 17 00:00:00 2001 From: Gurminder Singh Date: Sun, 21 Jun 2026 14:02:56 -0700 Subject: [PATCH 1/3] feat: generate default clause when table column NULL is changed --- .../google/cloud/solutions/spannerddl/diff/DdlDiff.java | 3 ++- src/test/resources/expectedDdlDiff.txt | 4 ++++ src/test/resources/newDdl.txt | 7 +++++++ src/test/resources/originalDdl.txt | 7 +++++++ 4 files changed, 20 insertions(+), 1 deletion(-) 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..2806abd 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 @@ -625,7 +625,8 @@ 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())); } // Update options. diff --git a/src/test/resources/expectedDdlDiff.txt b/src/test/resources/expectedDdlDiff.txt index 7027453..e10017f 100644 --- a/src/test/resources/expectedDdlDiff.txt +++ b/src/test/resources/expectedDdlDiff.txt @@ -379,5 +379,9 @@ ALTER TABLE mytable SET OPTIONS (locality_group='lg2') ALTER TABLE mytable SET OPTIONS (droppedKey=NULL,newKey='value2') +== test 75 column not null changed but default same + +ALTER TABLE AlbumsIndex ALTER COLUMN enforcement_enabled BOOL NOT NULL DEFAULT (FALSE) + == diff --git a/src/test/resources/newDdl.txt b/src/test/resources/newDdl.txt index 907fb82..bd51633 100644 --- a/src/test/resources/newDdl.txt +++ b/src/test/resources/newDdl.txt @@ -592,4 +592,11 @@ 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 not null changed but default same + +CREATE TABLE AlbumsIndex ( + id STRING(36), + enforcement_enabled BOOL NOT NULL DEFAULT (FALSE), +) PRIMARY KEY (id) + == diff --git a/src/test/resources/originalDdl.txt b/src/test/resources/originalDdl.txt index caf0af7..2707b82 100644 --- a/src/test/resources/originalDdl.txt +++ b/src/test/resources/originalDdl.txt @@ -591,5 +591,12 @@ 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 not null changed but default same + +CREATE TABLE AlbumsIndex ( + id STRING(36), + enforcement_enabled BOOL DEFAULT (FALSE), +) PRIMARY KEY (id) + == From ffc2b4e97a673edc8002f27b90ccbf193ba9c6ce Mon Sep 17 00:00:00 2001 From: Gurminder Singh Date: Sun, 21 Jun 2026 16:36:12 -0700 Subject: [PATCH 2/3] Incorporated auto review comment --- .../com/google/cloud/solutions/spannerddl/diff/DdlDiff.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) 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 2806abd..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() @@ -627,6 +629,8 @@ private static void addColumnDiffs( columnDiff.rightValue().getColumnTypeString(), (columnDiff.rightValue().isNotNull() ? "NOT NULL" : null), columnDiff.rightValue().getColumnDefaultClause())); + } else { + notNullOrTypeChanged = false; } // Update options. @@ -650,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 " From e172520908b1c1af4c2150b2ddfb9a5fc42eb419 Mon Sep 17 00:00:00 2001 From: Niel Markwick Date: Fri, 26 Jun 2026 21:15:33 +0200 Subject: [PATCH 3/3] Added more test cases --- src/test/resources/expectedDdlDiff.txt | 11 ++++++++--- src/test/resources/newDdl.txt | 13 ++++++++++--- src/test/resources/originalDdl.txt | 12 +++++++++--- 3 files changed, 27 insertions(+), 9 deletions(-) diff --git a/src/test/resources/expectedDdlDiff.txt b/src/test/resources/expectedDdlDiff.txt index e10017f..3e4d663 100644 --- a/src/test/resources/expectedDdlDiff.txt +++ b/src/test/resources/expectedDdlDiff.txt @@ -379,9 +379,14 @@ ALTER TABLE mytable SET OPTIONS (locality_group='lg2') ALTER TABLE mytable SET OPTIONS (droppedKey=NULL,newKey='value2') -== test 75 column not null changed but default same - -ALTER TABLE AlbumsIndex ALTER COLUMN enforcement_enabled BOOL NOT NULL DEFAULT (FALSE) +== 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 bd51633..b977317 100644 --- a/src/test/resources/newDdl.txt +++ b/src/test/resources/newDdl.txt @@ -592,11 +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 not null changed but default same +== test 75 column changing not null with default value -CREATE TABLE AlbumsIndex ( +CREATE TABLE test ( id STRING(36), - enforcement_enabled BOOL NOT NULL DEFAULT (FALSE), + 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 2707b82..a5dced5 100644 --- a/src/test/resources/originalDdl.txt +++ b/src/test/resources/originalDdl.txt @@ -591,11 +591,17 @@ 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 not null changed but default same +== test 75 column changing not null with default value -CREATE TABLE AlbumsIndex ( +CREATE TABLE test ( id STRING(36), - enforcement_enabled BOOL DEFAULT (FALSE), + 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) ==