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

WIP: Add MySQL support to bobgen-sql #292

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
5 changes: 3 additions & 2 deletions gen/bobgen-atlas/driver/atlas_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ func TestPostgres(t *testing.T) {
config: Config{
Dialect: "psql",
},
schema: psqlSchemas,
goldenJson: "atlas.psql_golden.json",
schema: psqlSchemas,
goldenJson: "atlas.psql_golden.json",
modelTemplates: gen.PSQLModelTemplates,
}
testDialect(t, psqlCase)
}
Expand Down
2 changes: 2 additions & 0 deletions gen/bobgen-atlas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func run(c *cli.Context) error {

var modelTemplates []fs.FS
switch driverConfig.Dialect {
case "psql":
modelTemplates = append(modelTemplates, gen.PSQLModelTemplates)
case "mysql":
modelTemplates = append(modelTemplates, gen.MySQLModelTemplates)
case "sqlite":
Expand Down
21 changes: 19 additions & 2 deletions gen/bobgen-helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,6 +452,14 @@ func GetFreePort() (int, error) {
}

func Migrate(ctx context.Context, db *sql.DB, dir fs.FS) error {
return MigrateWithOptions(ctx, db, dir, MigrationOpts{})
}

type MigrationOpts struct {
SplitFileIntoStatements bool
}

func MigrateWithOptions(ctx context.Context, db *sql.DB, dir fs.FS, opts MigrationOpts) error {
err := fs.WalkDir(dir, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
Expand All @@ -466,8 +474,17 @@ func Migrate(ctx context.Context, db *sql.DB, dir fs.FS) error {
}

fmt.Printf("migrating %s...\n", path)
if _, err = db.ExecContext(ctx, string(content)); err != nil {
return fmt.Errorf("migrating %s: %w", path, err)

stmts := []string{string(content)}

if opts.SplitFileIntoStatements {
stmts = strings.Split(string(content), ";")
}

for _, stmt := range stmts {
if _, err = db.ExecContext(ctx, stmt); err != nil {
return fmt.Errorf("migrating %s: %w", path, err)
}
}

return nil
Expand Down
1 change: 1 addition & 0 deletions gen/bobgen-prisma/driver/prisma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ func TestPostgres(t *testing.T) {
DriverName: "pgx",
DriverPkg: "github.com/jackc/pgx/v5/stdlib",
},
modelTemplates: gen.PSQLModelTemplates,
})
}

Expand Down
1 change: 1 addition & 0 deletions gen/bobgen-prisma/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,7 @@ func generate(root root) error {
dialect = "psql"
driverName = "pgx"
driverPkg = "github.com/jackc/pgx/v5/stdlib"
modelTemplates = append(modelTemplates, gen.PSQLModelTemplates)
case ProviderSQLite:
dialect = "sqlite"
driverName = "sqlite"
Expand Down
75 changes: 75 additions & 0 deletions gen/bobgen-sql/driver/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,14 @@ import (
"os"
"path/filepath"

sqle "github.com/dolthub/go-mysql-server"
"github.com/dolthub/go-mysql-server/memory"
"github.com/dolthub/go-mysql-server/server"
embeddedpostgres "github.com/fergusstrange/embedded-postgres"
"github.com/lib/pq"
"github.com/stephenafamo/bob/gen"
helpers "github.com/stephenafamo/bob/gen/bobgen-helpers"
mysqlDriver "github.com/stephenafamo/bob/gen/bobgen-mysql/driver"
psqlDriver "github.com/stephenafamo/bob/gen/bobgen-psql/driver"
sqliteDriver "github.com/stephenafamo/bob/gen/bobgen-sqlite/driver"
"github.com/stephenafamo/bob/gen/drivers"
Expand Down Expand Up @@ -109,6 +113,77 @@ func getPsqlDriver(ctx context.Context, config Config) (psqlDriver.Interface, er
return d, nil
}

func RunMySQL(ctx context.Context, state *gen.State, config Config) error {
config.fs = os.DirFS(config.Dir)

d, err := getMySQLDriver(ctx, config)
if err != nil {
return fmt.Errorf("getting mysql driver: %w", err)
}

return gen.Run(ctx, state, d)
}

func getMySQLDriver(ctx context.Context, config Config) (mysqlDriver.Interface, error) {
port, err := helpers.GetFreePort()
if err != nil {
return nil, fmt.Errorf("could not get a free port: %w", err)
}

memDB := memory.NewDatabase("bob_droppable")
memDB.BaseDatabase.EnablePrimaryKeyIndexes()

pro := memory.NewDBProvider(memDB)
engine := sqle.NewDefault(pro)

serverConfig := server.Config{
Protocol: "tcp",
Address: fmt.Sprintf("localhost:%d", port),
}
s, err := server.NewServer(serverConfig, engine, memory.NewSessionBuilder(pro), nil)
if err != nil {
return nil, fmt.Errorf("creating mysql server: %w", err)
}
go func() {
if err = s.Start(); err != nil {
panic(fmt.Errorf("starting mysql server: %w", err))
}
}()

defer func() {
if err := s.Close(); err != nil {
fmt.Println("Error stopping mysql:", err)
}
}()

dsn := fmt.Sprintf("no_user:@tcp(localhost:%d)/bob_droppable", port)
db, err := sql.Open("mysql", dsn)
if err != nil {
return nil, fmt.Errorf("failed to connect to database: %w", err)
}
defer db.Close()

if err := helpers.MigrateWithOptions(ctx, db, config.fs, helpers.MigrationOpts{
SplitFileIntoStatements: true,
}); err != nil {
return nil, fmt.Errorf("migrating: %w", err)
}
db.Close() // close early

d := wrapDriver(ctx, mysqlDriver.New(mysqlDriver.Config{
Dsn: dsn,

Only: config.Only,
Except: config.Except,
Concurrency: config.Concurrency,
Output: config.Output,
Pkgname: config.Pkgname,
NoFactory: config.NoFactory,
}))

return d, nil
}

func RunSQLite(ctx context.Context, state *gen.State, config Config) error {
config.fs = os.DirFS(config.Dir)

Expand Down
29 changes: 29 additions & 0 deletions gen/bobgen-sql/driver/sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"context"
_ "embed"
"fmt"
"io/fs"
"os"
"testing"

_ "github.com/jackc/pgx/v5/stdlib"
"github.com/stephenafamo/bob/gen"
helpers "github.com/stephenafamo/bob/gen/bobgen-helpers"
"github.com/stephenafamo/bob/gen/drivers"
testfiles "github.com/stephenafamo/bob/test/files"
testgen "github.com/stephenafamo/bob/test/gen"
Expand All @@ -33,6 +36,31 @@ func TestPostgres(t *testing.T) {
},
GoldenFile: "../../bobgen-psql/driver/psql.golden.json",
OverwriteGolden: false,
Templates: &helpers.Templates{Models: []fs.FS{gen.PSQLModelTemplates}},
})
}

func TestMySQL(t *testing.T) {
t.Parallel()
out, cleanup := prep(t, "mysql")
defer cleanup()

config := Config{
fs: testfiles.MySQLSchema,
}

testgen.TestDriver(t, testgen.DriverTestConfig[any]{
Root: out,
GetDriver: func() drivers.Interface[any] {
d, err := getMySQLDriver(context.Background(), config)
if err != nil {
t.Fatalf("getting mysql driver: %s", err)
}
return d
},
GoldenFile: "../../bobgen-mysql/driver/mysql.golden.json",
OverwriteGolden: false,
Templates: &helpers.Templates{Models: []fs.FS{gen.MySQLModelTemplates}},
})
}

Expand All @@ -57,6 +85,7 @@ func TestSQLite(t *testing.T) {
},
GoldenFile: "../../bobgen-sqlite/driver/sqlite.golden.json",
OverwriteGolden: false,
Templates: &helpers.Templates{Models: []fs.FS{gen.SQLiteModelTemplates}},
})
}

