diff --git a/Makefile b/Makefile index 0226105..68a8cdc 100644 --- a/Makefile +++ b/Makefile @@ -8,6 +8,7 @@ DOCKER_IMAGE := "ghcr.io/metal-toolbox/fleetdb" PROJECT_NAME := fleetdb REPO := "https://github.com/metal-toolbox/fleetdb.git" SQLBOILER := v4.15.0 +SQLBOILER_DRIVER := v4.0.0 ## run all tests test: | unit-test integration-test @@ -66,6 +67,13 @@ docker-down: docker-clean: @docker-compose -f quickstart.yml down --volumes +## start if not started, and wait for database to be live +docker-up-and-wait: + @cockroach sql --insecure -e "select 1" > /dev/null 2>&1 || (echo "Attempting to start server..." && docker-compose -f quickstart.yml up -d crdb) + @for i in 5 4 3 2 1; do \ + cockroach sql --insecure -e "select 1" > /dev/null 2>&1 && break || (echo "Waiting for cockroach server to be live. $$i retries remaining..." && sleep 1); \ + done + ## setup devel database dev-database: | vendor @cockroach sql --insecure -e "drop database if exists fleetdb" @@ -73,7 +81,7 @@ dev-database: | vendor @FLEETDB_CRDB_URI="${DEV_DB}" go run main.go migrate up ## setup test database -test-database: | vendor +test-database: | vendor docker-up-and-wait @cockroach sql --insecure -e "drop database if exists fleetdb_test" @cockroach sql --insecure -e "create database fleetdb_test" @FLEETDB_CRDB_URI="${TEST_DB}" go run main.go migrate up @@ -87,13 +95,12 @@ fresh-test: clean ## install sqlboiler install-sqlboiler: - go install github.com/volatiletech/sqlboiler/v4@${SQLBOILER} + @go install github.com/volatiletech/sqlboiler/v4@${SQLBOILER} + @go install github.com/metal-toolbox/sqlboiler-crdb-fleetdb/v4@${SQLBOILER_DRIVER} ## boil sql -boil: install-sqlboiler - make docker-up - make test-database - sqlboiler crdb --add-soft-deletes +boil: install-sqlboiler test-database + @sqlboiler crdb-fleetdb --add-soft-deletes ## log into database psql: diff --git a/internal/models/crdb_main_test.go b/internal/models/crdb_main_test.go index aa5a73c..2e4551f 100644 --- a/internal/models/crdb_main_test.go +++ b/internal/models/crdb_main_test.go @@ -47,12 +47,12 @@ func init() { func (c *crdbTester) setup() error { var err error - c.dbName = viper.GetString("crdb.dbname") - c.host = viper.GetString("crdb.host") - c.user = viper.GetString("crdb.user") - c.pass = viper.GetString("crdb.pass") - c.port = viper.GetInt("crdb.port") - c.sslmode = viper.GetString("crdb.sslmode") + c.dbName = viper.GetString("crdb-fleetdb.dbname") + c.host = viper.GetString("crdb-fleetdb.host") + c.user = viper.GetString("crdb-fleetdb.user") + c.pass = viper.GetString("crdb-fleetdb.pass") + c.port = viper.GetInt("crdb-fleetdb.port") + c.sslmode = viper.GetString("crdb-fleetdb.sslmode") // Create a randomized db name. c.testDBName = randomize.StableDBName(c.dbName) @@ -74,14 +74,14 @@ func (c *crdbTester) setup() error { createCmd.Stdin = newShowCreateTableFilter(newFKeyDestroyer(rgxCDBFkey, r)) if err = dumpCmd.Start(); err != nil { - return errors.Wrap(err, "failed to start 'cockroach dump' command") + return errors.Wrap(err, "failed to start cockroach show-create command") } if err = createCmd.Start(); err != nil { - return errors.Wrap(err, "failed to start 'cockroach sql' command") + return errors.Wrap(err, "failed to start 'cockroach sql' command for db create") } if err = dumpCmd.Wait(); err != nil { - return errors.Wrap(err, "failed to wait for dump 'cockroach sql' command") + return errors.Wrap(err, "failed to wait for cockroach show-create command") } // After dumpCmd is done, close the write end of the pipe @@ -90,7 +90,7 @@ func (c *crdbTester) setup() error { } if err = createCmd.Wait(); err != nil { - return errors.Wrap(err, "failed to wait for create 'cockroach sql' command") + return errors.Wrap(err, "failed to wait for 'cockroach sql' command for db create") } return nil diff --git a/sqlboiler.toml b/sqlboiler.toml index 1a57a8d..87e436b 100644 --- a/sqlboiler.toml +++ b/sqlboiler.toml @@ -2,7 +2,7 @@ wipe = true pkgname = "models" output = "internal/models" -[crdb] +[crdb-fleetdb] dbname = "fleetdb_test" host = "localhost" port = 26257