Skip to content

Commit

Permalink
Move DB bootstrapping into the Go code
Browse files Browse the repository at this point in the history
  • Loading branch information
flimzy committed Nov 23, 2023
1 parent 1998263 commit 08955ed
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 50 deletions.
50 changes: 0 additions & 50 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,54 +1,4 @@
define MYSQL_SQL
CREATE TABLE users (
id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
username VARCHAR(32) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX uniq_email (email)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB;
endef

define PSQL_SQL
CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(32) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL
);
endef

export MYSQL_SQL
MYSQL := "$$MYSQL_SQL"

export PSQL_SQL
PSQL := "$$PSQL_SQL"

INSERTS := "INSERT INTO users (username, email) VALUES ('gopher', 'gopher@go.com'), ('john', 'john@doe.com'), ('jane', 'jane@doe.com');"

MYSQLCMD=mysql
ifndef CI
MYSQLCMD=docker compose exec mysql mysql
endif

PSQLCMD=psql
ifndef CI
PSQLCMD=docker compose exec postgres psql
endif

test: MYSQL_DSN=root:pass@/txdb_test
test: PSQL_DSN=postgres://postgres:pass@localhost/txdb_test
test: mysql psql
@go test -race

mysql:
@$(MYSQLCMD) -h 127.0.0.1 -u root -ppass -e 'DROP DATABASE IF EXISTS txdb_test'
@$(MYSQLCMD) -h 127.0.0.1 -u root -ppass -e 'CREATE DATABASE txdb_test'
@$(MYSQLCMD) -h 127.0.0.1 -u root -ppass txdb_test -e $(MYSQL)
@$(MYSQLCMD) -h 127.0.0.1 -u root -ppass txdb_test -e $(INSERTS)

psql:
@$(PSQLCMD) "postgresql://postgres:pass@127.0.0.1" -c 'DROP DATABASE IF EXISTS txdb_test'
@$(PSQLCMD) "postgresql://postgres:pass@127.0.0.1" -c 'CREATE DATABASE txdb_test'
@$(PSQLCMD) "postgresql://postgres:pass@127.0.0.1/txdb_test" -c $(PSQL)
@$(PSQLCMD) "postgresql://postgres:pass@127.0.0.1/txdb_test" -c $(INSERTS)

.PHONY: test mysql psql
51 changes: 51 additions & 0 deletions bootstrap_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package txdb_test

import (
"database/sql"
"testing"
)

const (
mysql_sql = `CREATE TABLE users (
id BIGINT UNSIGNED AUTO_INCREMENT NOT NULL,
username VARCHAR(32) NOT NULL,
email VARCHAR(255) NOT NULL,
PRIMARY KEY (id),
UNIQUE INDEX uniq_email (email)
) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB`

psql_sql = `CREATE TABLE users (
id SERIAL PRIMARY KEY,
username VARCHAR(32) NOT NULL,
email VARCHAR(255) UNIQUE NOT NULL
)`

inserts = `INSERT INTO users (username, email) VALUES ('gopher', 'gopher@go.com'), ('john', 'john@doe.com'), ('jane', 'jane@doe.com')`
)

// bootstrap bootstraps the database with the nfor tests.
func bootstrap(t *testing.T, driver, dsn string) {
db, err := sql.Open(driver, dsn)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec("DROP DATABASE IF EXISTS txdb_test"); err != nil {
t.Fatal(err)
}
if _, err := db.Exec("CREATE DATABASE txdb_test"); err != nil {
t.Fatal(err)
}
switch driver {
case "mysql":
if _, err := db.Exec(mysql_sql); err != nil {
t.Fatal(err)
}
case "postgresql":
if _, err := db.Exec(psql_sql); err != nil {
t.Fatal(err)
}
}
if _, err := db.Exec(inserts); err != nil {
t.Fatal(err)
}
}
1 change: 1 addition & 0 deletions db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ func (d *testDriver) register(t *testing.T) {
dsn := d.dsn(t)
d.registered = true
txdb.Register(d.name, d.driver, dsn)
bootstrap(t, d.driver, dsn)
}
}

Expand Down

0 comments on commit 08955ed

Please sign in to comment.