Skip to content

Commit 7500553

Browse files
authored
feat: add ShowCreateAllTables() functions (#157)
2 parents 59b4c54 + 7964838 commit 7500553

28 files changed

+394
-114
lines changed

.golangci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ linters:
1010
disable:
1111
- deadcode # deprecated (since v1.49.0) due to: The owner seems to have abandoned the linter. Replaced by unused.
1212
- depguard # unnecessary
13+
- dupl # too many unnecessary detections
1314
- exhaustruct # https://github.com/GaijinEntertainment/go-exhaustruct
1415
- exhaustivestruct # https://github.com/mbilski/exhaustivestruct
1516
- gci # unnecessary

exp/database/sql/ddl/cockroachdb/ddl_index_create.go

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type CreateIndexStmt struct {
1717
IfNotExists bool
1818
Name *ObjectName
1919
TableName *ObjectName
20+
Using []*Ident
2021
Columns []*ColumnIdent
2122
}
2223

@@ -27,8 +28,11 @@ func (s *CreateIndexStmt) GetNameForDiff() string {
2728
func (s *CreateIndexStmt) String() string {
2829
var str string
2930
if s.Comment != "" {
30-
for _, v := range strings.Split(s.Comment, "\n") {
31-
str += CommentPrefix + v + "\n"
31+
comments := strings.Split(s.Comment, "\n")
32+
for i := range comments {
33+
if comments[i] != "" {
34+
str += CommentPrefix + comments[i] + "\n"
35+
}
3236
}
3337
}
3438
str += "CREATE "
@@ -39,7 +43,12 @@ func (s *CreateIndexStmt) String() string {
3943
if s.IfNotExists {
4044
str += "IF NOT EXISTS "
4145
}
42-
str += s.Name.String() + " ON " + s.TableName.String() + " (" + stringz.JoinStringers(", ", s.Columns...) + ");\n"
46+
str += s.Name.String() + " ON " + s.TableName.String()
47+
if len(s.Using) > 0 {
48+
str += " USING "
49+
str += stringz.JoinStringers(" ", s.Using...)
50+
}
51+
str += " (" + stringz.JoinStringers(", ", s.Columns...) + ");\n"
4352
return str
4453
}
4554

@@ -49,10 +58,9 @@ func (s *CreateIndexStmt) StringForDiff() string {
4958
str += "UNIQUE "
5059
}
5160
str += "INDEX "
52-
if s.IfNotExists {
53-
str += "IF NOT EXISTS "
54-
}
55-
str += s.Name.StringForDiff() + " ON " + s.TableName.StringForDiff() + " ("
61+
str += s.Name.StringForDiff() + " ON " + s.TableName.StringForDiff()
62+
// TODO: add USING
63+
str += " ("
5664
for i, c := range s.Columns {
5765
if i > 0 {
5866
str += ", "

exp/database/sql/ddl/cockroachdb/ddl_index_create_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestCreateIndexStmt_String(t *testing.T) {
3131
IfNotExists: true,
3232
Name: &ObjectName{Name: &Ident{Name: "test", QuotationMark: `"`, Raw: `"test"`}},
3333
TableName: &ObjectName{Name: &Ident{Name: "users", QuotationMark: `"`, Raw: `"users"`}},
34+
Using: []*Ident{{Name: "btree", QuotationMark: ``, Raw: `btree`}},
3435
Columns: []*ColumnIdent{
3536
{
3637
Ident: &Ident{Name: "id", QuotationMark: `"`, Raw: `"id"`},
@@ -39,7 +40,7 @@ func TestCreateIndexStmt_String(t *testing.T) {
3940
},
4041
}
4142
expected := `-- test comment content
42-
CREATE INDEX IF NOT EXISTS "test" ON "users" ("id" ASC);
43+
CREATE INDEX IF NOT EXISTS "test" ON "users" USING btree ("id" ASC);
4344
`
4445
actual := stmt.String()
4546

exp/database/sql/ddl/cockroachdb/ddl_index_drop.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ func (s *DropIndexStmt) GetNameForDiff() string {
2323
func (s *DropIndexStmt) String() string {
2424
var str string
2525
if s.Comment != "" {
26-
for _, v := range strings.Split(s.Comment, "\n") {
27-
str += CommentPrefix + v + "\n"
26+
comments := strings.Split(s.Comment, "\n")
27+
for i := range comments {
28+
if comments[i] != "" {
29+
str += CommentPrefix + comments[i] + "\n"
30+
}
2831
}
2932
}
3033
str += "DROP INDEX "

exp/database/sql/ddl/cockroachdb/ddl_table_alter.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ func (s *AlterTableStmt) GetNameForDiff() string {
2727
func (s *AlterTableStmt) String() string {
2828
var str string
2929
if s.Comment != "" {
30-
for _, v := range strings.Split(s.Comment, "\n") {
31-
str += CommentPrefix + v + "\n"
30+
comments := strings.Split(s.Comment, "\n")
31+
for i := range comments {
32+
if comments[i] != "" {
33+
str += CommentPrefix + comments[i] + "\n"
34+
}
3235
}
3336
}
3437
str += "ALTER TABLE "

exp/database/sql/ddl/cockroachdb/ddl_table_create.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,11 @@ func (s *CreateTableStmt) GetNameForDiff() string {
2828
func (s *CreateTableStmt) String() string {
2929
var str string
3030
if s.Comment != "" {
31-
for _, v := range strings.Split(s.Comment, "\n") {
32-
str += CommentPrefix + v + "\n"
31+
comments := strings.Split(s.Comment, "\n")
32+
for i := range comments {
33+
if comments[i] != "" {
34+
str += CommentPrefix + comments[i] + "\n"
35+
}
3336
}
3437
}
3538
str += "CREATE TABLE "

exp/database/sql/ddl/cockroachdb/ddl_table_drop.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@ func (s *DropTableStmt) GetNameForDiff() string {
2323
func (s *DropTableStmt) String() string {
2424
var str string
2525
if s.Comment != "" {
26-
for _, v := range strings.Split(s.Comment, "\n") {
27-
str += CommentPrefix + v + "\n"
26+
comments := strings.Split(s.Comment, "\n")
27+
for i := range comments {
28+
if comments[i] != "" {
29+
str += CommentPrefix + comments[i] + "\n"
30+
}
2831
}
2932
}
3033
str += "DROP TABLE "

exp/database/sql/ddl/cockroachdb/diff.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55

66
errorz "github.com/kunitsucom/util.go/errors"
77
"github.com/kunitsucom/util.go/exp/database/sql/ddl"
8+
"github.com/kunitsucom/util.go/exp/diff/simplediff"
89
)
910

1011
//nolint:funlen,cyclop,gocognit
@@ -83,7 +84,8 @@ func Diff(before, after *DDL) (*DDL, error) {
8384
if beforeStmt.StringForDiff() != afterStmt.StringForDiff() {
8485
result.Stmts = append(result.Stmts,
8586
&DropIndexStmt{
86-
Name: beforeStmt.Name,
87+
Comment: simplediff.Diff(beforeStmt.StringForDiff(), afterStmt.StringForDiff()).String(),
88+
Name: beforeStmt.Name,
8789
},
8890
afterStmt,
8991
)

exp/database/sql/ddl/cockroachdb/diff_create_table_test.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,9 @@ func TestDiffCreateTable(t *testing.T) {
5555

5656
expectedStr := `-- -
5757
-- +"age" INTEGER NOT NULL DEFAULT 0
58-
--
5958
ALTER TABLE "users" ADD COLUMN "age" INTEGER NOT NULL DEFAULT 0;
6059
-- -
6160
-- +CONSTRAINT users_age_check CHECK (age >= 0)
62-
--
6361
ALTER TABLE "users" ADD CONSTRAINT users_age_check CHECK ("age" >= 0);
6462
`
6563

@@ -89,15 +87,12 @@ ALTER TABLE "users" ADD CONSTRAINT users_age_check CHECK ("age" >= 0);
8987

9088
expectedStr := `-- -UNIQUE INDEX users_unique_name (name ASC)
9189
-- +
92-
--
9390
DROP INDEX users_unique_name;
9491
-- -CONSTRAINT users_age_check CHECK (age >= 0)
9592
-- +
96-
--
9793
ALTER TABLE "users" DROP CONSTRAINT users_age_check;
9894
-- -"age" INTEGER NOT NULL DEFAULT 0
9995
-- +
100-
--
10196
ALTER TABLE "users" DROP COLUMN "age";
10297
`
10398

@@ -127,15 +122,12 @@ ALTER TABLE "users" DROP COLUMN "age";
127122

128123
expectedStr := `-- -"name" VARCHAR(255) NOT NULL
129124
-- +"name" TEXT NOT NULL
130-
--
131125
ALTER TABLE "users" ALTER COLUMN "name" SET DATA TYPE TEXT;
132126
-- -"age" INT DEFAULT 0
133127
-- +"age" BIGINT DEFAULT 0
134-
--
135128
ALTER TABLE "users" ALTER COLUMN "age" SET DATA TYPE BIGINT;
136129
-- -
137130
-- +UNIQUE INDEX users_unique_name (name ASC)
138-
--
139131
CREATE UNIQUE INDEX users_unique_name ON "users" ("name");
140132
`
141133

@@ -156,7 +148,6 @@ CREATE UNIQUE INDEX users_unique_name ON "users" ("name");
156148

157149
expectedStr := `-- -"age" INT DEFAULT 0
158150
-- +"age" INT
159-
--
160151
ALTER TABLE "users" ALTER COLUMN "age" DROP DEFAULT;
161152
`
162153

@@ -182,15 +173,12 @@ ALTER TABLE "users" ALTER COLUMN "age" DROP DEFAULT;
182173

183174
expectedStr := `-- -"age" INT
184175
-- +"age" INT DEFAULT 0
185-
--
186176
ALTER TABLE "users" ALTER COLUMN "age" SET DEFAULT 0;
187177
-- -CONSTRAINT users_age_check CHECK (age >= 0)
188178
-- +
189-
--
190179
ALTER TABLE "users" DROP CONSTRAINT users_age_check;
191180
-- -
192181
-- +CONSTRAINT users_age_check CHECK (age <> 0)
193-
--
194182
ALTER TABLE "users" ADD CONSTRAINT users_age_check CHECK ("age" <> 0);
195183
`
196184

@@ -218,39 +206,30 @@ ALTER TABLE "users" ADD CONSTRAINT users_age_check CHECK ("age" <> 0);
218206

219207
expectedStr := `-- -public.users
220208
-- +public.app_users
221-
--
222209
ALTER TABLE "public.users" RENAME TO "public.app_users";
223210
-- -CONSTRAINT users_pkey PRIMARY KEY (id ASC)
224211
-- +
225-
--
226212
ALTER TABLE "public.app_users" DROP CONSTRAINT users_pkey;
227213
-- -CONSTRAINT users_group_id_fkey FOREIGN KEY (group_id ASC) REFERENCES groups (id ASC)
228214
-- +
229-
--
230215
ALTER TABLE "public.app_users" DROP CONSTRAINT users_group_id_fkey;
231216
-- -UNIQUE INDEX users_unique_name (name ASC)
232217
-- +
233-
--
234218
DROP INDEX public.users_unique_name;
235219
-- -CONSTRAINT users_age_check CHECK (age >= 0)
236220
-- +
237-
--
238221
ALTER TABLE "public.app_users" DROP CONSTRAINT users_age_check;
239222
-- -
240223
-- +CONSTRAINT app_users_pkey PRIMARY KEY (id ASC)
241-
--
242224
ALTER TABLE "public.app_users" ADD CONSTRAINT app_users_pkey PRIMARY KEY ("id");
243225
-- -
244226
-- +CONSTRAINT app_users_group_id_fkey FOREIGN KEY (group_id ASC) REFERENCES groups (id ASC)
245-
--
246227
ALTER TABLE "public.app_users" ADD CONSTRAINT app_users_group_id_fkey FOREIGN KEY (group_id) REFERENCES "groups" ("id");
247228
-- -
248229
-- +UNIQUE INDEX app_users_unique_name (name ASC)
249-
--
250230
CREATE UNIQUE INDEX public.app_users_unique_name ON "public.app_users" ("name");
251231
-- -
252232
-- +CONSTRAINT app_users_age_check CHECK (age >= 0)
253-
--
254233
ALTER TABLE "public.app_users" ADD CONSTRAINT app_users_age_check CHECK ("age" >= 0);
255234
`
256235

@@ -279,7 +258,6 @@ ALTER TABLE "public.app_users" ADD CONSTRAINT app_users_age_check CHECK ("age" >
279258

280259
expectedStr := `-- -"age" INT DEFAULT 0
281260
-- +"age" INTEGER NOT NULL DEFAULT 0
282-
--
283261
ALTER TABLE "users" ALTER COLUMN "age" SET NOT NULL;
284262
`
285263

@@ -307,7 +285,6 @@ ALTER TABLE "users" ALTER COLUMN "age" SET NOT NULL;
307285

308286
expectedStr := `-- -"age" INT NOT NULL DEFAULT 0
309287
-- +"age" INT DEFAULT 0
310-
--
311288
ALTER TABLE "users" ALTER COLUMN "age" DROP NOT NULL;
312289
`
313290

@@ -335,11 +312,9 @@ ALTER TABLE "users" ALTER COLUMN "age" DROP NOT NULL;
335312

336313
expectedStr := `-- -CONSTRAINT users_pkey PRIMARY KEY (id ASC)
337314
-- +
338-
--
339315
ALTER TABLE "users" DROP CONSTRAINT users_pkey;
340316
-- -
341317
-- +CONSTRAINT users_pkey PRIMARY KEY (id ASC, name ASC)
342-
--
343318
ALTER TABLE "users" ADD CONSTRAINT users_pkey PRIMARY KEY ("id", name);
344319
`
345320

@@ -367,11 +342,9 @@ ALTER TABLE "users" ADD CONSTRAINT users_pkey PRIMARY KEY ("id", name);
367342

368343
expectedStr := `-- -CONSTRAINT users_group_id_fkey FOREIGN KEY (group_id ASC) REFERENCES groups (id ASC)
369344
-- +
370-
--
371345
ALTER TABLE "users" DROP CONSTRAINT users_group_id_fkey;
372346
-- -
373347
-- +CONSTRAINT users_group_id_fkey FOREIGN KEY (group_id ASC, name ASC) REFERENCES groups (id ASC, name ASC)
374-
--
375348
ALTER TABLE "users" ADD CONSTRAINT users_group_id_fkey FOREIGN KEY (group_id, name) REFERENCES "groups" ("id", name);
376349
`
377350

@@ -425,7 +398,6 @@ CREATE UNIQUE INDEX users_unique_name ON "users" ("id" ASC, name ASC);
425398

426399
expectedStr := `-- -"age" INT NOT NULL DEFAULT 0
427400
-- +"age" INT NOT NULL DEFAULT ((0 + 3) - 1 * 4 / 2)
428-
--
429401
ALTER TABLE "users" ALTER COLUMN "age" SET DEFAULT ((0 + 3) - 1 * 4 / 2);
430402
`
431403

@@ -473,7 +445,6 @@ ALTER TABLE "users" ALTER COLUMN "age" SET DEFAULT ((0 + 3) - 1 * 4 / 2);
473445

474446
expectedStr := `-- -unique_code TEXT
475447
-- +unique_code TEXT DEFAULT 'CODE-' || TO_CHAR(NOW(), 'YYYYMMDDHH24MISS') || '-' || LPAD(TO_CHAR(NEXTVAL('seq_complex_default')), 5, '0')
476-
--
477448
ALTER TABLE complex_defaults ALTER COLUMN unique_code SET DEFAULT 'CODE-' || TO_CHAR(NOW(), 'YYYYMMDDHH24MISS') || '-' || LPAD(TO_CHAR(NEXTVAL('seq_complex_default')), 5, '0');
478449
`
479450

@@ -499,7 +470,6 @@ ALTER TABLE complex_defaults ALTER COLUMN unique_code SET DEFAULT 'CODE-' || TO_
499470

500471
expected := `-- -
501472
-- +CONSTRAINT users_age_check CHECK (age >= 0)
502-
--
503473
ALTER TABLE "users" ADD CONSTRAINT users_age_check CHECK ("age" >= 0) NOT VALID;
504474
`
505475
actual, err := DiffCreateTable(

exp/database/sql/ddl/cockroachdb/diff_test.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ func TestDiff(t *testing.T) {
303303

304304
expected := `-- -
305305
-- +updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT timezone('UTC':::STRING, current_timestamp():::TIMESTAMPTZ)
306-
--
307306
ALTER TABLE public.users ADD COLUMN updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT timezone('UTC':::STRING, current_timestamp():::TIMESTAMPTZ);
308307
`
309308
actual, err := Diff(before, after)
@@ -362,7 +361,10 @@ CREATE INDEX public.users_idx_by_username ON public.users (username ASC);
362361
after, err := NewParser(NewLexer(`CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON public.users (username ASC, age ASC);`)).Parse()
363362
require.NoError(t, err)
364363

365-
expected := `DROP INDEX public.users_idx_by_username;
364+
expected := `-- -CREATE UNIQUE INDEX public.users_idx_by_username ON public.users (username DESC);
365+
-- +CREATE UNIQUE INDEX public.users_idx_by_username ON public.users (username ASC, age ASC);
366+
--
367+
DROP INDEX public.users_idx_by_username;
366368
CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON public.users (username ASC, age ASC);
367369
`
368370
actual, err := Diff(before, after)
@@ -385,7 +387,6 @@ CREATE UNIQUE INDEX IF NOT EXISTS public.users_idx_by_username ON public.users (
385387

386388
expected := `-- -username VARCHAR(10) NOT NULL
387389
-- +username VARCHAR(11) NOT NULL
388-
--
389390
ALTER TABLE public.users ALTER COLUMN username SET DATA TYPE VARCHAR(11);
390391
`
391392
actual, err := Diff(before, after)

0 commit comments

Comments
 (0)