-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
97d4847
commit 6c773a9
Showing
15 changed files
with
334 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
FROM godfish_test/client_base:latest | ||
|
||
WORKDIR /src | ||
RUN make build-sqlserver | ||
|
||
# Alpine linux doesn't have a SQL Server client. Build a golang binary to | ||
# check if server is ready. Use it in the entrypoint. | ||
WORKDIR /src/.ci/sqlserver | ||
RUN go build -v -o /client_check_db . | ||
COPY .ci/sqlserver/client.sh / | ||
|
||
WORKDIR /src | ||
ENTRYPOINT /client.sh |
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,37 @@ | ||
package main | ||
|
||
import ( | ||
"database/sql" | ||
"log" | ||
"os" | ||
|
||
// Imported for the side effect of registering the driver. | ||
_ "github.com/denisenkom/go-mssqldb" | ||
) | ||
|
||
var dsn string | ||
|
||
func init() { | ||
log.SetOutput(os.Stderr) | ||
|
||
const key = "DB_DSN" | ||
if dsn = os.Getenv(key); dsn == "" { | ||
log.Fatalf("missing required env var %q", key) | ||
} | ||
} | ||
|
||
func main() { | ||
if err := ping(); err != nil { | ||
log.Fatal(err) | ||
} | ||
log.Println("ok") | ||
} | ||
|
||
func ping() error { | ||
conn, err := sql.Open("sqlserver", dsn) | ||
if err != nil { | ||
return err | ||
} | ||
defer conn.Close() | ||
return conn.Ping() | ||
} |
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,27 @@ | ||
#!/usr/bin/env sh | ||
|
||
set -eu | ||
|
||
echo "building binary" | ||
make build-sqlserver | ||
echo "testing godfish" | ||
make test ARGS='-v -count=1 -coverprofile=/tmp/cover.out' | ||
|
||
# Wait for db server to be ready, with some limits. | ||
|
||
num_attempts=0 | ||
|
||
until /client_check_db ; do | ||
num_attempts=$((num_attempts+1)) | ||
if [ $num_attempts -ge 15 ]; then | ||
>&2 echo "ERROR: max attempts exceeded" | ||
exit 1 | ||
fi | ||
|
||
>&2 echo "db is unavailable now, sleeping" | ||
sleep 2 | ||
done | ||
>&2 echo "db is up" | ||
|
||
echo "testing godfish against live db" | ||
make test-sqlserver ARGS='-v -count=1 -coverprofile=/tmp/cover_driver.out' |
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,27 @@ | ||
version: "3.9" | ||
|
||
services: | ||
client: | ||
build: | ||
context: "${BUILD_DIR}" | ||
dockerfile: "${BUILD_DIR}/.ci/sqlserver/client.Dockerfile" | ||
image: godfish_test/sqlserver/client:latest | ||
container_name: godfish_ci_sqlserver_client | ||
depends_on: | ||
- server | ||
entrypoint: /client.sh | ||
env_file: | ||
- env | ||
environment: | ||
CGO_ENABLED: 0 | ||
tty: true | ||
server: | ||
build: | ||
context: "${BUILD_DIR}" | ||
dockerfile: "${BUILD_DIR}/.ci/sqlserver/server.Dockerfile" | ||
image: godfish_test/sqlserver/server:latest | ||
container_name: godfish_ci_sqlserver_server | ||
env_file: | ||
- env | ||
environment: | ||
ACCEPT_EULA: '1' |
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,2 @@ | ||
SA_PASSWORD=1_complicated_password! | ||
DB_DSN="sqlserver://sa:${SA_PASSWORD}@server" |
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 @@ | ||
FROM mcr.microsoft.com/mssql/server |
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,18 @@ | ||
name: sqlserver | ||
on: [push, pull_request] | ||
jobs: | ||
all: | ||
runs-on: ubuntu-20.04 | ||
steps: | ||
- name: Checkout repo | ||
uses: actions/checkout@v2 | ||
- name: Build environment and run tests | ||
run: make -f ci.Makefile ci-sqlserver-up | ||
- name: Upload code coverage | ||
uses: codecov/codecov-action@v2 | ||
with: | ||
fail_ci_if_error: true | ||
files: /tmp/cover.out,/tmp/cover_driver.out | ||
verbose: true | ||
- name: Teardown | ||
run: make -f ci.Makefile ci-sqlserver-down |
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
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
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
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,18 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"log" | ||
"os" | ||
|
||
"github.com/rafaelespinoza/godfish/drivers/sqlserver" | ||
"github.com/rafaelespinoza/godfish/internal/cmd" | ||
) | ||
|
||
func main() { | ||
root := cmd.New(sqlserver.NewDriver()) | ||
if err := root.Run(context.TODO(), os.Args[1:]); err != nil { | ||
log.Println(err) | ||
os.Exit(1) | ||
} | ||
} |
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,91 @@ | ||
package sqlserver | ||
|
||
import ( | ||
"database/sql" | ||
"errors" | ||
"strings" | ||
|
||
mssql "github.com/denisenkom/go-mssqldb" | ||
"github.com/rafaelespinoza/godfish" | ||
) | ||
|
||
// NewDriver creates a new Microsoft SQL Server driver. | ||
func NewDriver() godfish.Driver { return &driver{} } | ||
|
||
// driver implements the godfish.Driver interface for Microsoft SQL Server. | ||
type driver struct { | ||
connection *sql.DB | ||
} | ||
|
||
func (d *driver) Name() string { return "sqlserver" } | ||
func (d *driver) Connect(dsn string) (err error) { | ||
if d.connection != nil { | ||
return | ||
} | ||
|
||
// github.com/denisenkom/go-mssqldb registers two sql.Driver names: | ||
// "mssql" and "sqlserver". Their docs seem to steer users towards the | ||
// latter, so just use that one. | ||
conn, err := sql.Open("sqlserver", dsn) | ||
if err != nil { | ||
return | ||
} | ||
d.connection = conn | ||
return | ||
} | ||
|
||
func (d *driver) Close() (err error) { | ||
conn := d.connection | ||
if conn == nil { | ||
return | ||
} | ||
d.connection = nil | ||
err = conn.Close() | ||
return | ||
} | ||
|
||
func (d *driver) Execute(query string, args ...interface{}) (err error) { | ||
_, err = d.connection.Exec(query) | ||
return | ||
} | ||
|
||
func (d *driver) CreateSchemaMigrationsTable() (err error) { | ||
_, err = d.connection.Exec(` | ||
IF NOT EXISTS ( | ||
SELECT 1 FROM information_schema.tables WHERE table_schema = (SELECT schema_name()) AND table_name = 'schema_migrations' | ||
) | ||
CREATE TABLE schema_migrations (migration_id VARCHAR(128) PRIMARY KEY NOT NULL) | ||
`) | ||
return | ||
} | ||
|
||
func (d *driver) AppliedVersions() (out godfish.AppliedVersions, err error) { | ||
rows, err := d.connection.Query(`SELECT migration_id FROM schema_migrations ORDER BY migration_id ASC`) | ||
|
||
var ierr mssql.Error | ||
// https://docs.microsoft.com/en-us/sql/relational-databases/errors-events/database-engine-events-and-errors | ||
// Invalid object name 'schema_migrations' | ||
if errors.As(err, &ierr) && ierr.SQLErrorNumber() == 208 && strings.Contains(ierr.Error(), "schema_migrations") { | ||
err = godfish.ErrSchemaMigrationsDoesNotExist | ||
} | ||
out = godfish.AppliedVersions(rows) | ||
return | ||
} | ||
|
||
func (d *driver) UpdateSchemaMigrations(forward bool, version string) (err error) { | ||
conn := d.connection | ||
if forward { | ||
_, err = conn.Exec(` | ||
INSERT INTO schema_migrations (migration_id) | ||
VALUES (@p1)`, | ||
version, | ||
) | ||
} else { | ||
_, err = conn.Exec(` | ||
DELETE FROM schema_migrations | ||
WHERE migration_id = @p1`, | ||
version, | ||
) | ||
} | ||
return | ||
} |
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,27 @@ | ||
package sqlserver_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/rafaelespinoza/godfish/drivers/sqlserver" | ||
"github.com/rafaelespinoza/godfish/internal/test" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
queries := test.Queries{ | ||
CreateFoos: test.MigrationContent{ | ||
Forward: "CREATE TABLE foos (id int PRIMARY KEY);", | ||
Reverse: "DROP TABLE foos;", | ||
}, | ||
CreateBars: test.MigrationContent{ | ||
Forward: "CREATE TABLE bars (id int PRIMARY KEY);", | ||
Reverse: "DROP TABLE bars;", | ||
}, | ||
AlterFoos: test.MigrationContent{ | ||
Forward: `ALTER TABLE foos ADD a varchar(255);`, | ||
Reverse: "ALTER TABLE foos DROP COLUMN a;", | ||
}, | ||
} | ||
|
||
test.RunDriverTests(t, sqlserver.NewDriver(), queries) | ||
} |
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
Oops, something went wrong.