Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Skip when table not found #3

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions pkg/db/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"context"
"database/sql"
"errors"
"github.com/DanielLiu1123/gencoder/pkg/model"
"slices"
"sort"
Expand All @@ -22,6 +23,9 @@ where table_schema = ?
tableRow := db.QueryRowContext(ctx, tableSql, schema, table)
var t model.Table
if err := tableRow.Scan(&t.Schema, &t.Name, &t.Comment); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, err
}

Expand All @@ -45,7 +49,7 @@ order by ordinal_position;
}
defer columnRows.Close()

var columns []*model.Column
columns := make([]*model.Column, 0)
for columnRows.Next() {
var col model.Column
if err := columnRows.Scan(&col.Ordinal, &col.Name, &col.Type, &col.IsNullable, &col.DefaultValue, &col.IsPrimaryKey, &col.Comment); err != nil {
Expand Down Expand Up @@ -103,7 +107,7 @@ order by index_name, seq_in_index;
})
}

var indexes []*model.Index
indexes := make([]*model.Index, 0)
for _, index := range indexMap {
indexes = append(indexes, index)
}
Expand Down
13 changes: 3 additions & 10 deletions pkg/db/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package db

import (
"context"
"database/sql"
"github.com/stretchr/testify/assert"
"github.com/xo/dburl"
"os/exec"
"testing"
"time"
Expand Down Expand Up @@ -56,17 +56,10 @@ func TestGenMySQLTable(t *testing.T) {
// Wait for MySQL to initialize
time.Sleep(10 * time.Second)

dsn := "root:root@tcp(127.0.0.1:3306)/testdb"
db, err := sql.Open("mysql", dsn)
db, err := dburl.Open("mysql://root:root@localhost:3306/testdb")
if err != nil {
t.Fatalf("Failed to connect to the database: %s", err)
t.Fatalf("Failed to open database connection: %s", err)
}
defer func(db *sql.DB) {
err := db.Close()
if err != nil {
t.Fatalf("Failed to close the database: %s", err)
}
}(db)

_, err = db.Exec(`CREATE TABLE testdb.user (
id INT AUTO_INCREMENT PRIMARY KEY,
Expand Down
8 changes: 6 additions & 2 deletions pkg/db/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"context"
"database/sql"
"errors"
"github.com/DanielLiu1123/gencoder/pkg/model"
"slices"
"sort"
Expand All @@ -23,6 +24,9 @@ WHERE table_schema = $1
tableRow := db.QueryRowContext(ctx, tableSql, schema, name)
var t model.Table
if err := tableRow.Scan(&t.Schema, &t.Name, &t.Comment); err != nil {
if errors.Is(err, sql.ErrNoRows) {
return nil, nil
}
return nil, err
}

Expand Down Expand Up @@ -53,7 +57,7 @@ ORDER BY a.attnum;
}
defer columnRows.Close()

var columns []*model.Column
columns := make([]*model.Column, 0)
for columnRows.Next() {
var col model.Column
if err := columnRows.Scan(&col.Ordinal, &col.Name, &col.Type, &col.IsNullable, &col.DefaultValue, &col.IsPrimaryKey, &col.Comment); err != nil {
Expand Down Expand Up @@ -118,7 +122,7 @@ ORDER BY ic.relname, indkey_col.ordinality;
})
}

var indexes []*model.Index
indexes := make([]*model.Index, 0)
for _, index := range indexMap {
indexes = append(indexes, index)
}
Expand Down
6 changes: 6 additions & 0 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ func collectRenderContextsForDBConfig(dbCfg *model.DatabaseConfig) []*model.Rend
log.Fatal(err)
}

// table not found
if table == nil {
log.Printf("table %s.%s not found, skipping", schema, tbCfg.Name)
continue
}

ctx := createRenderContext(dbCfg, tbCfg, table)

contexts = append(contexts, ctx)
Expand Down