Skip to content

Commit

Permalink
Add a makefile.
Browse files Browse the repository at this point in the history
- 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.
  • Loading branch information
aarondl committed Nov 13, 2017
1 parent 4094c1f commit e327cdf
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 54 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
/sqlboiler
/sqlboiler-psql
/sqlboiler-mysql
/sqlboiler-mssql
/cmd/sqlboiler/sqlboiler
sqlboiler.toml
models/
Expand Down
147 changes: 147 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
7 changes: 4 additions & 3 deletions importers/imports.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`,
},
Expand All @@ -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": {
Expand All @@ -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"`,
},
Expand Down
2 changes: 1 addition & 1 deletion templates_test/main_test/mssql_main.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
26 changes: 1 addition & 25 deletions templates_test/main_test/mysql_main.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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()
}
26 changes: 1 addition & 25 deletions templates_test/main_test/psql_main.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -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, " ")
}

0 comments on commit e327cdf

Please sign in to comment.