diff --git a/Makefile b/Makefile index 30e1320..68d2742 100644 --- a/Makefile +++ b/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 diff --git a/bootstrap_test.go b/bootstrap_test.go new file mode 100644 index 0000000..8d77f81 --- /dev/null +++ b/bootstrap_test.go @@ -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) + } +} diff --git a/db_test.go b/db_test.go index 8b035a0..5e4dfb2 100644 --- a/db_test.go +++ b/db_test.go @@ -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) } }