From e327cdfdbabee3a305453611403d97ab468b25ea Mon Sep 17 00:00:00 2001 From: Aaron L Date: Mon, 13 Nov 2017 13:42:18 -0800 Subject: [PATCH] Add a makefile. - The makefile is there because there's too many commands for one person to be running to develop on sqlboiler anymore. Had to make it more standardized. - Use the actual drivers create query string bits. --- .gitignore | 3 + Makefile | 147 ++++++++++++++++++++++++ importers/imports.go | 7 +- templates_test/main_test/mssql_main.tpl | 2 +- templates_test/main_test/mysql_main.tpl | 26 +---- templates_test/main_test/psql_main.tpl | 26 +---- 6 files changed, 157 insertions(+), 54 deletions(-) create mode 100644 Makefile diff --git a/.gitignore b/.gitignore index 8f7858d7a..84c1f1ffc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,7 @@ /sqlboiler +/sqlboiler-psql +/sqlboiler-mysql +/sqlboiler-mssql /cmd/sqlboiler/sqlboiler sqlboiler.toml models/ diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..12205606b --- /dev/null +++ b/Makefile @@ -0,0 +1,147 @@ +# Everything in this make file assumes you have the ability +# to run the commands as they are written (meaning .my.cnf and .pgpass +# files set up with admin users) as well as the mssql db set up with +# the sa / Sqlboiler@1234 credentials (see docker run below for example) + +.PHONY: \ + build build-psql build-mysql build-mssql \ + tests test-psql test-mysql test-mssql \ + test-dbs test-db-psql test-db-mysql test-db-mssql \ + test-users test-user-psql test-user-mysql test-user-mssql \ + driver-tests driver-test-psql driver-test-mysql driver-test-mssql \ + driver-users driver-user-psql driver-user-mysql driver-user-mssql \ + driver-dbs driver-db-psql driver-db-mysql driver-db-mssql \ + run-mssql + +USER=sqlboiler_root_user +DB=sqlboiler_model_test +PASS=sqlboiler +MSSQLPASS=Sqlboiler@1234 + +DRIVER_USER=sqlboiler_driver_user +DRIVER_DB=sqlboiler_driver_test + +# Builds all software and runs model tests +tests: test-psql test-mysql test-mssql +# Creates super users +test-users: test-user-psql test-user-mysql test-user-mssql +# Creates databases +test-dbs: test-db-psql test-db-mysql test-db-mssql + +# Runs tests on the drivers +driver-tests: driver-test-psql driver-test-mysql driver-test-mssql +# Creates regular databases +driver-dbs: driver-db-psql driver-db-mysql driver-db-mssql +# Creates regular users with access to only one DB, may modify the DB +# to ensure write access to the user +driver-users: driver-user-psql driver-user-mysql driver-user-mssql + +run-mssql: + docker run --detach --rm --env 'ACCEPT_EULA=Y' --env 'SA_PASSWORD=Sqlboiler@1234' --publish 1433:1433 --name mssql microsoft/mssql-server-linux:2017-latest +kill-mssql: + docker rm --force mssql + +# ==================================== +# Building operations +# ==================================== + +build: + go build github.com/volatiletech/sqlboiler + +build-psql: + go build github.com/volatiletech/sqlboiler/drivers/sqlboiler-psql + +build-mysql: + go build github.com/volatiletech/sqlboiler/drivers/sqlboiler-mysql + +build-mssql: + go build github.com/volatiletech/sqlboiler/drivers/sqlboiler-mssql + +# ==================================== +# Testing operations +# ==================================== + +test-psql: build build-psql + ./sqlboiler --wipe psql + go test ./models + +test-mysql: build build-mysql + ./sqlboiler --wipe mysql + go test ./models + +test-mssql: build build-mssql + ./sqlboiler --wipe mssql + go test ./models + +test-user-psql: + # Can't use createuser because it interactively promtps for a password + psql --command "create user $(USER) with superuser password '$(PASS)';" postgres + +test-user-mysql: + mysql --execute "create user $(USER) identified by '$(PASS)';" + mysql --execute "grant all privileges on *.* to $(USER);" + +# Must be run after a database is created +test-user-mssql: + sqlcmd -S localhost -U sa -P $(MSSQLPASS) -Q "create login $(USER) with password = '$(MSSQLPASS)';" + sqlcmd -S localhost -U sa -P $(MSSQLPASS) -Q "alter server role sysadmin add member $(USER);" + +test-db-psql: + env PGPASSWORD=$(PASS) dropdb --username $(USER) --if-exists $(DB) + env PGPASSWORD=$(PASS) createdb --owner $(USER) --username $(USER) $(DB) + env PGPASSWORD=$(PASS) psql --username $(USER) --file testdata/psql_test_schema.sql $(DB) + +test-db-mysql: + mysql --user $(USER) --password=$(PASS) --execute "drop database if exists $(DB);" + mysql --user $(USER) --password=$(PASS) --execute "create database $(DB);" + mysql --user $(USER) --password=$(PASS) $(DB) < testdata/mysql_test_schema.sql + +test-db-mssql: + sqlcmd -S localhost -U $(USER) -P $(MSSQLPASS) -Q "drop database if exists $(DB)"; + sqlcmd -S localhost -U $(USER) -P $(MSSQLPASS) -Q "create database $(DB)"; + sqlcmd -S localhost -U $(USER) -P $(MSSQLPASS) -d $(DB) -i testdata/mssql_test_schema.sql + +# ==================================== +# Driver operations +# ==================================== + +driver-test-psql: + cd drivers/sqlboiler-psql/driver && go test +driver-test-mysql: + go test github.com/volatiletech/sqlboiler/drivers/sqlboiler-mysql/driver +driver-test-mssql: + go test github.com/volatiletech/sqlboiler/drivers/sqlboiler-mssql/driver + +driver-db-psql: + createdb $(DRIVER_DB) + +driver-db-mysql: + mysql --execute "create database $(DRIVER_DB);" + +driver-db-mssql: + sqlcmd -S localhost -U sa -P $(MSSQLPASS) -Q "create database $(DRIVER_DB);" + sqlcmd -S localhost -U sa -P $(MSSQLPASS) -d $(DRIVER_DB) -Q "exec sp_configure 'contained database authentication', 1;" + sqlcmd -S localhost -U sa -P $(MSSQLPASS) -d $(DRIVER_DB) -Q "reconfigure" + sqlcmd -S localhost -U sa -P $(MSSQLPASS) -d $(DRIVER_DB) -Q "alter database $(DRIVER_DB) set containment = partial;" + +driver-user-psql: + psql --command "create role $(DRIVER_USER) login nocreatedb nocreaterole nocreateuser password '$(PASS)';" $(DRIVER_DB) + psql --command "alter database $(DRIVER_DB) owner to $(DRIVER_USER);" $(DRIVER_DB) + +driver-user-mysql: + mysql --execute "create user $(DRIVER_USER) identified by '$(PASS)';" + mysql --execute "grant all privileges on $(DRIVER_DB).* to $(DRIVER_USER);" + +driver-user-mssql: + sqlcmd -S localhost -U sa -P $(MSSQLPASS) -d $(DRIVER_DB) -Q "create user $(DRIVER_USER) with password = '$(MSSQLPASS)'"; + sqlcmd -S localhost -U sa -P $(MSSQLPASS) -d $(DRIVER_DB) -Q "grant alter, control to $(DRIVER_USER)"; + +# ==================================== +# Clean operations +# ==================================== + +clean: + rm ./sqlboiler + rm ./sqlboiler-psql + rm ./sqlboiler-mysql + rm ./sqlboiler-mssql diff --git a/importers/imports.go b/importers/imports.go index 2c33edaab..2b1629b6b 100644 --- a/importers/imports.go +++ b/importers/imports.go @@ -267,6 +267,7 @@ func NewDefaultImports() Collection { ThirdParty: List{ `"github.com/pkg/errors"`, `"github.com/spf13/viper"`, + `"github.com/volatiletech/sqlboiler/drivers/sqlboiler-psql/driver"`, `"github.com/volatiletech/sqlboiler/randomize"`, `_ "github.com/lib/pq"`, }, @@ -280,14 +281,14 @@ func NewDefaultImports() Collection { `"io/ioutil"`, `"os"`, `"os/exec"`, - `"strconv"`, `"strings"`, }, ThirdParty: List{ `"github.com/pkg/errors"`, `"github.com/spf13/viper"`, + `"github.com/volatiletech/sqlboiler/drivers/sqlboiler-mysql/driver"`, `"github.com/volatiletech/sqlboiler/randomize"`, - `"github.com/go-sql-driver/mysql"`, + `_ "github.com/go-sql-driver/mysql"`, }, }, "mssql": { @@ -302,7 +303,7 @@ func NewDefaultImports() Collection { ThirdParty: List{ `"github.com/pkg/errors"`, `"github.com/spf13/viper"`, - `"github.com/volatiletech/sqlboiler/drivers"`, + `"github.com/volatiletech/sqlboiler/drivers/sqlboiler-mssql/driver"`, `"github.com/volatiletech/sqlboiler/randomize"`, `_ "github.com/denisenkom/go-mssqldb"`, }, diff --git a/templates_test/main_test/mssql_main.tpl b/templates_test/main_test/mssql_main.tpl index ba46be770..507309d11 100644 --- a/templates_test/main_test/mssql_main.tpl +++ b/templates_test/main_test/mssql_main.tpl @@ -122,7 +122,7 @@ func (m *mssqlTester) conn() (*sql.DB, error) { } var err error - m.dbConn, err = sql.Open("mssql", drivers.MSSQLBuildQueryString(m.user, m.pass, m.testDBName, m.host, m.port, m.sslmode)) + m.dbConn, err = sql.Open("mssql", driver.MSSQLBuildQueryString(m.user, m.pass, m.testDBName, m.host, m.port, m.sslmode)) if err != nil { return nil, err } diff --git a/templates_test/main_test/mysql_main.tpl b/templates_test/main_test/mysql_main.tpl index 39bd8ee92..84d4564a0 100644 --- a/templates_test/main_test/mysql_main.tpl +++ b/templates_test/main_test/mysql_main.tpl @@ -171,34 +171,10 @@ func (m *mysqlTester) conn() (*sql.DB, error) { } var err error - m.dbConn, err = sql.Open("mysql", mySQLBuildQueryString(m.user, m.pass, m.testDBName, m.host, m.port, m.sslmode)) + m.dbConn, err = sql.Open("mysql", driver.MySQLBuildQueryString(m.user, m.pass, m.testDBName, m.host, m.port, m.sslmode)) if err != nil { return nil, err } return m.dbConn, nil } - - -func mySQLBuildQueryString(user, pass, dbname, host string, port int, sslmode string) string { - var config mysql.Config - - config.User = user - if len(pass) != 0 { - config.Passwd = pass - } - config.DBName = dbname - config.Net = "tcp" - config.Addr = host - if port == 0 { - port = 3306 - } - config.Addr += ":" + strconv.Itoa(port) - config.TLSConfig = sslmode - - // MySQL is a bad, and by default reads date/datetime into a []byte - // instead of a time.Time. Tell it to stop being a bad. - config.ParseTime = true - - return config.FormatDSN() -} diff --git a/templates_test/main_test/psql_main.tpl b/templates_test/main_test/psql_main.tpl index a2b4d9eca..1c155ae9b 100644 --- a/templates_test/main_test/psql_main.tpl +++ b/templates_test/main_test/psql_main.tpl @@ -162,34 +162,10 @@ func (p *pgTester) conn() (*sql.DB, error) { } var err error - p.dbConn, err = sql.Open("postgres", psqlBuildQueryString(p.user, p.pass, p.testDBName, p.host, p.port, p.sslmode)) + p.dbConn, err = sql.Open("postgres", driver.PSQLBuildQueryString(p.user, p.pass, p.testDBName, p.host, p.port, p.sslmode)) if err != nil { return nil, err } return p.dbConn, nil } - -func psqlBuildQueryString(user, pass, dbname, host string, port int, sslmode string) string { - parts := []string{} - if len(user) != 0 { - parts = append(parts, fmt.Sprintf("user=%s", user)) - } - if len(pass) != 0 { - parts = append(parts, fmt.Sprintf("password=%s", pass)) - } - if len(dbname) != 0 { - parts = append(parts, fmt.Sprintf("dbname=%s", dbname)) - } - if len(host) != 0 { - parts = append(parts, fmt.Sprintf("host=%s", host)) - } - if port != 0 { - parts = append(parts, fmt.Sprintf("port=%d", port)) - } - if len(sslmode) != 0 { - parts = append(parts, fmt.Sprintf("sslmode=%s", sslmode)) - } - - return strings.Join(parts, " ") -}