Skip to content

Commit

Permalink
Merge pull request #138 from nkonin/fix/dsn-ignore-tables
Browse files Browse the repository at this point in the history
fix -ignore-tables, -ignore-enums and -ignore-views when -dsn is present
  • Loading branch information
go-jet authored Apr 14, 2022
2 parents 47a554d + 971e2df commit 3ff9241
Show file tree
Hide file tree
Showing 5 changed files with 214 additions and 78 deletions.
6 changes: 6 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
version: 2
updates:
- package-ecosystem: gomod
directory: /
schedule:
interval: daily
70 changes: 70 additions & 0 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# For most projects, this workflow file will not need changing; you simply need
# to commit it to your repository.
#
# You may wish to alter this file to override the set of languages analyzed,
# or to provide custom queries or build logic.
#
# ******** NOTE ********
# We have attempted to detect the languages in your repository. Please check
# the `language` matrix defined below to confirm you have the correct set of
# supported CodeQL languages.
#
name: "CodeQL"

on:
push:
branches: [ master ]
pull_request:
# The branches below must be a subset of the branches above
branches: [ master ]
schedule:
- cron: '36 17 * * 0'

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ 'go' ]
# CodeQL supports [ 'cpp', 'csharp', 'go', 'java', 'javascript', 'python', 'ruby' ]
# Learn more about CodeQL language support at https://git.io/codeql-language-support

steps:
- name: Checkout repository
uses: actions/checkout@v3

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
# By default, queries listed here will override any specified in a config file.
# Prefix the list here with "+" to use these queries and those in the config file.
# queries: ./path/to/local/query, your-org/your-repo/queries@main

# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl

# ✏️ If the Autobuild fails above, remove it and uncomment the following three lines
# and modify them (or add more) to build your code if your project
# uses a compiled language

#- run: |
# make bootstrap
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
15 changes: 9 additions & 6 deletions cmd/jet/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package main
import (
"flag"
"fmt"
"os"
"strings"

"github.com/go-jet/jet/v2/generator/metadata"
sqlitegen "github.com/go-jet/jet/v2/generator/sqlite"
"github.com/go-jet/jet/v2/generator/template"
Expand All @@ -11,8 +14,6 @@ import (
"github.com/go-jet/jet/v2/mysql"
postgres2 "github.com/go-jet/jet/v2/postgres"
"github.com/go-jet/jet/v2/sqlite"
"os"
"strings"

mysqlgen "github.com/go-jet/jet/v2/generator/mysql"
postgresgen "github.com/go-jet/jet/v2/generator/postgres"
Expand Down Expand Up @@ -112,8 +113,9 @@ func main() {

switch source {
case "postgresql", "postgres":
generatorTemplate := genTemplate(postgres2.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList)
if dsn != "" {
err = postgresgen.GenerateDSN(dsn, schemaName, destDir)
err = postgresgen.GenerateDSN(dsn, schemaName, destDir, generatorTemplate)
break
}
dbConn := postgresgen.DBConnection{
Expand All @@ -131,12 +133,13 @@ func main() {
err = postgresgen.Generate(
destDir,
dbConn,
genTemplate(postgres2.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList),
generatorTemplate,
)

case "mysql", "mysqlx", "mariadb":
generatorTemplate := genTemplate(mysql.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList)
if dsn != "" {
err = mysqlgen.GenerateDSN(dsn, destDir)
err = mysqlgen.GenerateDSN(dsn, destDir, generatorTemplate)
break
}
dbConn := mysqlgen.DBConnection{
Expand All @@ -151,7 +154,7 @@ func main() {
err = mysqlgen.Generate(
destDir,
dbConn,
genTemplate(mysql.Dialect, ignoreTablesList, ignoreViewsList, ignoreEnumsList),
generatorTemplate,
)
case "sqlite":
if dsn == "" {
Expand Down
89 changes: 57 additions & 32 deletions tests/mysql/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,45 +88,70 @@ func TestCmdGenerator(t *testing.T) {
}

func TestIgnoreTablesViewsEnums(t *testing.T) {
cmd := exec.Command("jet",
"-source=MySQL",
"-dbname=dvds",
"-host="+dbconfig.MySqLHost,
"-port="+strconv.Itoa(dbconfig.MySQLPort),
"-user="+dbconfig.MySQLUser,
"-password="+dbconfig.MySQLPassword,
"-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)
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",
"-dbname=dvds",
"-host=" + dbconfig.MySqLHost,
"-port=" + strconv.Itoa(dbconfig.MySQLPort),
"-user=" + dbconfig.MySQLUser,
"-password=" + dbconfig.MySQLPassword,
"-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,
},
},
}

cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cmd := exec.Command("jet", tt.args...)

err := cmd.Run()
require.NoError(t, err)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

tableSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/table")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "customer.go", "film.go", "film_actor.go",
"film_category.go", "film_text.go", "inventory.go", "language.go", "payment.go")
err := cmd.Run()
require.NoError(t, err)

viewSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "sales_by_store.go", "staff_list.go")
tableSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/table")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "customer.go", "film.go", "film_actor.go",
"film_category.go", "film_text.go", "inventory.go", "language.go", "payment.go")

enumFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/enum")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, enumFiles, "nicer_but_slower_film_list_rating.go")
viewSQLBuilderFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "sales_by_store.go", "staff_list.go")

modelFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/model")
require.NoError(t, err)
enumFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/enum")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, enumFiles, "nicer_but_slower_film_list_rating.go")

testutils.AssertFileNamesEqual(t, modelFiles,
"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",
"sales_by_store.go", "staff_list.go")
modelFiles, err := ioutil.ReadDir(genTestDir3 + "/dvds/model")
require.NoError(t, err)

testutils.AssertFileNamesEqual(t, modelFiles,
"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",
"sales_by_store.go", "staff_list.go")
})
}
}

