Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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.
Expand All @@ -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 "
Expand Down
9 changes: 9 additions & 0 deletions src/test/resources/expectedDdlDiff.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

==

14 changes: 14 additions & 0 deletions src/test/resources/newDdl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)


==
13 changes: 13 additions & 0 deletions src/test/resources/originalDdl.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)

==

Loading