Skip to content

Commit

Permalink
internal/integration: test mysql auto_increment
Browse files Browse the repository at this point in the history
Also, fix minor diff issue in AUTO_INCREMENT migration
  • Loading branch information
a8m committed Feb 12, 2022
1 parent 3b6920d commit 30bd629
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 8 deletions.
12 changes: 10 additions & 2 deletions internal/integration/script_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,16 @@ func (t *myTest) cmdCmpShow(ts *testscript.TestScript, _ bool, args []string) {
if err := t.db.QueryRow(fmt.Sprintf("SHOW CREATE TABLE `%s`.`%s`", schema, name)).Scan(&name, &create); err != nil {
return "", err
}
// Trim the "table_options" if it was not requested explicitly.
return create[:strings.LastIndexByte(create, ')')+1], nil
i := strings.LastIndexByte(create, ')')
create, opts := create[:i+1], strings.Fields(create[i+1:])
for _, opt := range opts {
switch strings.Split(opt, "=")[0] {
// Keep only options that are relevant for the tests.
case "AUTO_INCREMENT", "COMMENT":
create += " " + opt
}
}
return create, nil
})
}

Expand Down
93 changes: 93 additions & 0 deletions internal/integration/testdata/mysql/autoincrement.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
apply 1.hcl
cmpshow users 1.sql

# Setup a custom AUTO_INCREMENT initial value.
apply 2.hcl
cmpshow users 2.sql

# Increase the AUTO_INCREMENT value.
apply 3.hcl
cmpshow users 3.sql

-- 1.hcl --
schema "$db" {}

table "users" {
schema = schema.$db
column "id" {
null = false
type = bigint
auto_increment = true
}
primary_key {
columns = [column.id]
}
}

-- 1.sql --
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
)

-- 8/1.sql --
CREATE TABLE `users` (
`id` bigint NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
)

-- 2.hcl --
schema "$db" {}

table "users" {
schema = schema.$db
column "id" {
null = false
type = bigint
auto_increment = true
}
primary_key {
columns = [column.id]
}
auto_increment = 1000
}

-- 2.sql --
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) AUTO_INCREMENT=1000

-- 8/2.sql --
CREATE TABLE `users` (
`id` bigint NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) AUTO_INCREMENT=1000

-- 3.hcl --
schema "$db" {}

table "users" {
schema = schema.$db
column "id" {
null = false
type = bigint
auto_increment = true
}
primary_key {
columns = [column.id]
}
auto_increment = 2000
}

-- 3.sql --
CREATE TABLE `users` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) AUTO_INCREMENT=2000

-- 8/3.sql --
CREATE TABLE `users` (
`id` bigint NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) AUTO_INCREMENT=2000
14 changes: 10 additions & 4 deletions sql/mysql/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,16 @@ func (*diff) charsetChange(from, top, to []schema.Attr) schema.Change {
// attribute in case it is not the default.
func (*diff) autoIncChange(from, to []schema.Attr) schema.Change {
var fromA, toA AutoIncrement
// The table is empty and AUTO_INCREMENT was not configured. This can happen
// because older versions of MySQL (< 8.0) stored the AUTO_INCREMENT counter
// in main memory (not persistent), and the value is reset on process restart.
if sqlx.Has(from, &fromA) && sqlx.Has(to, &toA) && fromA.V <= 1 && toA.V > 1 {
switch fromHas, toHas := sqlx.Has(from, &fromA), sqlx.Has(to, &toA); {
// Ignore if the AUTO_INCREMENT attribute was dropped from the desired schema.
case fromHas && !toHas:
// The AUTO_INCREMENT exists in the desired schema, and may not exists in the inspected one.
// This can happen because older versions of MySQL (< 8.0) stored the AUTO_INCREMENT counter
// in main memory (not persistent), and the value is reset on process restart for empty tables.
case toA.V > 1 && toA.V > fromA.V:
// Suggest a diff only if the desired value is greater than the inspected one,
// because this attribute cannot be maintained in users schema and used to set
// up only the initial value.
return &schema.ModifyAttr{
From: &fromA,
To: &toA,
Expand Down
4 changes: 2 additions & 2 deletions sql/mysql/sqlspec.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,11 @@ func convertTable(spec *sqlspec.Table, parent *schema.Schema) (*schema.Table, er
// MySQL allows setting the initial AUTO_INCREMENT value
// on the table definition.
if attr, ok := spec.Attr("auto_increment"); ok {
b, err := attr.Int64()
v, err := attr.Int64()
if err != nil {
return nil, err
}
t.AddAttrs(&AutoIncrement{V: b})
t.AddAttrs(&AutoIncrement{V: v})
}
return t, err
}
Expand Down

0 comments on commit 30bd629

Please sign in to comment.