Add schema rename support
Using SchemaFrom("schemaName") it is possible to set SQL builder table to point to a different schema.
This commit is contained in:
parent
38776e35ab
commit
fae8dde639
17 changed files with 206 additions and 153 deletions
|
|
@ -1,7 +1,6 @@
|
|||
package mysql
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/stretchr/testify/require"
|
||||
"strings"
|
||||
"testing"
|
||||
|
|
@ -974,7 +973,7 @@ func TestAllTypesInsert(t *testing.T) {
|
|||
stmt := AllTypes.INSERT(AllTypes.AllColumns).
|
||||
MODEL(toInsert)
|
||||
|
||||
fmt.Println(stmt.DebugSql())
|
||||
//fmt.Println(stmt.DebugSql())
|
||||
|
||||
testutils.AssertExec(t, stmt, tx, 1)
|
||||
|
||||
|
|
@ -1028,7 +1027,7 @@ func TestAllTypesInsertOnDuplicateKeyUpdate(t *testing.T) {
|
|||
AllTypes.Date.SET(DateT(time.Now())),
|
||||
)
|
||||
|
||||
fmt.Println(stmt.DebugSql())
|
||||
//fmt.Println(stmt.DebugSql())
|
||||
|
||||
_, err = stmt.Exec(tx)
|
||||
require.NoError(t, err)
|
||||
|
|
@ -1257,7 +1256,7 @@ FROM test_sample.user;
|
|||
err := stmt.Query(db, &dest)
|
||||
require.NoError(t, err)
|
||||
|
||||
testutils.PrintJson(dest)
|
||||
//testutils.PrintJson(dest)
|
||||
|
||||
testutils.AssertJSON(t, dest, `
|
||||
[
|
||||
|
|
|
|||
|
|
@ -135,7 +135,7 @@ import (
|
|||
"github.com/go-jet/jet/v2/mysql"
|
||||
)
|
||||
|
||||
var Actor = newActorTable()
|
||||
var Actor = newActorTable("dvds", "actor", "")
|
||||
|
||||
type ActorTable struct {
|
||||
mysql.Table
|
||||
|
|
@ -151,13 +151,16 @@ type ActorTable struct {
|
|||
}
|
||||
|
||||
// AS creates new ActorTable with assigned alias
|
||||
func (a *ActorTable) AS(alias string) ActorTable {
|
||||
aliasTable := newActorTable()
|
||||
aliasTable.Table.AS(alias)
|
||||
return aliasTable
|
||||
func (a ActorTable) AS(alias string) ActorTable {
|
||||
return newActorTable(a.SchemaName(), a.TableName(), alias)
|
||||
}
|
||||
|
||||
func newActorTable() ActorTable {
|
||||
// Schema creates new ActorTable with assigned schema name
|
||||
func (a ActorTable) FromSchema(schemaName string) ActorTable {
|
||||
return newActorTable(schemaName, a.TableName(), a.Alias())
|
||||
}
|
||||
|
||||
func newActorTable(schemaName, tableName, alias string) ActorTable {
|
||||
var (
|
||||
ActorIDColumn = mysql.IntegerColumn("actor_id")
|
||||
FirstNameColumn = mysql.StringColumn("first_name")
|
||||
|
|
@ -168,7 +171,7 @@ func newActorTable() ActorTable {
|
|||
)
|
||||
|
||||
return ActorTable{
|
||||
Table: mysql.NewTable("dvds", "actor", allColumns...),
|
||||
Table: mysql.NewTable(schemaName, tableName, alias, allColumns...),
|
||||
|
||||
//Columns
|
||||
ActorID: ActorIDColumn,
|
||||
|
|
@ -218,7 +221,7 @@ import (
|
|||
"github.com/go-jet/jet/v2/mysql"
|
||||
)
|
||||
|
||||
var ActorInfo = newActorInfoTable()
|
||||
var ActorInfo = newActorInfoTable("dvds", "actor_info", "")
|
||||
|
||||
type ActorInfoTable struct {
|
||||
mysql.Table
|
||||
|
|
@ -234,13 +237,16 @@ type ActorInfoTable struct {
|
|||
}
|
||||
|
||||
// AS creates new ActorInfoTable with assigned alias
|
||||
func (a *ActorInfoTable) AS(alias string) ActorInfoTable {
|
||||
aliasTable := newActorInfoTable()
|
||||
aliasTable.Table.AS(alias)
|
||||
return aliasTable
|
||||
func (a ActorInfoTable) AS(alias string) ActorInfoTable {
|
||||
return newActorInfoTable(a.SchemaName(), a.TableName(), alias)
|
||||
}
|
||||
|
||||
func newActorInfoTable() ActorInfoTable {
|
||||
// Schema creates new ActorInfoTable with assigned schema name
|
||||
func (a ActorInfoTable) FromSchema(schemaName string) ActorInfoTable {
|
||||
return newActorInfoTable(schemaName, a.TableName(), a.Alias())
|
||||
}
|
||||
|
||||
func newActorInfoTable(schemaName, tableName, alias string) ActorInfoTable {
|
||||
var (
|
||||
ActorIDColumn = mysql.IntegerColumn("actor_id")
|
||||
FirstNameColumn = mysql.StringColumn("first_name")
|
||||
|
|
@ -251,7 +257,7 @@ func newActorInfoTable() ActorInfoTable {
|
|||
)
|
||||
|
||||
return ActorInfoTable{
|
||||
Table: mysql.NewTable("dvds", "actor_info", allColumns...),
|
||||
Table: mysql.NewTable(schemaName, tableName, alias, allColumns...),
|
||||
|
||||
//Columns
|
||||
ActorID: ActorIDColumn,
|
||||
|
|
|
|||
|
|
@ -744,3 +744,46 @@ LIMIT 3;
|
|||
|
||||
require.Equal(t, len(dest), 3)
|
||||
}
|
||||
|
||||
func Test_SchemaRename(t *testing.T) {
|
||||
Film := Film.FromSchema("dvds2")
|
||||
Language := Language.FromSchema("dvds2")
|
||||
|
||||
stmt := SELECT(
|
||||
Film.FilmID,
|
||||
Film.Title,
|
||||
Language.LanguageID,
|
||||
Language.Name,
|
||||
).FROM(
|
||||
Language.
|
||||
INNER_JOIN(Film, Film.LanguageID.EQ(Language.LanguageID)),
|
||||
).WHERE(
|
||||
Language.LanguageID.EQ(Int(1)),
|
||||
).ORDER_BY(
|
||||
Language.LanguageID, Film.FilmID,
|
||||
).LIMIT(5)
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, `
|
||||
SELECT film.film_id AS "film.film_id",
|
||||
film.title AS "film.title",
|
||||
language.language_id AS "language.language_id",
|
||||
language.name AS "language.name"
|
||||
FROM dvds2.language
|
||||
INNER JOIN dvds2.film ON (film.language_id = language.language_id)
|
||||
WHERE language.language_id = 1
|
||||
ORDER BY language.language_id, film.film_id
|
||||
LIMIT 5;
|
||||
`)
|
||||
|
||||
dest := struct {
|
||||
model.Language
|
||||
Films []model.Film
|
||||
}{}
|
||||
|
||||
err := stmt.Query(db, &dest)
|
||||
require.NoError(t, err)
|
||||
require.Len(t, dest.Films, 5)
|
||||
require.Equal(t, dest.Films[0].Title, "ACADEMY DINOSAUR")
|
||||
require.Equal(t, dest.Films[1].Title, "ACE GOLDFINGER")
|
||||
require.Equal(t, dest.Films[4].Title, "AFRICAN EGG")
|
||||
}
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ package mysql
|
|||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-jet/jet/v2/internal/testutils"
|
||||
. "github.com/go-jet/jet/v2/mysql"
|
||||
"github.com/go-jet/jet/v2/tests/.gentestdata/mysql/dvds/table"
|
||||
|
|
@ -193,7 +192,7 @@ SET url = 'http://www.duckduckgo.com',
|
|||
description = NULL
|
||||
WHERE link.id = 201;
|
||||
`
|
||||
fmt.Println(stmt.DebugSql())
|
||||
//fmt.Println(stmt.DebugSql())
|
||||
|
||||
testutils.AssertDebugStatementSql(t, stmt, expectedSQL, "http://www.duckduckgo.com", "DuckDuckGo", nil, int64(201))
|
||||
testutils.AssertExec(t, stmt, db)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue