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

Fix migration fail to create entry in gofr_migrations #531

Closed
wants to merge 6 commits into from
Closed
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
3 changes: 1 addition & 2 deletions pkg/gofr/container/datasources.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package container
import (
"context"
"database/sql"
"database/sql/driver"

"github.com/redis/go-redis/v9"

Expand All @@ -12,7 +11,6 @@ import (
)

type DB interface {
Driver() driver.Driver
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
QueryContext(ctx context.Context, query string, args ...any) (*sql.Rows, error)
Expand All @@ -23,6 +21,7 @@ type DB interface {
Begin() (*gofrSQL.Tx, error)
Select(ctx context.Context, data interface{}, query string, args ...interface{})
HealthCheck() *datasource.Health
Dialect() string
}

type Redis interface {
Expand Down
15 changes: 7 additions & 8 deletions pkg/gofr/container/mock_datasources.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions pkg/gofr/datasource/sql/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (d *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
return d.DB.Query(query, args...)
}

func (d *DB) Dialect() string {
return d.config.Dialect
}

func (d *DB) QueryRow(query string, args ...interface{}) *sql.Row {
defer d.logQuery(time.Now(), "QueryRow", query, args...)
return d.DB.QueryRow(query, args...)
Expand Down
42 changes: 5 additions & 37 deletions pkg/gofr/migration/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ import (
"database/sql"
"time"

"github.com/go-sql-driver/mysql"
"github.com/lib/pq"

"gofr.dev/pkg/gofr/container"
gofrSql "gofr.dev/pkg/gofr/datasource/sql"
)
Expand All @@ -21,8 +18,6 @@ const (
constraint primary_key primary key (version, method)
);`

checkSQLGoFrMigrationsTable = `SELECT EXISTS (SELECT 1 FROM information_schema.tables WHERE table_name = 'gofr_migrations');`

getLastSQLGoFrMigration = `SELECT COALESCE(MAX(version), 0) FROM gofr_migrations;`

insertGoFrMigrationRowMySQL = `INSERT INTO gofr_migrations (version, method, start_time,duration) VALUES (?, ?, ?, ?);`
Expand Down Expand Up @@ -90,35 +85,8 @@ func (s sqlMigratorObject) apply(m Migrator) Migrator {
}

func (d sqlMigrator) checkAndCreateMigrationTable(c *container.Container) error {
// this can be replaced with having switch case only in the exists variable - but we have chosen to differentiate based
// on driver because if new dialect comes will follow the same, also this complete has to be refactored as mentioned in RUN.
switch c.SQL.Driver().(type) {
case *mysql.MySQLDriver:
var exists int

err := c.SQL.QueryRow(checkSQLGoFrMigrationsTable).Scan(&exists)
if err != nil {
return err
}

if exists != 1 {
if _, err := c.SQL.Exec(createSQLGoFrMigrationsTable); err != nil {
return err
}
}
case *pq.Driver:
var exists bool

err := c.SQL.QueryRow(checkSQLGoFrMigrationsTable).Scan(&exists)
if err != nil {
return err
}

if !exists {
if _, err := c.SQL.Exec(createSQLGoFrMigrationsTable); err != nil {
return err
}
}
if _, err := c.SQL.Exec(createSQLGoFrMigrationsTable); err != nil {
return err
}

return d.Migrator.checkAndCreateMigrationTable(c)
Expand All @@ -144,14 +112,14 @@ func (d sqlMigrator) getLastMigration(c *container.Container) int64 {
}

func (d sqlMigrator) commitMigration(c *container.Container, data migrationData) error {
switch c.SQL.Driver().(type) {
case *mysql.MySQLDriver:
switch c.SQL.Dialect() {
case "mysql":
err := insertMigrationRecord(data.SQLTx, insertGoFrMigrationRowMySQL, data.MigrationNumber, data.StartTime)
if err != nil {
return err
}

case *pq.Driver:
case "postgres":
err := insertMigrationRecord(data.SQLTx, insertGoFrMigrationRowPostgres, data.MigrationNumber, data.StartTime)
if err != nil {
return err
Expand Down
Loading
Loading