<#7160 [V2] Replace GORM with sqlx for database op...
# flyte-github
a
#7160 [V2] Replace GORM with sqlx for database operations Pull request opened by pingsutw Tracking issue N/A Why are the changes needed? GORM adds ORM abstraction overhead and implicit query generation that makes it harder to reason about database behavior. Replacing it with sqlx gives us: • Explicit SQL queries — no hidden query generation or N+1 surprises • Lighter dependency footprint — sqlx is a thin wrapper over
database/sql
• Better control over PostgreSQL-specific features (ON CONFLICT, RETURNING, etc.) • Simpler migration system using plain SQL files instead of struct-tag-driven AutoMigrate What changes were proposed in this pull request? Complete migration from
<http://gorm.io/gorm|gorm.io/gorm>
to
<http://github.com/jmoiron/sqlx|github.com/jmoiron/sqlx>
across the entire V2 codebase: Database Foundation (
flytestdlib/database/
)
•
GetDB()
/
GetReadOnlyDB()
now return
*sqlx.DB
instead of
*gorm.DB
• PostgreSQL connections use
sqlx.Open("pgx", dsn)
with the pgx stdlib driver • New custom SQL migration runner (
migrate.go
) that reads numbered
.sql
files from
embed.FS
• Deleted
gorm.go
(GORM logger adapter) Models • Replaced all
gorm:"..."
struct tags with
db:"..."
tags for sqlx field mapping • Removed all
TableName()
methods — table names are now explicit in SQL queries Repository Implementations • All GORM query builder chains replaced with explicit SQL via
sqlx.GetContext
,
sqlx.SelectContext
,
db.ExecContext
•
gorm.ErrRecordNotFound
→
sql.ErrNoRows
throughout •
gorm.ErrDuplicatedKey
→ custom
ErrAlreadyExists
sentinel •
clause.OnConflict{DoNothing: true}
→
INSERT ... ON CONFLICT DO NOTHING
•
clause.Returning{}
→
RETURNING
SQL clauses • LISTEN/NOTIFY: DSN stored directly instead of extracting from GORM dialector • Renamed filter/sort interfaces:
GormQueryExpression
→
QueryExpression
,
GetGormOrderExpr
→
GetOrderExpr
Migrations • Replaced
gormigrate
with plain SQL migration files under
sql/
directories •
runs/migrations/sql/001_init_schema.sql
— full runs service schema •
cache_service/migrations/sql/001_init_schema.sql
— cache service schema • Each service embeds its SQL directory and exposes
RunMigrations(ctx, db)
App & Service Layer • `app/context.go`:
DB *gorm.DB
→
DB *sqlx.DB
• Setup files use
migrations.RunMigrations()
instead of gormigrate • Service layer error checks use
sql.ErrNoRows
• All test files updated Dependencies • Added:
<http://github.com/jmoiron/sqlx|github.com/jmoiron/sqlx>
,
<http://github.com/jackc/pgx/v5/stdlib|github.com/jackc/pgx/v5/stdlib>
• Removed:
<http://gorm.io/gorm|gorm.io/gorm>
,
<http://gorm.io/driver/postgres|gorm.io/driver/postgres>
,
<http://github.com/go-gormigrate/gormigrate/v2|github.com/go-gormigrate/gormigrate/v2>
How was this patch tested? •
go build ./...
passes cleanly •
go vet ./...
passes • Repository unit tests pass (
go test ./runs/repository/impl/...
— 41 tests) • Zero remaining GORM imports in any
.go
file • Only gorm.io text remaining: documentation strings in auto-generated flag help text Labels • changed: Migration from GORM ORM to sqlx for all database operations • removed: GORM, gorm driver, gormigrate dependencies Check all the applicable boxes • I updated the documentation accordingly. • All new and existing tests passed. • All commits are signed-off. •
main
• #6583 • [V2] Replace GORM with sqlx for database operations 👈 flyteorg/flyte