Skip to content

Commit

Permalink
Add hide_auto_increment option to the entire AUTO_INCREMENT clause
Browse files Browse the repository at this point in the history
Fix #273
  • Loading branch information
k1LoW committed Jan 16, 2021
1 parent 8903026 commit 4bef36e
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 7 deletions.
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ BUILD_LDFLAGS = -X $(PKG).commit=$(COMMIT) -X $(PKG).date=$(DATE)

default: test

ci: depsdev build db test testdoc test_too_many_tables test_json test_ext_subcommand sec doc
ci: depsdev build db test testdoc testdoc_hide_auto_increment test_too_many_tables test_json test_ext_subcommand sec doc

ci_windows: depsdev build db_sqlite testdoc_sqlite

Expand Down Expand Up @@ -77,6 +77,13 @@ testdoc: testdoc_sqlite
testdoc_sqlite:
./tbls diff sq://$(PWD)/testdata/testdb.sqlite3 -c testdata/test_tbls.yml sample/sqlite

testdoc_hide_auto_increment:
usql my://root:mypass@localhost:33308/testdb -c "CREATE DATABASE IF NOT EXISTS auto_increment;"
usql my://root:mypass@localhost:33308/auto_increment -f testdata/ddl/auto_increment.sql
./tbls doc my://root:mypass@localhost:33308/auto_increment?hide_auto_increment -f $(TMPDIR)/auto_increment
usql my://root:mypass@localhost:33308/auto_increment -c "INSERT INTO users (username, password, email, created) VALUES ('alice', 'PASS', '[email protected]', now());"
./tbls diff my://root:mypass@localhost:33308/auto_increment?hide_auto_increment $(TMPDIR)/auto_increment

test_too_many_tables:
usql pg://postgres:pgpass@localhost:55432/testdb?sslmode=disable -c "DROP DATABASE IF EXISTS too_many;CREATE DATABASE too_many;"
usql pg://postgres:pgpass@localhost:55432/too_many?sslmode=disable -f testdata/ddl/createtable_too_many.sql
Expand Down
4 changes: 4 additions & 0 deletions datasource/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func Analyze(dsn config.DSN) (*schema.Schema, error) {
opts = append(opts, mysql.ShowAutoIcrrement())
values.Del(k)
}
if k == "hide_auto_increment" {
opts = append(opts, mysql.HideAutoIcrrement())
values.Del(k)
}
}
u.RawQuery = values.Encode()
urlstr = u.String()
Expand Down
31 changes: 25 additions & 6 deletions drivers/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,19 @@ import (
)

var reFK = regexp.MustCompile(`FOREIGN KEY \((.+)\) REFERENCES ([^\s]+)\s?\((.+)\)`)
var reAI = regexp.MustCompile(`AUTO_INCREMENT=[\d]+`)
var reAI = regexp.MustCompile(` AUTO_INCREMENT=[\d]+`)
var supportGeneratedColumn = true

// Mysql struct
type Mysql struct {
db *sql.DB
mariaMode bool
db *sql.DB
mariaMode bool

// Show AUTO_INCREMENT with increment number
showAutoIncrement bool

// Hide the entire AUTO_INCREMENT clause
hideAutoIncrement bool
}

func ShowAutoIcrrement() drivers.Option {
Expand All @@ -33,6 +38,16 @@ func ShowAutoIcrrement() drivers.Option {
}
}

func HideAutoIcrrement() drivers.Option {
return func(d drivers.Driver) error {
switch d := d.(type) {
case *Mysql:
d.hideAutoIncrement = true
}
return nil
}
}

// New return new Mysql
func New(db *sql.DB, opts ...drivers.Option) (*Mysql, error) {
m := &Mysql{
Expand Down Expand Up @@ -124,10 +139,14 @@ func (m *Mysql) Analyze(s *schema.Schema) error {
if err != nil {
return errors.WithStack(err)
}
if m.showAutoIncrement {

switch {
case m.showAutoIncrement:
table.Def = tableDef
} else {
table.Def = reAI.ReplaceAllLiteralString(tableDef, "AUTO_INCREMENT=[Redacted by tbls]")
case m.hideAutoIncrement:
table.Def = reAI.ReplaceAllLiteralString(tableDef, "")
default:
table.Def = reAI.ReplaceAllLiteralString(tableDef, " AUTO_INCREMENT=[Redacted by tbls]")
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions testdata/ddl/auto_increment.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS users;

CREATE TABLE users (
id int PRIMARY KEY AUTO_INCREMENT,
username varchar (50) UNIQUE NOT NULL,
password varchar (50) NOT NULL,
email varchar (355) UNIQUE NOT NULL COMMENT 'ex. [email protected]',
created timestamp NOT NULL,
updated timestamp
) COMMENT = 'Users table';

0 comments on commit 4bef36e

Please sign in to comment.