Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Exclude the schema name from the table name of INDEX #52

Merged
merged 1 commit into from
Mar 24, 2024
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
2 changes: 1 addition & 1 deletion pkg/ddl/cockroachdb/ddl_index_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type CreateIndexStmt struct {
Unique bool
IfNotExists bool
Name *ObjectName
TableName *ObjectName
TableName *Ident
Using []*Ident
Columns []*ColumnIdent
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/cockroachdb/ddl_index_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCreateIndexStmt_String(t *testing.T) {
Comment: "test comment content",
IfNotExists: true,
Name: &ObjectName{Name: &Ident{Name: "test", QuotationMark: `"`, Raw: `"test"`}},
TableName: &ObjectName{Name: &Ident{Name: "users", QuotationMark: `"`, Raw: `"users"`}},
TableName: &Ident{Name: "users", QuotationMark: `"`, Raw: `"users"`},
Using: []*Ident{{Name: "btree", QuotationMark: ``, Raw: `btree`}},
Columns: []*ColumnIdent{
{
Expand Down
8 changes: 4 additions & 4 deletions pkg/ddl/cockroachdb/diff_create_table.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,8 @@ func DiffCreateTable(before, after *CreateTableStmt, opts ...DiffCreateTableOpti
Schema: after.Name.Schema, //diff:ignore-line-postgres-cockroach
Name: ac.GetName(), //diff:ignore-line-postgres-cockroach
}, //diff:ignore-line-postgres-cockroach
TableName: after.Name, //diff:ignore-line-postgres-cockroach
Columns: ac.Columns, //diff:ignore-line-postgres-cockroach
TableName: after.Name.Name, //diff:ignore-line-postgres-cockroach
Columns: ac.Columns, //diff:ignore-line-postgres-cockroach
}, //diff:ignore-line-postgres-cockroach
) //diff:ignore-line-postgres-cockroach
default: //diff:ignore-line-postgres-cockroach
Expand Down Expand Up @@ -165,8 +165,8 @@ func DiffCreateTable(before, after *CreateTableStmt, opts ...DiffCreateTableOpti
Schema: after.Name.Schema, //diff:ignore-line-postgres-cockroach
Name: ac.GetName(), //diff:ignore-line-postgres-cockroach
}, //diff:ignore-line-postgres-cockroach
TableName: after.Name, //diff:ignore-line-postgres-cockroach
Columns: ac.Columns, //diff:ignore-line-postgres-cockroach
TableName: after.Name.Name, //diff:ignore-line-postgres-cockroach
Columns: ac.Columns, //diff:ignore-line-postgres-cockroach
}) //diff:ignore-line-postgres-cockroach
default: //diff:ignore-line-postgres-cockroach
// ALTER TABLE table_name ADD CONSTRAINT constraint_name constraint;
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/cockroachdb/diff_create_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ ALTER TABLE "public.app_users" ADD CONSTRAINT app_users_pkey PRIMARY KEY ("id");
ALTER TABLE "public.app_users" ADD CONSTRAINT app_users_group_id_fkey FOREIGN KEY (group_id) REFERENCES "groups" ("id");
-- -
-- +UNIQUE INDEX app_users_unique_name (name ASC)
CREATE UNIQUE INDEX public.app_users_unique_name ON "public.app_users" ("name");
CREATE UNIQUE INDEX public.app_users_unique_name ON "app_users" ("name");
-- -
-- +CONSTRAINT app_users_age_check CHECK ("age" >= 0)
ALTER TABLE "public.app_users" ADD CONSTRAINT app_users_age_check CHECK ("age" >= 0);
Expand Down
14 changes: 7 additions & 7 deletions pkg/ddl/cockroachdb/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func TestDiff(t *testing.T) {
Stmts: []Stmt{
&CreateIndexStmt{
Name: &ObjectName{Name: &Ident{Name: "table_name_idx_column_name", Raw: "table_name_idx_column_name"}},
TableName: &ObjectName{Name: &Ident{Name: "table_name", Raw: "table_name"}},
TableName: &Ident{Name: "table_name", Raw: "table_name"},
Columns: []*ColumnIdent{
{
Ident: &Ident{Name: "column_name", Raw: "column_name"},
Expand Down Expand Up @@ -343,7 +343,7 @@ ALTER TABLE public.users ADD COLUMN updated_at TIMESTAMP WITH TIME ZONE NOT NULL
require.NoError(t, err)

expected := `DROP INDEX public.users_idx_by_username;
CREATE INDEX public.users_idx_by_username ON public.users (username ASC);
CREATE INDEX public.users_idx_by_username ON users (username ASC);
`
actual, err := Diff(before, after)
require.NoError(t, err)
Expand All @@ -356,17 +356,17 @@ CREATE INDEX public.users_idx_by_username ON public.users (username ASC);
t.Run("success,before,after,Index", func(t *testing.T) {
t.Parallel()

before, err := NewParser(NewLexer(`CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON public.users (username DESC);`)).Parse()
before, err := NewParser(NewLexer(`CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON users (username DESC);`)).Parse()
require.NoError(t, err)

after, err := NewParser(NewLexer(`CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON public.users (username ASC, age ASC);`)).Parse()
after, err := NewParser(NewLexer(`CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON users (username ASC, age ASC);`)).Parse()
require.NoError(t, err)

expected := `-- -CREATE UNIQUE INDEX public.users_idx_by_username ON public.users (username DESC);
-- +CREATE UNIQUE INDEX public.users_idx_by_username ON public.users (username ASC, age ASC);
expected := `-- -CREATE UNIQUE INDEX public.users_idx_by_username ON users (username DESC);
-- +CREATE UNIQUE INDEX public.users_idx_by_username ON users (username ASC, age ASC);
--
DROP INDEX public.users_idx_by_username;
CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON public.users (username ASC, age ASC);
CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON users (username ASC, age ASC);
`
actual, err := Diff(before, after)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/cockroachdb/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (p *Parser) parseCreateIndexStmt() (*CreateIndexStmt, error) {
return nil, apperr.Errorf(errFmtPrefix+"checkCurrentToken: %w", err)
}

createIndexStmt.TableName = NewObjectName(p.currentToken.Literal.Str)
createIndexStmt.TableName = NewObjectName(p.currentToken.Literal.Str).Name

p.nextToken() // current = USING or (

Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/postgres/ddl_index_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ type CreateIndexStmt struct {
Unique bool
IfNotExists bool
Name *ObjectName
TableName *ObjectName
TableName *Ident
Using []*Ident
Columns []*ColumnIdent
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/postgres/ddl_index_create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func TestCreateIndexStmt_String(t *testing.T) {
Comment: "test comment content",
IfNotExists: true,
Name: &ObjectName{Name: &Ident{Name: "test", QuotationMark: `"`, Raw: `"test"`}},
TableName: &ObjectName{Name: &Ident{Name: "users", QuotationMark: `"`, Raw: `"users"`}},
TableName: &Ident{Name: "users", QuotationMark: `"`, Raw: `"users"`},
Using: []*Ident{{Name: "btree", QuotationMark: ``, Raw: `btree`}},
Columns: []*ColumnIdent{
{
Expand Down
10 changes: 5 additions & 5 deletions pkg/ddl/postgres/diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,17 +330,17 @@ ALTER TABLE public.users ADD COLUMN description TEXT NOT NULL;
t.Run("success,before,after,Index", func(t *testing.T) {
t.Parallel()

before, err := NewParser(NewLexer(`CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON public.users (username);`)).Parse()
before, err := NewParser(NewLexer(`CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON users (username);`)).Parse()
require.NoError(t, err)

after, err := NewParser(NewLexer(`CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON public.users (username, age);`)).Parse()
after, err := NewParser(NewLexer(`CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON users (username, age);`)).Parse()
require.NoError(t, err)

expected := `-- -CREATE UNIQUE INDEX public.users_idx_by_username ON public.users (username);
-- +CREATE UNIQUE INDEX public.users_idx_by_username ON public.users (username, age);
expected := `-- -CREATE UNIQUE INDEX public.users_idx_by_username ON users (username);
-- +CREATE UNIQUE INDEX public.users_idx_by_username ON users (username, age);
--
DROP INDEX public.users_idx_by_username;
CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON public.users (username, age);
CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON users (username, age);
`
actual, err := Diff(before, after)
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion pkg/ddl/postgres/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (p *Parser) parseCreateIndexStmt() (*CreateIndexStmt, error) {
return nil, apperr.Errorf(errFmtPrefix+"checkCurrentToken: %w", err)
}

createIndexStmt.TableName = NewObjectName(p.currentToken.Literal.Str)
createIndexStmt.TableName = NewObjectName(p.currentToken.Literal.Str).Name

p.nextToken() // current = USING or (

Expand Down
Loading