Expand Down
2 changes: 1 addition & 1 deletion gen/bobgen-sql/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func run(c *cli.Context) error {
case "psql", "postgres":
return driver.RunPostgres(c.Context, state, driverConfig)
case "mysql":
return fmt.Errorf("mysql dialect is not supported yet")
return driver.RunMySQL(c.Context, state, driverConfig)
case "sqlite":
return driver.RunSQLite(c.Context, state, driverConfig)
default:
Expand Down
24 changes: 21 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
module github.com/stephenafamo/bob

go 1.22
go 1.22.2

toolchain go1.22.1
toolchain go1.23.0

require (
ariga.io/atlas v0.9.0
github.com/DATA-DOG/go-txdb v0.1.6
github.com/Masterminds/sprig/v3 v3.2.2
github.com/aarondl/opt v0.0.0-20230114172057-b91f370c41f0
github.com/antlr/antlr4/runtime/Go/antlr/v4 v4.0.0-20230321174746-8dcc6526cfb1
github.com/dolthub/go-mysql-server v0.18.2-0.20241029221022-84d576aadba3
github.com/fergusstrange/embedded-postgres v1.26.0
github.com/go-sql-driver/mysql v1.7.2-0.20231213112541-0004702b931d
github.com/google/go-cmp v0.5.9
Expand Down Expand Up @@ -44,43 +45,59 @@ require (
github.com/aarondl/json v0.0.0-20221020222930-8b0db17ef1bf // indirect
github.com/agext/levenshtein v1.2.1 // indirect
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/dolthub/flatbuffers/v23 v23.3.3-dh.2 // indirect
github.com/dolthub/go-icu-regex v0.0.0-20240916130659-0118adc6b662 // indirect
github.com/dolthub/jsonpath v0.0.2-0.20240227200619-19675ab05c71 // indirect
github.com/dolthub/vitess v0.0.0-20241028204000-267861bc75a0 // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-kit/kit v0.10.0 // indirect
github.com/go-openapi/inflect v0.19.0 // indirect
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hashicorp/golang-lru v0.5.4 // indirect
github.com/huandu/xstrings v1.3.1 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
github.com/knadh/koanf/maps v0.1.1 // indirect
github.com/lestrrat-go/strftime v1.0.4 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-sqlite3 v1.14.16 // indirect
github.com/mitchellh/copystructure v1.2.0 // indirect
github.com/mitchellh/go-wordwrap v1.0.0 // indirect
github.com/mitchellh/reflectwalk v1.0.2 // indirect
github.com/ncruces/go-strftime v0.1.9 // indirect
github.com/pganalyze/pg_query_go/v5 v5.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.1.0 // indirect
github.com/shopspring/decimal v1.3.1 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/spf13/cast v1.5.0 // indirect
github.com/stretchr/testify v1.8.2 // indirect
github.com/tetratelabs/wazero v1.7.0 // indirect
github.com/volatiletech/inflect v0.0.1 // indirect
github.com/xi2/xz v0.0.0-20171230120015-48954b6210f8 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/zclconf/go-cty v1.8.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
golang.org/x/crypto v0.17.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/xerrors v0.0.0-20220907171357-04be3eba64a2 // indirect
google.golang.org/genproto v0.0.0-20230110181048-76db0878b65f // indirect
google.golang.org/grpc v1.53.0 // indirect
google.golang.org/protobuf v1.31.0 // indirect
gopkg.in/src-d/go-errors.v1 v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
lukechampine.com/uint128 v1.2.0 // indirect
modernc.org/cc/v3 v3.41.0 // indirect
Expand All @@ -95,3 +112,4 @@ require (

// replace github.com/pingcap/tidb => github.com/pingcap/tidb v1.1.0-beta.0.20230311041313-145b7cdf72fe
// replace github.com/stephenafamo/sqlparser => ../sqlparser
// replace github.com/dolthub/go-mysql-server => ../../forks/go-mysql-server
Loading
Loading