func assertGeneratedFiles(t *testing.T) {
Expand Down
112 changes: 72 additions & 40 deletions tests/postgres/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,56 +93,88 @@ func TestCmdGenerator(t *testing.T) {
}

func TestGeneratorIgnoreTables(t *testing.T) {
err := os.RemoveAll(genTestDir2)
require.NoError(t, err)
tests := []struct {
name string
args []string
}{
{
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",
"-host=localhost",
"-port=" + strconv.Itoa(dbconfig.PgPort),
"-user=jet",
"-password=jet",
"-dbname=jetdb",
"-schema=dvds",
"-ignore-tables=actor,ADDRESS,country, Film , cITY,",
"-ignore-views=Actor_info, FILM_LIST ,staff_list",
"-ignore-enums=mpaa_rating",
"-path=" + genTestDir2,
},
},
}

cmd := exec.Command("jet",
"-source=PostgreSQL",
"-host=localhost",
"-port="+strconv.Itoa(dbconfig.PgPort),
"-user=jet",
"-password=jet",
"-dbname=jetdb",
"-schema=dvds",
"-ignore-tables=actor,ADDRESS,country, Film , cITY,",
"-ignore-views=Actor_info, FILM_LIST ,staff_list",
"-ignore-enums=mpaa_rating",
"-path="+genTestDir2)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
err := os.RemoveAll(genTestDir2)
require.NoError(t, err)

fmt.Println(cmd.Args)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout
cmd := exec.Command("jet", tt.args...)

err = cmd.Run()
require.NoError(t, err)
fmt.Println(cmd.Args)
cmd.Stderr = os.Stderr
cmd.Stdout = os.Stdout

// Table SQL Builder files
tableSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/table")
require.NoError(t, err)
err = cmd.Run()
require.NoError(t, err)

testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "category.go",
"customer.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go")
// Table SQL Builder files
tableSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/table")
require.NoError(t, err)

// View SQL Builder files
viewSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/view")
require.NoError(t, err)
testutils.AssertFileNamesEqual(t, tableSQLBuilderFiles, "category.go",
"customer.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go")

testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go")
// View SQL Builder files
viewSQLBuilderFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/view")
require.NoError(t, err)

// Enums SQL Builder files
_, err = ioutil.ReadDir("./.gentestdata2/jetdb/dvds/enum")
require.Error(t, err, "open ./.gentestdata2/jetdb/dvds/enum: no such file or directory")
testutils.AssertFileNamesEqual(t, viewSQLBuilderFiles, "nicer_but_slower_film_list.go",
"sales_by_film_category.go", "customer_list.go", "sales_by_store.go")

modelFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/model")
require.NoError(t, err)
// Enums SQL Builder files
_, err = ioutil.ReadDir("./.gentestdata2/jetdb/dvds/enum")
require.Error(t, err, "open ./.gentestdata2/jetdb/dvds/enum: no such file or directory")

testutils.AssertFileNamesEqual(t, modelFiles, "category.go",
"customer.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go",
"nicer_but_slower_film_list.go", "sales_by_film_category.go",
"customer_list.go", "sales_by_store.go")
modelFiles, err := ioutil.ReadDir("./.gentestdata2/jetdb/dvds/model")
require.NoError(t, err)

testutils.AssertFileNamesEqual(t, modelFiles, "category.go",
"customer.go", "film_actor.go", "film_category.go", "inventory.go", "language.go",
"payment.go", "rental.go", "staff.go", "store.go",
"nicer_but_slower_film_list.go", "sales_by_film_category.go",
"customer_list.go", "sales_by_store.go")
})
}
}

func TestGenerator(t *testing.T) {
Expand Down

0 comments on commit 3ff9241

Please sign in to comment.