Add support for CockorachDB.
This commit is contained in:
parent
3ff9241eea
commit
bc776f947b
33 changed files with 1040 additions and 1037 deletions
|
|
@ -2,6 +2,7 @@ package postgres
|
|||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
. "github.com/go-jet/jet/v2/postgres"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/jetdb/test_sample/model"
|
||||
|
|
@ -13,9 +14,13 @@ import (
|
|||
)
|
||||
|
||||
func TestInsertValues(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
insertQuery := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
||||
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES(101, "http://www.google.com", "Google", DEFAULT).
|
||||
VALUES(102, "http://www.yahoo.com", "Yahoo", nil).
|
||||
RETURNING(Link.AllColumns)
|
||||
|
||||
var expectedSQL = `
|
||||
testutils.AssertDebugStatementSql(t, insertQuery, `
|
||||
INSERT INTO test_sample.link (id, url, name, description)
|
||||
VALUES (100, 'http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT),
|
||||
(101, 'http://www.google.com', 'Google', DEFAULT),
|
||||
|
|
@ -24,76 +29,61 @@ RETURNING link.id AS "link.id",
|
|||
link.url AS "link.url",
|
||||
link.name AS "link.name",
|
||||
link.description AS "link.description";
|
||||
`
|
||||
insertQuery := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
||||
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES(101, "http://www.google.com", "Google", DEFAULT).
|
||||
VALUES(102, "http://www.yahoo.com", "Yahoo", nil).
|
||||
RETURNING(Link.AllColumns)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, insertQuery, expectedSQL,
|
||||
`,
|
||||
100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
|
||||
101, "http://www.google.com", "Google",
|
||||
102, "http://www.yahoo.com", "Yahoo", nil)
|
||||
|
||||
insertedLinks := []model.Link{}
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
var insertedLinks []model.Link
|
||||
|
||||
err := insertQuery.Query(db, &insertedLinks)
|
||||
err := insertQuery.Query(tx, &insertedLinks)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(insertedLinks), 3)
|
||||
testutils.AssertDeepEqual(t, insertedLinks[0], model.Link{
|
||||
ID: 100,
|
||||
URL: "http://www.postgresqltutorial.com",
|
||||
Name: "PostgreSQL Tutorial",
|
||||
})
|
||||
testutils.AssertDeepEqual(t, insertedLinks[1], model.Link{
|
||||
ID: 101,
|
||||
URL: "http://www.google.com",
|
||||
Name: "Google",
|
||||
})
|
||||
testutils.AssertDeepEqual(t, insertedLinks[2], model.Link{
|
||||
ID: 102,
|
||||
URL: "http://www.yahoo.com",
|
||||
Name: "Yahoo",
|
||||
})
|
||||
|
||||
require.Equal(t, len(insertedLinks), 3)
|
||||
var allLinks []model.Link
|
||||
|
||||
testutils.AssertDeepEqual(t, insertedLinks[0], model.Link{
|
||||
ID: 100,
|
||||
URL: "http://www.postgresqltutorial.com",
|
||||
Name: "PostgreSQL Tutorial",
|
||||
err = Link.SELECT(Link.AllColumns).
|
||||
WHERE(Link.ID.BETWEEN(Int(100), Int(199))).
|
||||
ORDER_BY(Link.ID).
|
||||
Query(tx, &allLinks)
|
||||
|
||||
require.NoError(t, err)
|
||||
testutils.AssertDeepEqual(t, insertedLinks, allLinks)
|
||||
})
|
||||
|
||||
testutils.AssertDeepEqual(t, insertedLinks[1], model.Link{
|
||||
ID: 101,
|
||||
URL: "http://www.google.com",
|
||||
Name: "Google",
|
||||
})
|
||||
|
||||
testutils.AssertDeepEqual(t, insertedLinks[2], model.Link{
|
||||
ID: 102,
|
||||
URL: "http://www.yahoo.com",
|
||||
Name: "Yahoo",
|
||||
})
|
||||
|
||||
allLinks := []model.Link{}
|
||||
|
||||
err = Link.SELECT(Link.AllColumns).
|
||||
WHERE(Link.ID.GT_EQ(Int(100))).
|
||||
ORDER_BY(Link.ID).
|
||||
Query(db, &allLinks)
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
testutils.AssertDeepEqual(t, insertedLinks, allLinks)
|
||||
}
|
||||
|
||||
func TestInsertEmptyColumnList(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
|
||||
expectedSQL := `
|
||||
INSERT INTO test_sample.link
|
||||
VALUES (100, 'http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT);
|
||||
`
|
||||
|
||||
stmt := Link.INSERT().
|
||||
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, expectedSQL,
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
INSERT INTO test_sample.link
|
||||
VALUES (100, 'http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT);
|
||||
`,
|
||||
100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial")
|
||||
|
||||
AssertExec(t, stmt, 1)
|
||||
testutils.AssertExecAndRollback(t, stmt, db, 1)
|
||||
requireLogged(t, stmt)
|
||||
}
|
||||
|
||||
func TestInsertOnConflict(t *testing.T) {
|
||||
|
||||
t.Run("do nothing", func(t *testing.T) {
|
||||
employee := model.Employee{EmployeeID: rand.Int31()}
|
||||
|
||||
|
|
@ -108,11 +98,12 @@ VALUES ($1, $2, $3, $4, $5),
|
|||
($6, $7, $8, $9, $10)
|
||||
ON CONFLICT (employee_id) DO NOTHING;
|
||||
`)
|
||||
AssertExec(t, stmt, 1)
|
||||
testutils.AssertExecAndRollback(t, stmt, db, 1)
|
||||
requireLogged(t, stmt)
|
||||
})
|
||||
|
||||
t.Run("on constraint do nothing", func(t *testing.T) {
|
||||
skipForCockroachDB(t) // does not support
|
||||
employee := model.Employee{EmployeeID: rand.Int31()}
|
||||
|
||||
stmt := Employee.INSERT(Employee.AllColumns).
|
||||
|
|
@ -126,12 +117,11 @@ VALUES ($1, $2, $3, $4, $5),
|
|||
($6, $7, $8, $9, $10)
|
||||
ON CONFLICT ON CONSTRAINT employee_pkey DO NOTHING;
|
||||
`)
|
||||
AssertExec(t, stmt, 1)
|
||||
testutils.AssertExecAndRollback(t, stmt, db, 1)
|
||||
requireLogged(t, stmt)
|
||||
})
|
||||
|
||||
t.Run("do update", func(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
stmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
||||
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES(200, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
|
|
@ -148,18 +138,19 @@ VALUES ($1, $2, $3, DEFAULT),
|
|||
($4, $5, $6, DEFAULT)
|
||||
ON CONFLICT (id) DO UPDATE
|
||||
SET id = excluded.id,
|
||||
url = $7
|
||||
url = $7::text
|
||||
RETURNING link.id AS "link.id",
|
||||
link.url AS "link.url",
|
||||
link.name AS "link.name",
|
||||
link.description AS "link.description";
|
||||
`)
|
||||
|
||||
AssertExec(t, stmt, 2)
|
||||
testutils.AssertExecAndRollback(t, stmt, db, 2)
|
||||
})
|
||||
|
||||
t.Run("on constraint do update", func(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
skipForCockroachDB(t) // does not support
|
||||
|
||||
stmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
||||
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
VALUES(200, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
|
|
@ -177,18 +168,18 @@ VALUES ($1, $2, $3, DEFAULT),
|
|||
($4, $5, $6, DEFAULT)
|
||||
ON CONFLICT ON CONSTRAINT link_pkey DO UPDATE
|
||||
SET id = excluded.id,
|
||||
url = $7
|
||||
url = $7::text
|
||||
RETURNING link.id AS "link.id",
|
||||
link.url AS "link.url",
|
||||
link.name AS "link.name",
|
||||
link.description AS "link.description";
|
||||
`)
|
||||
|
||||
AssertExec(t, stmt, 2)
|
||||
testutils.AssertExecAndRollback(t, stmt, db, 2)
|
||||
})
|
||||
|
||||
t.Run("do update complex", func(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
skipForCockroachDB(t) // does not support ROW
|
||||
|
||||
stmt := Link.INSERT(Link.ID, Link.URL, Link.Name, Link.Description).
|
||||
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
|
|
@ -210,21 +201,15 @@ ON CONFLICT (id) WHERE (id * 2) > 10 DO UPDATE
|
|||
SELECT MAX(link.id) + 1
|
||||
FROM test_sample.link
|
||||
),
|
||||
(name, description) = ROW(excluded.name, 'new description')
|
||||
(name, description) = ROW(excluded.name, 'new description'::text)
|
||||
WHERE link.description IS NOT NULL;
|
||||
`)
|
||||
|
||||
AssertExec(t, stmt, 1)
|
||||
testutils.AssertExecAndRollback(t, stmt, db, 1)
|
||||
})
|
||||
}
|
||||
|
||||
func TestInsertModelObject(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
var expectedSQL = `
|
||||
INSERT INTO test_sample.link (url, name)
|
||||
VALUES ('http://www.duckduckgo.com', 'Duck Duck go');
|
||||
`
|
||||
|
||||
linkData := model.Link{
|
||||
URL: "http://www.duckduckgo.com",
|
||||
Name: "Duck Duck go",
|
||||
|
|
@ -234,18 +219,15 @@ VALUES ('http://www.duckduckgo.com', 'Duck Duck go');
|
|||
INSERT(Link.URL, Link.Name).
|
||||
MODEL(linkData)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL, "http://www.duckduckgo.com", "Duck Duck go")
|
||||
testutils.AssertDebugStatementSql(t, query, `
|
||||
INSERT INTO test_sample.link (url, name)
|
||||
VALUES ('http://www.duckduckgo.com', 'Duck Duck go');
|
||||
`, "http://www.duckduckgo.com", "Duck Duck go")
|
||||
|
||||
AssertExec(t, query, 1)
|
||||
testutils.AssertExecAndRollback(t, query, db, 1)
|
||||
}
|
||||
|
||||
func TestInsertModelObjectEmptyColumnList(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
var expectedSQL = `
|
||||
INSERT INTO test_sample.link
|
||||
VALUES (1000, 'http://www.duckduckgo.com', 'Duck Duck go', NULL);
|
||||
`
|
||||
|
||||
linkData := model.Link{
|
||||
ID: 1000,
|
||||
URL: "http://www.duckduckgo.com",
|
||||
|
|
@ -256,19 +238,16 @@ VALUES (1000, 'http://www.duckduckgo.com', 'Duck Duck go', NULL);
|
|||
INSERT().
|
||||
MODEL(linkData)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL, int32(1000), "http://www.duckduckgo.com", "Duck Duck go", nil)
|
||||
testutils.AssertDebugStatementSql(t, query, `
|
||||
INSERT INTO test_sample.link
|
||||
VALUES (1000, 'http://www.duckduckgo.com', 'Duck Duck go', NULL);
|
||||
`,
|
||||
int64(1000), "http://www.duckduckgo.com", "Duck Duck go", nil)
|
||||
|
||||
AssertExec(t, query, 1)
|
||||
testutils.AssertExecAndRollback(t, query, db, 1)
|
||||
}
|
||||
|
||||
func TestInsertModelsObject(t *testing.T) {
|
||||
expectedSQL := `
|
||||
INSERT INTO test_sample.link (url, name)
|
||||
VALUES ('http://www.postgresqltutorial.com', 'PostgreSQL Tutorial'),
|
||||
('http://www.google.com', 'Google'),
|
||||
('http://www.yahoo.com', 'Yahoo');
|
||||
`
|
||||
|
||||
tutorial := model.Link{
|
||||
URL: "http://www.postgresqltutorial.com",
|
||||
Name: "PostgreSQL Tutorial",
|
||||
|
|
@ -288,23 +267,20 @@ VALUES ('http://www.postgresqltutorial.com', 'PostgreSQL Tutorial'),
|
|||
INSERT(Link.URL, Link.Name).
|
||||
MODELS([]model.Link{tutorial, google, yahoo})
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, expectedSQL,
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
INSERT INTO test_sample.link (url, name)
|
||||
VALUES ('http://www.postgresqltutorial.com', 'PostgreSQL Tutorial'),
|
||||
('http://www.google.com', 'Google'),
|
||||
('http://www.yahoo.com', 'Yahoo');
|
||||
`,
|
||||
"http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
|
||||
"http://www.google.com", "Google",
|
||||
"http://www.yahoo.com", "Yahoo")
|
||||
|
||||
AssertExec(t, stmt, 3)
|
||||
testutils.AssertExecAndRollback(t, stmt, db, 3)
|
||||
}
|
||||
|
||||
func TestInsertUsingMutableColumns(t *testing.T) {
|
||||
var expectedSQL = `
|
||||
INSERT INTO test_sample.link (url, name, description)
|
||||
VALUES ('http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT),
|
||||
('http://www.google.com', 'Google', NULL),
|
||||
('http://www.google.com', 'Google', NULL),
|
||||
('http://www.yahoo.com', 'Yahoo', NULL);
|
||||
`
|
||||
|
||||
google := model.Link{
|
||||
URL: "http://www.google.com",
|
||||
Name: "Google",
|
||||
|
|
@ -321,22 +297,32 @@ VALUES ('http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT),
|
|||
MODEL(google).
|
||||
MODELS([]model.Link{google, yahoo})
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, expectedSQL,
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
INSERT INTO test_sample.link (url, name, description)
|
||||
VALUES ('http://www.postgresqltutorial.com', 'PostgreSQL Tutorial', DEFAULT),
|
||||
('http://www.google.com', 'Google', NULL),
|
||||
('http://www.google.com', 'Google', NULL),
|
||||
('http://www.yahoo.com', 'Yahoo', NULL);
|
||||
`,
|
||||
"http://www.postgresqltutorial.com", "PostgreSQL Tutorial",
|
||||
"http://www.google.com", "Google", nil,
|
||||
"http://www.google.com", "Google", nil,
|
||||
"http://www.yahoo.com", "Yahoo", nil)
|
||||
|
||||
AssertExec(t, stmt, 4)
|
||||
testutils.AssertExecAndRollback(t, stmt, db, 4)
|
||||
}
|
||||
|
||||
func TestInsertQuery(t *testing.T) {
|
||||
_, err := Link.DELETE().
|
||||
WHERE(Link.ID.NOT_EQ(Int(0)).AND(Link.Name.EQ(String("Youtube")))).
|
||||
Exec(db)
|
||||
require.NoError(t, err)
|
||||
query := Link.
|
||||
INSERT(Link.URL, Link.Name).
|
||||
QUERY(
|
||||
SELECT(Link.URL, Link.Name).
|
||||
FROM(Link).
|
||||
WHERE(Link.ID.EQ(Int(0))),
|
||||
).
|
||||
RETURNING(Link.AllColumns)
|
||||
|
||||
var expectedSQL = `
|
||||
testutils.AssertDebugStatementSql(t, query, `
|
||||
INSERT INTO test_sample.link (url, name) (
|
||||
SELECT link.url AS "link.url",
|
||||
link.name AS "link.name"
|
||||
|
|
@ -347,38 +333,26 @@ RETURNING link.id AS "link.id",
|
|||
link.url AS "link.url",
|
||||
link.name AS "link.name",
|
||||
link.description AS "link.description";
|
||||
`
|
||||
`, int64(0))
|
||||
|
||||
query := Link.
|
||||
INSERT(Link.URL, Link.Name).
|
||||
QUERY(
|
||||
SELECT(Link.URL, Link.Name).
|
||||
FROM(Link).
|
||||
WHERE(Link.ID.EQ(Int(0))),
|
||||
).
|
||||
RETURNING(Link.AllColumns)
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
var dest []model.Link
|
||||
|
||||
testutils.AssertDebugStatementSql(t, query, expectedSQL, int64(0))
|
||||
err := query.Query(tx, &dest)
|
||||
require.NoError(t, err)
|
||||
|
||||
dest := []model.Link{}
|
||||
var youtubeLinks []model.Link
|
||||
err = Link.
|
||||
SELECT(Link.AllColumns).
|
||||
WHERE(Link.Name.EQ(String("Youtube"))).
|
||||
Query(tx, &youtubeLinks)
|
||||
|
||||
err = query.Query(db, &dest)
|
||||
|
||||
require.NoError(t, err)
|
||||
|
||||
youtubeLinks := []model.Link{}
|
||||
err = Link.
|
||||
SELECT(Link.AllColumns).
|
||||
WHERE(Link.Name.EQ(String("Youtube"))).
|
||||
Query(db, &youtubeLinks)
|
||||
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(youtubeLinks), 2)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, len(youtubeLinks), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func TestInsertWithQueryContext(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
|
||||
stmt := Link.INSERT().
|
||||
VALUES(1100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT).
|
||||
RETURNING(Link.AllColumns)
|
||||
|
|
@ -388,15 +362,15 @@ func TestInsertWithQueryContext(t *testing.T) {
|
|||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
dest := []model.Link{}
|
||||
err := stmt.QueryContext(ctx, db, &dest)
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
dest := []model.Link{}
|
||||
err := stmt.QueryContext(ctx, tx, &dest)
|
||||
|
||||
require.Error(t, err, "context deadline exceeded")
|
||||
require.Error(t, err, "context deadline exceeded")
|
||||
})
|
||||
}
|
||||
|
||||
func TestInsertWithExecContext(t *testing.T) {
|
||||
cleanUpLinkTable(t)
|
||||
|
||||
stmt := Link.INSERT().
|
||||
VALUES(100, "http://www.postgresqltutorial.com", "PostgreSQL Tutorial", DEFAULT)
|
||||
|
||||
|
|
@ -405,7 +379,7 @@ func TestInsertWithExecContext(t *testing.T) {
|
|||
|
||||
time.Sleep(10 * time.Millisecond)
|
||||
|
||||
_, err := stmt.ExecContext(ctx, db)
|
||||
|
||||
require.Error(t, err, "context deadline exceeded")
|
||||
testutils.ExecuteInTxAndRollback(t, db, func(tx *sql.Tx) {
|
||||
testutils.AssertExecContextErr(t, stmt, ctx, tx, "context deadline exceeded")
|
||||
})
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue