Skip to content

Commit dd2996c

Browse files
committed
internal/integration: test mysql auto_increment
Also, fix minor diff issue in AUTO_INCREMENT migration
1 parent 2bb71cc commit dd2996c

File tree

4 files changed

+115
-8
lines changed

4 files changed

+115
-8
lines changed

internal/integration/script_test.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,16 @@ func (t *myTest) cmdCmpShow(ts *testscript.TestScript, _ bool, args []string) {
154154
if err := t.db.QueryRow(fmt.Sprintf("SHOW CREATE TABLE `%s`.`%s`", schema, name)).Scan(&name, &create); err != nil {
155155
return "", err
156156
}
157-
// Trim the "table_options" if it was not requested explicitly.
158-
return create[:strings.LastIndexByte(create, ')')+1], nil
157+
i := strings.LastIndexByte(create, ')')
158+
create, opts := create[:i+1], strings.Fields(create[i+1:])
159+
for _, opt := range opts {
160+
switch strings.Split(opt, "=")[0] {
161+
// Keep only options that are relevant for the tests.
162+
case "AUTO_INCREMENT", "COMMENT":
163+
create += " " + opt
164+
}
165+
}
166+
return create, nil
159167
})
160168
}
161169

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
apply 1.hcl
2+
cmpshow users 1.sql
3+
4+
# Setup a custom AUTO_INCREMENT initial value.
5+
apply 2.hcl
6+
cmpshow users 2.sql
7+
8+
# Increase the AUTO_INCREMENT value.
9+
apply 3.hcl
10+
cmpshow users 3.sql
11+
12+
-- 1.hcl --
13+
schema "$db" {}
14+
15+
table "users" {
16+
schema = schema.$db
17+
column "id" {
18+
null = false
19+
type = bigint
20+
auto_increment = true
21+
}
22+
primary_key {
23+
columns = [column.id]
24+
}
25+
}
26+
27+
-- 1.sql --
28+
CREATE TABLE `users` (
29+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
30+
PRIMARY KEY (`id`)
31+
)
32+
33+
-- 8/1.sql --
34+
CREATE TABLE `users` (
35+
`id` bigint NOT NULL AUTO_INCREMENT,
36+
PRIMARY KEY (`id`)
37+
)
38+
39+
-- 2.hcl --
40+
schema "$db" {}
41+
42+
table "users" {
43+
schema = schema.$db
44+
column "id" {
45+
null = false
46+
type = bigint
47+
auto_increment = true
48+
}
49+
primary_key {
50+
columns = [column.id]
51+
}
52+
auto_increment = 1000
53+
}
54+
55+
-- 2.sql --
56+
CREATE TABLE `users` (
57+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
58+
PRIMARY KEY (`id`)
59+
) AUTO_INCREMENT=1000
60+
61+
-- 8/2.sql --
62+
CREATE TABLE `users` (
63+
`id` bigint NOT NULL AUTO_INCREMENT,
64+
PRIMARY KEY (`id`)
65+
) AUTO_INCREMENT=1000
66+
67+
-- 3.hcl --
68+
schema "$db" {}
69+
70+
table "users" {
71+
schema = schema.$db
72+
column "id" {
73+
null = false
74+
type = bigint
75+
auto_increment = true
76+
}
77+
primary_key {
78+
columns = [column.id]
79+
}
80+
auto_increment = 2000
81+
}
82+
83+
-- 3.sql --
84+
CREATE TABLE `users` (
85+
`id` bigint(20) NOT NULL AUTO_INCREMENT,
86+
PRIMARY KEY (`id`)
87+
) AUTO_INCREMENT=2000
88+
89+
-- 8/3.sql --
90+
CREATE TABLE `users` (
91+
`id` bigint NOT NULL AUTO_INCREMENT,
92+
PRIMARY KEY (`id`)
93+
) AUTO_INCREMENT=2000

sql/mysql/diff.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,16 @@ func (*diff) charsetChange(from, top, to []schema.Attr) schema.Change {
228228
// attribute in case it is not the default.
229229
func (*diff) autoIncChange(from, to []schema.Attr) schema.Change {
230230
var fromA, toA AutoIncrement
231-
// The table is empty and AUTO_INCREMENT was not configured. This can happen
232-
// because older versions of MySQL (< 8.0) stored the AUTO_INCREMENT counter
233-
// in main memory (not persistent), and the value is reset on process restart.
234-
if sqlx.Has(from, &fromA) && sqlx.Has(to, &toA) && fromA.V <= 1 && toA.V > 1 {
231+
switch fromHas, toHas := sqlx.Has(from, &fromA), sqlx.Has(to, &toA); {
232+
// Ignore if the AUTO_INCREMENT attribute was dropped from the desired schema.
233+
case fromHas && !toHas:
234+
// The AUTO_INCREMENT exists in the desired schema, and may not exists in the inspected one.
235+
// This can happen because older versions of MySQL (< 8.0) stored the AUTO_INCREMENT counter
236+
// in main memory (not persistent), and the value is reset on process restart for empty tables.
237+
case toA.V > 1 && toA.V > fromA.V:
238+
// Suggest a diff only if the desired value is greater than the inspected one,
239+
// because this attribute cannot be maintained in users schema and used to set
240+
// up only the initial value.
235241
return &schema.ModifyAttr{
236242
From: &fromA,
237243
To: &toA,

sql/mysql/sqlspec.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,11 @@ func convertTable(spec *sqlspec.Table, parent *schema.Schema) (*schema.Table, er
9292
// MySQL allows setting the initial AUTO_INCREMENT value
9393
// on the table definition.
9494
if attr, ok := spec.Attr("auto_increment"); ok {
95-
b, err := attr.Int64()
95+
v, err := attr.Int64()
9696
if err != nil {
9797
return nil, err
9898
}
99-
t.AddAttrs(&AutoIncrement{V: b})
99+
t.AddAttrs(&AutoIncrement{V: v})
100100
}
101101
return t, err
102102
}

0 commit comments

Comments
 (0)