Skip to content

Commit

Permalink
fix(pkg/ddl/spanner): fix PRIMARY KEY alter logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ginokent committed Jan 15, 2024
1 parent b408322 commit 6485116
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 29 deletions.
13 changes: 9 additions & 4 deletions pkg/ddl/spanner/diff_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,15 @@ func DiffCreateTable(before, after *CreateTableStmt, opts ...DiffCreateTableOpti
Name: before.Name,
})
return result, nil
case before.Options.StringForDiff() != after.Options.StringForDiff():
result.Stmts = append(result.Stmts,
&DropTableStmt{
Comment: simplediff.Diff(before.Options.String(), after.Options.String()).String(),
Name: before.Name,
},
after,
)
return result, nil
case (before == nil && after == nil) || reflect.DeepEqual(before, after) || before.String() == after.String():
return nil, apperr.Errorf("before: %s, after: %s: %w", before.GetNameForDiff(), after.GetNameForDiff(), ddl.ErrNoDifference)
}
Expand Down Expand Up @@ -129,10 +138,6 @@ func DiffCreateTable(before, after *CreateTableStmt, opts ...DiffCreateTableOpti
})
}

if before.Options.StringForDiff() != after.Options.StringForDiff() {
return nil, apperr.Errorf("before: %s, after: %s: %w", before.Options, after.Options, ddl.ErrAlterOptionNotSupported)
}

if len(result.Stmts) == 0 {
return nil, apperr.Errorf("before: %s, after: %s: %w", before.GetNameForDiff(), after.GetNameForDiff(), ddl.ErrNoDifference)
}
Expand Down
42 changes: 17 additions & 25 deletions pkg/ddl/spanner/diff_create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ ALTER TABLE "users" ALTER COLUMN "age" INT64;
t.Logf("✅: %s: actual: %%#v:\n%#v", t.Name(), actual)
})

t.Run("failure,ALTER_PRIMARY_KEY", func(t *testing.T) {
t.Run("success,ALTER_PRIMARY_KEY", func(t *testing.T) {
t.Parallel()

before := `CREATE TABLE "users" (id STRING(36) NOT NULL, group_id STRING(36) NOT NULL REFERENCES "groups" ("id"), "name" STRING(255) NOT NULL, "age" INT64 DEFAULT 0 NOT NULL CHECK ("age" >= 0), description STRING) PRIMARY KEY ("id");`
Expand All @@ -320,8 +320,22 @@ ALTER TABLE "users" ALTER COLUMN "age" INT64;
afterDDL.Stmts[0].(*CreateTableStmt),
DiffCreateTableUseAlterTableAddConstraintNotValid(false),
)
assert.ErrorIs(t, err, ddl.ErrAlterOptionNotSupported)
assert.Nil(t, actual)
assert.NoError(t, err)
expected := `-- -PRIMARY KEY ("id")
-- +PRIMARY KEY ("id", name)
DROP TABLE "users";
CREATE TABLE "users" (
id STRING(36) NOT NULL,
group_id STRING(36) NOT NULL,
"name" STRING(255) NOT NULL,
"age" INT64 NOT NULL DEFAULT 0,
description STRING,
CONSTRAINT users_group_id_fkey FOREIGN KEY (group_id) REFERENCES "groups" ("id"),
CONSTRAINT users_age_check CHECK ("age" >= 0)
) PRIMARY KEY ("id", name);
`

assert.Equal(t, expected, actual.String())
})

t.Run("success,DROP_ADD_FOREIGN_KEY", func(t *testing.T) {
Expand Down Expand Up @@ -544,26 +558,4 @@ ALTER TABLE "users" ADD CONSTRAINT users_age_check CHECK ("age" >= 0) NOT VALID;

t.Logf("✅: %s:\n%s", t.Name(), ddls)
})

t.Run("success,NoAsc", func(t *testing.T) {
t.Parallel()

beforeDDL, err := NewParser(NewLexer(`CREATE TABLE "users" (id STRING(36) NOT NULL) PRIMARY KEY ("id" ASC);`)).Parse()
require.NoError(t, err)

afterDDL, err := NewParser(NewLexer(`CREATE TABLE "users" (id STRING(36) NOT NULL) PRIMARY KEY ("id");`)).Parse()
require.NoError(t, err)

actual, err := DiffCreateTable(
beforeDDL.Stmts[0].(*CreateTableStmt),
afterDDL.Stmts[0].(*CreateTableStmt),
DiffCreateTableUseAlterTableAddConstraintNotValid(false),
)

assert.ErrorIs(t, err, ddl.ErrAlterOptionNotSupported)
assert.Nil(t, actual)

t.Logf("✅: %s: actual: %%#v: \n%#v", t.Name(), actual)
t.Logf("✅: %s: actual: %%s: \n%s", t.Name(), actual)
})
}

0 comments on commit 6485116

Please sign in to comment.