diff --git a/.circleci/config.yml b/.circleci/config.yml new file mode 100644 index 0000000..d233e6a --- /dev/null +++ b/.circleci/config.yml @@ -0,0 +1,208 @@ +version: 2 +jobs: + build: + working_directory: /root + docker: + - image: aarondl0/sqlboiler-test:latest + + - image: postgres:9.6 + environment: + POSTGRES_PASSWORD: psqlpassword + + - image: mysql:5.7 + environment: + MYSQL_ROOT_PASSWORD: mysqlpassword + + - image: microsoft/mssql-server-linux:ctp1-4 + environment: + ACCEPT_EULA: 'Y' + SA_PASSWORD: 'R@@tr@@t1234' + + environment: + GOPATH: /go + ROOTPATH: /go/src/github.com/vattle/sqlboiler + + steps: + - run: + name: Add PSQL Creds + command: | + echo "*:*:*:*:psqlpassword" > /root/.pgpass + chmod 600 /root/.pgpass + - run: + name: Add MySQL Creds + command: | + echo -e "[client]\nuser = root\npassword = mysqlpassword\nhost = localhost\nprotocol = tcp" > /root/.my.cnf + chmod 600 /root/.my.cnf + + - run: + name: Wait for PSQL + command: > + for i in `seq 30`; do + echo "Waiting for psql" + set +o errexit + psql --host localhost --username postgres --dbname template1 -c 'select * from information_schema.tables;' > /dev/null + status=$? + set -o errexit + if [ $status -eq 0 ]; then + break + fi + if [ $i -eq 30 ]; then + echo "Failed to wait for psql" + exit 1 + fi + sleep 1 + done + + - run: + name: Wait for MySQL + command: > + for i in `seq 30`; do + echo "Waiting for mysql" + set +o errexit + mysql --execute 'select * from information_schema.tables;' > /dev/null + status=$? + set -o errexit + if [ $status -eq 0 ]; then + break + fi + if [ $i -eq 30 ]; then + echo "Failed to wait for mysql" + exit 1 + fi + sleep 1 + done + + - run: + name: Wait for MSSQL + command: > + for i in `seq 30`; do + echo "Waiting for mssql" + set +o errexit + sqlcmd -H localhost -U sa -P R@@tr@@t1234 -Q "select * from information_schema.tables;" > /dev/null + status=$? + set -o errexit + if [ $status -eq 0 ]; then + break + fi + if [ $i -eq 30 ]; then + echo "Failed to wait for mssql" + exit 1 + fi + sleep 1 + done + + - run: + name: Make GOPATH + command: mkdir -p /go/src/github.com/vattle/sqlboiler + + - checkout: + path: /go/src/github.com/vattle/sqlboiler + + - run: + name: Create PSQL DB + command: | + createdb --host localhost --username postgres --owner postgres sqlboiler + psql --host localhost --username postgres --dbname sqlboiler < $ROOTPATH/testdata/postgres_test_schema.sql + - run: + name: Create MySQL DB + command: | + mysql --host localhost --execute 'create database sqlboiler;' + mysql --host localhost --database sqlboiler < $ROOTPATH/testdata/mysql_test_schema.sql + - run: + name: Create MSSQL DB + command: | + sqlcmd -S localhost -U sa -P R@@tr@@t1234 -Q "create database sqlboiler;" + sqlcmd -S localhost -U sa -P R@@tr@@t1234 -d sqlboiler -i $ROOTPATH/testdata/mssql_test_schema.sql + + - run: + name: Build SQLBoiler + command: | + cd $ROOTPATH; go get -v -t + cd $ROOTPATH; go build -v github.com/vattle/sqlboiler + + - run: + name: 'Configure SQLBoiler: PSQL' + command: echo -e '[postgres]\nhost="localhost"\nport=5432\nuser="postgres"\npass="psqlpassword"\ndbname="sqlboiler"\nsslmode="disable"\n' > $ROOTPATH/sqlboiler.toml + - run: + name: 'Configure SQLBoiler: MySQL' + command: echo -e '[mysql]\nhost="localhost"\nport=3306\nuser="root"\npass="mysqlpassword"\ndbname="sqlboiler"\nsslmode="false"\n' >> $ROOTPATH/sqlboiler.toml + - run: + name: 'Configure SQLBoiler: MSSQL' + command: echo -e '[mssql]\nhost="localhost"\nport=1433\nuser="sa"\npass="R@@tr@@t1234"\ndbname="sqlboiler"\nsslmode="disable"\n' >> $ROOTPATH/sqlboiler.toml + + - run: + name: 'Generate: PSQL' + command: cd $ROOTPATH; ./sqlboiler -o postgres postgres + - run: + name: 'Generate: MySQL' + command: cd $ROOTPATH; ./sqlboiler -o mysql mysql + - run: + name: 'Generate: MSSQL' + command: cd $ROOTPATH; ./sqlboiler -o mssql mssql + + - run: + name: Download generated and test deps + command: | + cd $ROOTPATH + go get -v -t ./... + + - run: + name: Run Tests + command: | + cd $ROOTPATH + cp ./testdata/mssql_test_schema.sql mssql/tables_schema.sql + go test -v -race ./... | tee test_out.txt + + - run: + name: Convert test output to JUNIT + command: | + mkdir -p $HOME/test_results/go + cat $ROOTPATH/test_out.txt | go-junit-report > $HOME/test_results/go/out.xml + + - store_test_results: + path: test_results +#test: +# pre: +# - echo -e "[postgres]\nhost=\"localhost\"\nport=5432\nuser=\"ubuntu\"\ndbname=\"sqlboiler\"\n" > sqlboiler.toml +# - createdb -U ubuntu sqlboiler +# - psql -U ubuntu sqlboiler < ./testdata/postgres_test_schema.sql +# +# - echo -e "[mysql]\nhost=\"localhost\"\nport=3306\nuser=\"ubuntu\"\ndbname=\"sqlboiler\"\nsslmode=\"false\"\n" >> sqlboiler.toml +# - echo "create database sqlboiler;" | mysql -u ubuntu +# - mysql -u ubuntu sqlboiler < ./testdata/mysql_test_schema.sql +# +# - echo -e "[mssql]\nhost=\"localhost\"\nport=1433\nuser=\"sa\"\ndbname=\"sqlboiler\"\nsslmode=\"disable\"\n" >> sqlboiler.toml +# - docker run -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=R@@tr@@t1234' -p 1433:1433 -d --name mssql microsoft/mssql-server-linux +# - sqlcmd -S localhost -U sa -P R@@tr@@t1234 -Q "create database sqlboiler;" +# - sqlcmd -S localhost -U sa -P R@@tr@@t1234 -d sqlboiler -i ./testdata/mssql_test_schema.sql +# +# - ./sqlboiler -o postgres postgres +# - ./sqlboiler -o mysql mysql +# - ./sqlboiler -o mssql mssql +# - cp ./testdata/mssql_test_schema.sql mssql/tables_schema.sql +# override: +# - go test -v -race ./... > $CIRCLE_ARTIFACTS/gotest.txt +# post: +# - cat $CIRCLE_ARTIFACTS/gotest.txt | go-junit-report > $CIRCLE_TEST_REPORTS/junit.xml +# +#machine: +# environment: +# GODIST: go1.7.linux-amd64.tar.gz +# PATH: /home/ubuntu/.go_workspace/bin:/usr/local/go/bin:/home/ubuntu/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/opt/mssql-tools/bin +# post: +# - mkdir -p download +# - test -e download/$GODIST || curl -o download/$GODIST https://storage.googleapis.com/golang/$GODIST +# - sudo rm -rf /usr/local/go +# - sudo tar -C /usr/local -xzf download/$GODIST +# +#dependencies: +# pre: +# - mkdir -p /home/ubuntu/.go_workspace/src/github.com/jstemmer +# - go get -u github.com/jstemmer/go-junit-report +# +# - curl https://packages.microsoft.com/keys/microsoft.asc | sudo apt-key add - +# - curl https://packages.microsoft.com/config/ubuntu/14.04/prod.list | sudo tee /etc/apt/sources.list.d/msprod.list +# - sudo apt-get update; sudo apt-get install mssql-tools unixodbc-dev +# - docker pull microsoft/mssql-server-linux +# cache_directories: +# - ~/download diff --git a/circle.yml b/circle.yml deleted file mode 100644 index ed75a12..0000000 --- a/circle.yml +++ /dev/null @@ -1,28 +0,0 @@ -test: - pre: - - mkdir -p /home/ubuntu/.go_workspace/src/github.com/jstemmer - - go get -u github.com/jstemmer/go-junit-report - - echo -e "[postgres]\nhost=\"localhost\"\nport=5432\nuser=\"ubuntu\"\ndbname=\"sqlboiler\"\n[mysql]\nhost=\"localhost\"\nport=3306\nuser=\"ubuntu\"\ndbname=\"sqlboiler\"\nsslmode=\"false\"" > sqlboiler.toml - - createdb -U ubuntu sqlboiler - - psql -U ubuntu sqlboiler < ./testdata/postgres_test_schema.sql - - echo "create database sqlboiler;" | mysql -u ubuntu - - mysql -u ubuntu sqlboiler < ./testdata/mysql_test_schema.sql - - ./sqlboiler postgres -o "postgres" - - ./sqlboiler postgres -o "mysql" - override: - - go test -v -race ./... > $CIRCLE_ARTIFACTS/gotest.txt - post: - - cat $CIRCLE_ARTIFACTS/gotest.txt | go-junit-report > $CIRCLE_TEST_REPORTS/junit.xml - -machine: - environment: - GODIST: "go1.7.linux-amd64.tar.gz" - post: - - mkdir -p download - - test -e download/$GODIST || curl -o download/$GODIST https://storage.googleapis.com/golang/$GODIST - - sudo rm -rf /usr/local/go - - sudo tar -C /usr/local -xzf download/$GODIST - -dependencies: - cache_directories: - - ~/download diff --git a/main.go b/main.go index a2f9396..acc7b1e 100644 --- a/main.go +++ b/main.go @@ -73,7 +73,7 @@ func main() { // Set up the cobra root command flags rootCmd.PersistentFlags().StringP("output", "o", "models", "The name of the folder to output to") - rootCmd.PersistentFlags().StringP("schema", "s", "public", "The name of your database schema, for databases that support real schemas") + rootCmd.PersistentFlags().StringP("schema", "s", "", "schema name for drivers that support it (default psql: public, mssql: dbo)") rootCmd.PersistentFlags().StringP("pkgname", "p", "models", "The name you wish to assign to your generated package") rootCmd.PersistentFlags().StringP("basedir", "", "", "The base directory has the templates and templates_test folders") rootCmd.PersistentFlags().StringSliceP("blacklist", "b", nil, "Do not include these tables in your generated package") @@ -202,6 +202,10 @@ func preRun(cmd *cobra.Command, args []string) error { viper.Set("postgres.port", cmdConfig.Postgres.Port) } + if len(cmdConfig.Schema) == 0 { + cmdConfig.Schema = "public" + } + err = vala.BeginValidation().Validate( vala.StringNotEmpty(cmdConfig.Postgres.User, "postgres.user"), vala.StringNotEmpty(cmdConfig.Postgres.Host, "postgres.host"), @@ -280,6 +284,10 @@ func preRun(cmd *cobra.Command, args []string) error { viper.Set("mssql.port", cmdConfig.MSSQL.Port) } + if len(cmdConfig.Schema) == 0 { + cmdConfig.Schema = "dbo" + } + err = vala.BeginValidation().Validate( vala.StringNotEmpty(cmdConfig.MSSQL.User, "mssql.user"), vala.StringNotEmpty(cmdConfig.MSSQL.Host, "mssql.host"), diff --git a/templates_test/main_test/mssql_main.tpl b/templates_test/main_test/mssql_main.tpl index c1c76f2..ba46be7 100644 --- a/templates_test/main_test/mssql_main.tpl +++ b/templates_test/main_test/mssql_main.tpl @@ -68,6 +68,9 @@ func (m *mssqlTester) sslMode(mode string) string { func (m *mssqlTester) createTestDB() error { sql := fmt.Sprintf(` CREATE DATABASE %s; + GO + ALTER DATABASE %[1]s + SET READ_COMMITTED_SNAPSHOT ON; GO`, m.testDBName) return m.runCmd(sql, "sqlcmd", "-S", m.host, "-U", m.user, "-P", m.pass) } diff --git a/templates_test/main_test/mysql_main.tpl b/templates_test/main_test/mysql_main.tpl index fc43d3d..b95b32c 100644 --- a/templates_test/main_test/mysql_main.tpl +++ b/templates_test/main_test/mysql_main.tpl @@ -90,12 +90,23 @@ func (m *mysqlTester) makeOptionFile() error { return errors.Wrap(err, "failed to create option file") } + isTCP := false + _, err = os.Stat(m.host) + if os.IsNotExist(err) { + isTCP = true + } else if err != nil { + return errors.Wrap(err, "could not stat m.host") + } + fmt.Fprintln(tmp, "[client]") fmt.Fprintf(tmp, "host=%s\n", m.host) fmt.Fprintf(tmp, "port=%d\n", m.port) fmt.Fprintf(tmp, "user=%s\n", m.user) fmt.Fprintf(tmp, "password=%s\n", m.pass) fmt.Fprintf(tmp, "ssl-mode=%s\n", m.sslMode(m.sslmode)) + if isTCP { + fmt.Fprintln(tmp, "protocol=tcp") + } fmt.Fprintln(tmp, "[mysqldump]") fmt.Fprintf(tmp, "host=%s\n", m.host) @@ -103,6 +114,9 @@ func (m *mysqlTester) makeOptionFile() error { fmt.Fprintf(tmp, "user=%s\n", m.user) fmt.Fprintf(tmp, "password=%s\n", m.pass) fmt.Fprintf(tmp, "ssl-mode=%s\n", m.sslMode(m.sslmode)) + if isTCP { + fmt.Fprintln(tmp, "protocol=tcp") + } m.optionFile = tmp.Name() diff --git a/testdata/Dockerfile b/testdata/Dockerfile new file mode 100644 index 0000000..034cedc --- /dev/null +++ b/testdata/Dockerfile @@ -0,0 +1,35 @@ +# This Dockerfile builds the image used for CI/testing. +FROM ubuntu:16.04 + +ENV PATH /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/go/bin:/opt/mssql-tools/bin +ENV GODIST go1.8.linux-amd64.tar.gz + +# Set up locales for sqlcmd (otherwise it breaks) +RUN locale-gen en_US.UTF-8 \ + && echo "LC_ALL=en_US.UTF-8" >> /etc/default/locale \ + && echo "LANG=en_US.UTF-8" >> /etc/default/locale + +# Install bootstrap-y tools +RUN apt-get update \ + && apt-get install -y apt-transport-https software-properties-common python3-software-properties \ + && apt-add-repository ppa:git-core/ppa \ + && apt-get update \ + && apt-get install -y curl git + +# Install database clients +# MySQL 8.0 is still in development, so we're using 5.7 which is already +# available in Ubuntu 16.04 +RUN curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - \ + && echo 'deb http://apt.postgresql.org/pub/repos/apt/ xenial-pgdg main' > /etc/apt/sources.list.d/psql.list \ + && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \ + && curl https://packages.microsoft.com/config/ubuntu/16.04/prod.list > /etc/apt/sources.list.d/msprod.list \ + && apt-get update \ + && env ACCEPT_EULA=Y apt-get install -y git postgresql-client-9.6 mysql-client-5.7 mssql-tools unixodbc-dev + +# Install Go +RUN curl -o $GODIST https://storage.googleapis.com/golang/$GODIST \ + && rm -rf /usr/local/go \ + && tar -C /usr/local -xzf $GODIST + +RUN go get -u -v github.com/jstemmer/go-junit-report \ + && mv /root/go/bin/go-junit-report /usr/bin/go-junit-report