Skip to content

Commit

Permalink
Merge pull request #54 from murasaki-bv/fix/timestamp-default
Browse files Browse the repository at this point in the history
fix timestamp with default values comparison
  • Loading branch information
daichirata authored Aug 10, 2023
2 parents 3df9ad0 + 28e0515 commit cd53fc6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
9 changes: 7 additions & 2 deletions internal/hammer/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,7 @@ func (g *Generator) generateDDLForColumns(from, to *Table) DDL {

if g.columnTypeEqual(fromCol, toCol) && fromCol.Generated == nil && toCol.Generated == nil {
if fromCol.Type.Base == spansql.Timestamp {
if fromCol.NotNull != toCol.NotNull {
if fromCol.NotNull != toCol.NotNull || !reflect.DeepEqual(fromCol.Default, toCol.Default) {
if !fromCol.NotNull && toCol.NotNull {
ddl.Append(Update{Table: to.Name, Def: toCol})
}
Expand Down Expand Up @@ -611,7 +611,12 @@ func (g *Generator) primaryKeyEqual(x, y *Table) bool {
}

func (g *Generator) columnDefEqual(x, y spansql.ColumnDef) bool {
return cmp.Equal(x, y, cmpopts.IgnoreTypes(spansql.Position{}))
return cmp.Equal(x, y,
cmpopts.IgnoreTypes(spansql.Position{}),
cmp.Comparer(func(x, y spansql.TimestampLiteral) bool {
return time.Time(x).Equal(time.Time(y))
}),
)
}

func (g *Generator) columnTypeEqual(x, y spansql.ColumnDef) bool {
Expand Down
19 changes: 19 additions & 0 deletions internal/hammer/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,25 @@ CREATE CHANGE STREAM SomeStream FOR ALL;
ignoreAlterDatabase: true,
expected: []string{"ALTER CHANGE STREAM SomeStream SET OPTIONS (retention_period='1d', value_capture_type='OLD_AND_NEW_VALUES')"},
},
{
name: "both sides have identical fields of timestamp with a default value",
from: `
CREATE TABLE Nonces (
nonce INT64 NOT NULL,
expires TIMESTAMP NOT NULL DEFAULT(TIMESTAMP '2000-01-01 00:00:00.000000+00:00'),
) PRIMARY KEY(nonce);
`,
to: `
CREATE TABLE Nonces (
nonce INT64 NOT NULL,
expires TIMESTAMP NOT NULL DEFAULT(TIMESTAMP '2000-01-01 12:00:00.000000+00:00'),
) PRIMARY KEY(nonce);
`,
ignoreAlterDatabase: true,
expected: []string{
`ALTER TABLE Nonces ALTER COLUMN expires TIMESTAMP NOT NULL DEFAULT (TIMESTAMP '2000-01-01 12:00:00.000000+00:00')`,
},
},
}
for _, v := range values {
t.Run(v.name, func(t *testing.T) {
Expand Down

0 comments on commit cd53fc6

Please sign in to comment.