-
-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move DB bootstrapping into the Go code
- Loading branch information
Showing
3 changed files
with
52 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters