fix -ignore-tables, -ignore-enums and -ignore-views when -dsn is present

This commit is contained in:
Nikita Konin 2022-04-13 20:47:56 +03:00
parent f17d05e823
commit 971e2df442
3 changed files with 138 additions and 78 deletions

View file

@ -3,6 +3,9 @@ package main
import ( import (
"flag" "flag"
"fmt" "fmt"
"os"
"strings"
"github.com/go-jet/jet/v2/generator/metadata" "github.com/go-jet/jet/v2/generator/metadata"
sqlitegen "github.com/go-jet/jet/v2/generator/sqlite" sqlitegen "github.com/go-jet/jet/v2/generator/sqlite"
"github.com/go-jet/jet/v2/generator/template" "github.com/go-jet/jet/v2/generator/template"
@ -11,8 +14,6 @@ import (
"github.com/go-jet/jet/v2/mysql" "github.com/go-jet/jet/v2/mysql"
postgres2 "github.com/go-jet/jet/v2/postgres" postgres2 "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/sqlite" "github.com/go-jet/jet/v2/sqlite"
"os"
"strings"
mysqlgen "github.com/go-jet/jet/v2/generator/mysql" mysqlgen "github.com/go-jet/jet/v2/generator/mysql"
postgresgen "github.com/go-jet/jet/v2/generator/postgres" postgresgen "github.com/go-jet/jet/v2/generator/postgres"
@ -112,8 +113,9 @@ func main() {
switch source { switch source {
case "postgresql", "postgres": case "postgresql", "postgres":
generatorTemplate := genTemplate(postgres2.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList)
if dsn != "" { if dsn != "" {
err = postgresgen.GenerateDSN(dsn, schemaName, destDir) err = postgresgen.GenerateDSN(dsn, schemaName, destDir, generatorTemplate)
break break
} }
dbConn := postgresgen.DBConnection{ dbConn := postgresgen.DBConnection{
@ -131,12 +133,13 @@ func main() {
err = postgresgen.Generate( err = postgresgen.Generate(
destDir, destDir,
dbConn, dbConn,
genTemplate(postgres2.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList), generatorTemplate,
) )
case "mysql", "mysqlx", "mariadb": case "mysql", "mysqlx", "mariadb":
generatorTemplate := genTemplate(mysql.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList)
if dsn != "" { if dsn != "" {
err = mysqlgen.GenerateDSN(dsn, destDir) err = mysqlgen.GenerateDSN(dsn, destDir, generatorTemplate)
break break
} }
dbConn := mysqlgen.DBConnection{ dbConn := mysqlgen.DBConnection{
@ -151,7 +154,7 @@ func main() {
err = mysqlgen.Generate( err = mysqlgen.Generate(
destDir, destDir,
dbConn, dbConn,
genTemplate(mysql.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList), generatorTemplate,
) )
case "sqlite": case "sqlite":
if dsn == "" { if dsn == "" {

View file

@ -88,7 +88,23 @@ func TestCmdGenerator(t *testing.T) {
} }
func TestIgnoreTablesViewsEnums(t *testing.T) { func TestIgnoreTablesViewsEnums(t *testing.T) {
cmd := exec.Command("jet", tests := []struct {
name string
args []string
}{
{
name: "with dsn",
args: []string{
"-dsn=mysql://" + dbconfig.MySQLConnectionString(sourceIsMariaDB(), "dvds"),
"-ignore-tables=actor,ADDRESS,Category, city ,country,staff,store,rental",
"-ignore-views=actor_info,CUSTomER_LIST, film_list",
"-ignore-enums=film_list_rating,film_rating",
"-path=" + genTestDir3,
},
},
{
name: "without dsn",
args: []string{
"-source=MySQL", "-source=MySQL",
"-dbname=dvds", "-dbname=dvds",
"-host=" + dbconfig.MySqLHost, "-host=" + dbconfig.MySqLHost,
@ -98,7 +114,14 @@ func TestIgnoreTablesViewsEnums(t *testing.T) {
"-ignore-tables=actor,ADDRESS,Category, city ,country,staff,store,rental", "-ignore-tables=actor,ADDRESS,Category, city ,country,staff,store,rental",
"-ignore-views=actor_info,CUSTomER_LIST, film_list", "-ignore-views=actor_info,CUSTomER_LIST, film_list",
"-ignore-enums=film_list_rating,film_rating", "-ignore-enums=film_list_rating,film_rating",
"-path="+genTestDir3) "-path=" + genTestDir3,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command("jet", tt.args...)
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout cmd.Stdout = os.Stdout
@ -127,6 +150,8 @@ func TestIgnoreTablesViewsEnums(t *testing.T) {
"customer.go", "film.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go", "customer.go", "film.go", "film_actor.go", "film_category.go", "film_text.go", "inventory.go", "language.go",
"payment.go", "nicer_but_slower_film_list_rating.go", "nicer_but_slower_film_list.go", "sales_by_film_category.go", "payment.go", "nicer_but_slower_film_list_rating.go", "nicer_but_slower_film_list.go", "sales_by_film_category.go",
"sales_by_store.go", "staff_list.go") "sales_by_store.go", "staff_list.go")
})
}
} }
func assertGeneratedFiles(t *testing.T) { func assertGeneratedFiles(t *testing.T) {

View file

@ -93,10 +93,30 @@ func TestCmdGenerator(t *testing.T) {
} }
func TestGeneratorIgnoreTables(t *testing.T) { func TestGeneratorIgnoreTables(t *testing.T) {
err := os.RemoveAll(genTestDir2) tests := []struct {
require.NoError(t, err) name string
args []string
cmd := exec.Command("jet", }{
{
name: "with dsn",
args: []string{
"-dsn=" + fmt.Sprintf("postgresql://%s:%s@%s:%d/%s?sslmode=disable",
dbconfig.PgUser,
dbconfig.PgPassword,
dbconfig.PgHost,
dbconfig.PgPort,
"jetdb",
),
"-schema=dvds",
"-ignore-tables=actor,ADDRESS,country, Film , cITY,",
"-ignore-views=Actor_info, FILM_LIST ,staff_list",
"-ignore-enums=mpaa_rating",
"-path=" + genTestDir2,
},
},
{
name: "without dsn",
args: []string{
"-source=PostgreSQL", "-source=PostgreSQL",
"-host=localhost", "-host=localhost",
"-port=" + strconv.Itoa(dbconfig.PgPort), "-port=" + strconv.Itoa(dbconfig.PgPort),
@ -107,7 +127,17 @@ func TestGeneratorIgnoreTables(t *testing.T) {
"-ignore-tables=actor,ADDRESS,country, Film , cITY,", "-ignore-tables=actor,ADDRESS,country, Film , cITY,",
"-ignore-views=Actor_info, FILM_LIST ,staff_list", "-ignore-views=Actor_info, FILM_LIST ,staff_list",
"-ignore-enums=mpaa_rating", "-ignore-enums=mpaa_rating",
"-path="+genTestDir2) "-path=" + genTestDir2,
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := os.RemoveAll(genTestDir2)
require.NoError(t, err)
cmd := exec.Command("jet", tt.args...)
fmt.Println(cmd.Args) fmt.Println(cmd.Args)
cmd.Stderr = os.Stderr cmd.Stderr = os.Stderr
@ -143,6 +173,8 @@ func TestGeneratorIgnoreTables(t *testing.T) {
"payment.go", "rental.go", "staff.go", "store.go", "payment.go", "rental.go", "staff.go", "store.go",
"nicer_but_slower_film_list.go", "sales_by_film_category.go", "nicer_but_slower_film_list.go", "sales_by_film_category.go",
"customer_list.go", "sales_by_store.go") "customer_list.go", "sales_by_store.go")
})
}
} }
func TestGenerator(t *testing.T) { func TestGenerator(t *testing.T) {