Skip to content

Commit bc776f9

Browse files
committed
Add support for CockorachDB.
1 parent 3ff9241 commit bc776f9

33 files changed

+1056
-1053
lines changed

.circleci/config.yml

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,13 @@ jobs:
3333
MYSQL_USER: jet
3434
MYSQL_PASSWORD: jet
3535

36+
- image: cockroachdb/cockroach-unstable:v22.1.0-beta.4
37+
command: ['start-single-node', '--insecure']
38+
environment:
39+
COCKROACH_USER: jet
40+
COCKROACH_PASSWORD: jet
41+
COCKROACH_DATABASE: jetdb
42+
3643
environment: # environment variables for the build itself
3744
TEST_RESULTS: /tmp/test-results # path to where test results will be saved
3845

@@ -82,7 +89,18 @@ jobs:
8289
echo -n .
8390
sleep 1
8491
done
85-
echo Failed waiting for MySQL && exit 1
92+
echo Failed waiting for MySQL && exit 1
93+
94+
- run:
95+
name: Waiting for Cockroach to be ready
96+
command: |
97+
for i in `seq 1 10`;
98+
do
99+
nc -z localhost 26257 && echo Success && exit 0
100+
echo -n .
101+
sleep 1
102+
done
103+
echo Failed waiting for Cockroach && exit 1
86104
87105
- run:
88106
name: Install MySQL CLI;
@@ -122,8 +140,9 @@ jobs:
122140
-coverpkg=github.com/go-jet/jet/v2/postgres/...,github.com/go-jet/jet/v2/mysql/...,github.com/go-jet/jet/v2/sqlite/...,github.com/go-jet/jet/v2/qrm/...,github.com/go-jet/jet/v2/generator/...,github.com/go-jet/jet/v2/internal/... \
123141
-coverprofile=cover.out 2>&1 | go-junit-report > $TEST_RESULTS/results.xml
124142
125-
# run mariaDB tests. No need to collect coverage, because coverage is already included with mysql tests
143+
# run mariaDB and cockroachdb tests. No need to collect coverage, because coverage is already included with mysql and postgres tests
126144
- run: MY_SQL_SOURCE=MariaDB go test -v ./tests/mysql/
145+
- run: PG_SOURCE=COCKROACH_DB go test -v ./tests/postgres/
127146

128147
- save_cache:
129148
key: go-mod-v4-{{ checksum "go.sum" }}

generator/postgres/query_set.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@ WITH primaryKeys AS (
3535
SELECT column_name
3636
FROM information_schema.key_column_usage AS c
3737
LEFT JOIN information_schema.table_constraints AS t
38-
ON t.constraint_name = c.constraint_name
38+
ON t.constraint_name = c.constraint_name AND
39+
c.table_schema = t.table_schema AND
40+
c.table_name = t.table_name
3941
WHERE t.table_schema = $1 AND t.table_name = $2 AND t.constraint_type = 'PRIMARY KEY'
4042
)
4143
SELECT column_name as "column.Name",

internal/testutils/test_utils.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package testutils
22

33
import (
44
"bytes"
5+
"context"
6+
"database/sql"
57
"encoding/json"
68
"fmt"
79
"github.com/go-jet/jet/v2/internal/jet"
@@ -25,6 +27,18 @@ var UnixTimeComparer = cmp.Comparer(func(t1, t2 time.Time) bool {
2527
return t1.Unix() == t2.Unix()
2628
})
2729

30+
// AssertExecAndRollback will execute and rollback statement in sql transaction
31+
func AssertExecAndRollback(t *testing.T, stmt jet.Statement, db *sql.DB, rowsAffected ...int64) {
32+
tx, err := db.Begin()
33+
require.NoError(t, err)
34+
defer func() {
35+
err := tx.Rollback()
36+
require.NoError(t, err)
37+
}()
38+
39+
AssertExec(t, stmt, tx, rowsAffected...)
40+
}
41+
2842
// AssertExec assert statement execution for successful execution and number of rows affected
2943
func AssertExec(t *testing.T, stmt jet.Statement, db qrm.DB, rowsAffected ...int64) {
3044
res, err := stmt.Exec(db)
@@ -38,13 +52,32 @@ func AssertExec(t *testing.T, stmt jet.Statement, db qrm.DB, rowsAffected ...int
3852
}
3953
}
4054

55+
// ExecuteInTxAndRollback will execute function in sql transaction and then rollback transaction
56+
func ExecuteInTxAndRollback(t *testing.T, db *sql.DB, f func(tx *sql.Tx)) {
57+
tx, err := db.Begin()
58+
require.NoError(t, err)
59+
defer func() {
60+
err := tx.Rollback()
61+
require.NoError(t, err)
62+
}()
63+
64+
f(tx)
65+
}
66+
4167
// AssertExecErr assert statement execution for failed execution with error string errorStr
4268
func AssertExecErr(t *testing.T, stmt jet.Statement, db qrm.DB, errorStr string) {
4369
_, err := stmt.Exec(db)
4470

4571
require.Error(t, err, errorStr)
4672
}
4773

74+
// AssertExecContextErr assert statement execution for failed execution with error string errorStr
75+
func AssertExecContextErr(t *testing.T, stmt jet.Statement, ctx context.Context, db qrm.DB, errorStr string) {
76+
_, err := stmt.ExecContext(ctx, db)
77+
78+
require.Error(t, err, errorStr)
79+
}
80+
4881
func getFullPath(relativePath string) string {
4982
path, _ := os.Getwd()
5083
return filepath.Join(path, "../", relativePath)

postgres/cast_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
func TestExpressionCAST_AS(t *testing.T) {
8-
assertSerialize(t, CAST(String("test")).AS("text"), `$1::text`, "test")
8+
assertSerialize(t, CAST(Int(11)).AS("text"), `$1::text`, int64(11))
99
}
1010

1111
func TestExpressionCAST_AS_BOOL(t *testing.T) {

postgres/dialect_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ import "testing"
44

55
func TestString_REGEXP_LIKE_operator(t *testing.T) {
66
assertSerialize(t, table3StrCol.REGEXP_LIKE(table2ColStr), "(table3.col2 ~* table2.col_str)")
7-
assertSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN")), "(table3.col2 ~* $1)", "JOHN")
8-
assertSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), false), "(table3.col2 ~* $1)", "JOHN")
9-
assertSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), true), "(table3.col2 ~ $1)", "JOHN")
7+
assertSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN")), "(table3.col2 ~* $1::text)", "JOHN")
8+
assertSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), false), "(table3.col2 ~* $1::text)", "JOHN")
9+
assertSerialize(t, table3StrCol.REGEXP_LIKE(String("JOHN"), true), "(table3.col2 ~ $1::text)", "JOHN")
1010
}
1111

1212
func TestString_NOT_REGEXP_LIKE_operator(t *testing.T) {
1313
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(table2ColStr), "(table3.col2 !~* table2.col_str)")
14-
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN")), "(table3.col2 !~* $1)", "JOHN")
15-
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN"), false), "(table3.col2 !~* $1)", "JOHN")
16-
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN"), true), "(table3.col2 !~ $1)", "JOHN")
14+
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN")), "(table3.col2 !~* $1::text)", "JOHN")
15+
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN"), false), "(table3.col2 !~* $1::text)", "JOHN")
16+
assertSerialize(t, table3StrCol.NOT_REGEXP_LIKE(String("JOHN"), true), "(table3.col2 !~ $1::text)", "JOHN")
1717
}
1818

1919
func TestExists(t *testing.T) {

postgres/expressions_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ func TestRawHelperMethods(t *testing.T) {
6060
assertSerialize(t, RawFloat("table.colInt + :float", RawArgs{":float": 11.22}).EQ(Float(3.14)),
6161
"((table.colInt + $1) = $2)", 11.22, 3.14)
6262
assertSerialize(t, RawString("table.colStr || str", RawArgs{"str": "doe"}).EQ(String("john doe")),
63-
"((table.colStr || $1) = $2)", "doe", "john doe")
63+
"((table.colStr || $1) = $2::text)", "doe", "john doe")
6464

6565
now := time.Now()
6666
assertSerialize(t, RawTime("table.colTime").EQ(TimeT(now)),

postgres/insert_statement_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ VALUES ('one', 'two'),
167167
ON CONFLICT (col_bool) WHERE col_bool IS NOT FALSE DO UPDATE
168168
SET col_bool = TRUE::boolean,
169169
col_int = 1,
170-
(col1, col_bool) = ROW(2, 'two')
170+
(col1, col_bool) = ROW(2, 'two'::text)
171171
WHERE table1.col1 > 2
172172
RETURNING table1.col1 AS "table1.col1",
173173
table1.col_bool AS "table1.col_bool";
@@ -193,7 +193,7 @@ VALUES ('one', 'two'),
193193
ON CONFLICT ON CONSTRAINT idk_primary_key DO UPDATE
194194
SET col_bool = FALSE::boolean,
195195
col_int = 1,
196-
(col1, col_bool) = ROW(2, 'two')
196+
(col1, col_bool) = ROW(2, 'two'::text)
197197
WHERE table1.col1 > 2
198198
RETURNING table1.col1 AS "table1.col1",
199199
table1.col_bool AS "table1.col_bool";

postgres/literal.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,9 @@ var Float = jet.Float
6161
var Decimal = jet.Decimal
6262

6363
// String creates new string literal expression
64-
var String = jet.String
64+
func String(value string) StringExpression {
65+
return CAST(jet.String(value)).AS_TEXT()
66+
}
6567

6668
// UUID is a helper function to create string literal expression from uuid object
6769
// value can be any uuid type with a String method

postgres/literal_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ func TestFloat(t *testing.T) {
5959
}
6060

6161
func TestString(t *testing.T) {
62-
assertSerialize(t, String("Some text"), `$1`, "Some text")
62+
assertSerialize(t, String("Some text"), `$1::text`, "Some text")
6363
}
6464

6565
func TestBytea(t *testing.T) {

tests/Makefile

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ jet-gen-postgres:
3939
jet -dsn=postgres://jet:jet@localhost:50901/jetdb?sslmode=disable -schema=dvds -path=./.gentestdata/
4040
jet -dsn=postgres://jet:jet@localhost:50901/jetdb?sslmode=disable -schema=chinook -path=./.gentestdata/
4141
jet -dsn=postgres://jet:jet@localhost:50901/jetdb?sslmode=disable -schema=chinook2 -path=./.gentestdata/
42+
jet -dsn=postgres://jet:jet@localhost:50901/jetdb?sslmode=disable -schema=northwind -path=./.gentestdata/
4243
jet -dsn=postgres://jet:jet@localhost:50901/jetdb?sslmode=disable -schema=test_sample -path=./.gentestdata/
4344

4445
jet-gen-mysql:
@@ -56,6 +57,12 @@ jet-gen-sqlite:
5657
jet -source=sqlite -dsn="./testdata/init/sqlite/sakila.db" -schema=dvds -path=./.gentestdata/sqlite/sakila
5758
jet -source=sqlite -dsn="./testdata/init/sqlite/test_sample.db" -schema=dvds -path=./.gentestdata/sqlite/test_sample
5859

60+
jet-gen-cockroach:
61+
jet -dsn=postgres://jet:[email protected]:26257/jetdb?sslmode=disable -schema=dvds -path=./.gentestdata/
62+
jet -dsn=postgres://jet:jet@localhost:26257/jetdb?sslmode=disable -schema=chinook -path=./.gentestdata/
63+
jet -dsn=postgres://jet:jet@localhost:26257/jetdb?sslmode=disable -schema=chinook2 -path=./.gentestdata/
64+
jet -dsn=postgres://jet:jet@localhost:26257/jetdb?sslmode=disable -schema=northwind -path=./.gentestdata/
65+
jet -dsn=postgres://jet:jet@localhost:26257/jetdb?sslmode=disable -schema=test_sample -path=./.gentestdata/
5966

6067
# docker-compose-cleanup will stop and remove test containers, volumes, and images.
6168
cleanup:

0 commit comments

Comments
 (0)