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 29, 2023
1 parent 1998263 commit db1b183
Show file tree
Hide file tree
Showing 4 changed files with 292 additions and 283 deletions.
8 changes: 2 additions & 6 deletions .github/workflows/ci.yaml
Expand Up @@ -50,12 +50,8 @@ jobs:
- uses: actions/setup-go@v4
with:
go-version: ${{ matrix.go }}
- name: Create Table
run: |
make mysql
make psql
- name: Run integration tests
env:
MYSQL_DSN: "root:pass@/txdb_test"
PSQL_DSN: "postgres://postgres:pass@localhost/txdb_test"
MYSQL_DSN: "root:pass@/"
PSQL_DSN: "postgres://postgres:pass@localhost/"
run: go test ./...
50 changes: 0 additions & 50 deletions Makefile
@@ -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
62 changes: 62 additions & 0 deletions bootstrap_test.go
@@ -0,0 +1,62 @@
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')`

testDB = "txdb_test"
)

// 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)
}
switch driver {
case "mysql":
if _, err := db.Exec(mysql_sql); err != nil {
t.Fatal(err)
}
case "postgres":
if _, err := db.Exec(psql_sql); err != nil {
t.Fatal(err)
}
default:
panic("unrecognized driver: " + driver)
}
if _, err := db.Exec(inserts); err != nil {
t.Fatal(err)
}
}

func createDB(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)
}
}

0 comments on commit db1b183

Please sign in to comment.