Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions .github/workflows/ci-test.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name: CI
on: push
jobs:
build:
postgresql:
runs-on: ubuntu-latest
services:
# Label used to access the service container
Expand All @@ -21,7 +21,6 @@ jobs:
--health-retries 5
ports:
- 5432:5432

strategy:
matrix:
go: [ '1.16', '1.17' ]
Expand All @@ -38,4 +37,32 @@ jobs:
sudo mv migrate /usr/bin/migrate
- name: Run migration
run: migrate -path db/migration -database "postgresql://root:test@localhost:5432/sqljson?sslmode=disable" -verbose up
- run: go test -v ./...
- run: go test -timeout 30s -run ^TestUserTable$ github.com/emirot/sqlgojson/src/sqlgojson
mysqltest:
runs-on: ubuntu-latest
strategy:
matrix:
go: [ '1.16', '1.17' ]
name: Go ${{ matrix.go }} sample
env:
DB_DATABASE: sqljson
DB_USER: root
DB_PASSWORD: 'root'
DB_HOST: localhost
steps:
- run: |
sudo /etc/init.d/mysql start
mysql -e 'CREATE DATABASE sqljson;' -uroot -proot
mysql -e 'SHOW DATABASES;' -uroot -proot
- uses: actions/checkout@v2
- name: Setup go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go }}
- name: Install go migrate
run: |
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.15.1/migrate.linux-amd64.tar.gz | tar xvz;
sudo mv migrate /usr/bin/migrate
- name: Run migration
run: migrate -path db/migration -database "mysql://root:root@tcp(localhost:3306)/sqljson" -verbose up
- run: go test -timeout 30s -run ^TestMysqlTable$ github.com/emirot/sqlgojson/src/sqlgojson
6 changes: 3 additions & 3 deletions db/migration/000001_init_schema.up.sql
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
CREATE TABLE IF NOT EXISTS users (
id INT,
name VARCHAR(255),
created_at TIMESTAMP DEFAULT NULL
created_at TIMESTAMP
);

INSERT INTO users values (1, 'nolan', '2017-07-01T14:59:55.711Z');
INSERT INTO users values (2, 'test', '2017-07-01T15:59:55.711Z');
INSERT INTO users values (1, 'nolan', '2017-07-01T14:59:55');
INSERT INTO users values (2, 'test', '2017-07-01T15:59:55');
INSERT INTO users values (3, 'test', null);

CREATE TABLE IF NOT EXISTS test (
Expand Down
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/emirot/sqlgojson

go 1.17

require github.com/lib/pq v1.10.4
require (
github.com/go-sql-driver/mysql v1.6.0
github.com/lib/pq v1.10.4
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/go-sql-driver/mysql v1.6.0 h1:BCTh4TKNUYmOmMUcQ3IipzF5prigylS7XXjEkfCHuOE=
github.com/go-sql-driver/mysql v1.6.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg=
github.com/lib/pq v1.10.4 h1:SO9z7FRPzA03QhHKJrH5BXA6HU1rS4V2nIVrrNC1iYk=
github.com/lib/pq v1.10.4/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
2 changes: 0 additions & 2 deletions src/sqlgojson/sqlgojson.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ package sqlgojson
import (
"database/sql"
"encoding/json"

_ "github.com/lib/pq"
)

func SqlGoJson(rows *sql.Rows) (string, error) {
Expand Down
42 changes: 41 additions & 1 deletion src/sqlgojson/sqlgojson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"database/sql"
"fmt"
"testing"

_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)

func queryPostgresql(query string) (*sql.Rows, error) {
Expand All @@ -25,6 +28,43 @@ func queryPostgresql(query string) (*sql.Rows, error) {
return rows, err
}

func queryMysql(query string) (*sql.Rows, error) {
connStr := "root:root@/sqljson"
db, err := sql.Open("mysql", connStr)
if err != nil {
panic(err)
}
defer db.Close()
err = db.Ping()
if err != nil {
panic(err)
}
fmt.Printf("\nSuccessfully connected to database \n")
rows, err := db.Query(query)
if err != nil {
panic(err)
}
return rows, err
}

func TestMysqlTable(t *testing.T) {
rows, err := queryMysql("SELECT * from test;")
if err != nil {
panic(err)
}
res, err := SqlGoJson(rows)
if err != nil {
panic(err)
}
// mysql doesn't have boolean type and use TinyINT instead.
expected := `[[{"id":1},{"num":3.14},{"b":1}],[{"id":2},{"num":4.014},{"b":0}]]`
fmt.Println(res, expected)
if res != expected {
t.Errorf("Failed")
}
fmt.Printf("rep:%v", res)
}

func TestUserTable(t *testing.T) {
rows, err := queryPostgresql("SELECT * from users;")
if err != nil {
Expand All @@ -34,7 +74,7 @@ func TestUserTable(t *testing.T) {
if err != nil {
panic(err)
}
if res != `[[{"id":1},{"name":"nolan"},{"created_at":"2017-07-01T14:59:55.711Z"}],[{"id":2},{"name":"test"},{"created_at":"2017-07-01T15:59:55.711Z"}],[{"id":3},{"name":"test"},{"created_at":null}]]` {
if res != `[[{"id":1},{"name":"nolan"},{"created_at":"2017-07-01T14:59:55"}],[{"id":2},{"name":"test"},{"created_at":"2017-07-01T15:59:55"}],[{"id":3},{"name":"test"},{"created_at":null}]]` {
t.Errorf("Failed")
}
fmt.Printf("rep:%v", res)
Expand Down