Skip to content

Commit 140aba1

Browse files
committed
refactor: sqlite remove cgo dependencies
1 parent 5a7d8df commit 140aba1

File tree

5 files changed

+24
-18
lines changed

5 files changed

+24
-18
lines changed

internal/database/dbutil/database.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,16 @@ import (
77
"github.com/jackc/pgerrcode"
88
"github.com/jmoiron/sqlx"
99
"github.com/lib/pq"
10-
"github.com/mattn/go-sqlite3"
1110
"github.com/oom-ai/oomstore/pkg/oomstore/types"
1211
"github.com/pkg/errors"
1312
"github.com/snowflakedb/gosnowflake"
1413
"google.golang.org/api/googleapi"
14+
"modernc.org/sqlite"
15+
sqlite3 "modernc.org/sqlite/lib"
1516
)
1617

1718
func OpenSQLite(dbFile string) (*sqlx.DB, error) {
18-
db, err := sqlx.Open("sqlite3", dbFile)
19+
db, err := sqlx.Open("sqlite", dbFile)
1920
return db, errors.WithStack(err)
2021
}
2122

@@ -59,10 +60,9 @@ func DeserializeString(i interface{}, backend types.BackendType) string {
5960
func IsTableNotFoundError(err error, backend types.BackendType) bool {
6061
err = errors.Cause(err)
6162
switch backend {
62-
// https://github.com/mattn/go-sqlite3/issues/244
6363
case types.BackendSQLite:
64-
if sqliteErr, ok := err.(sqlite3.Error); ok {
65-
return sqliteErr.Code == sqlite3.ErrError
64+
if sqliteErr, ok := err.(*sqlite.Error); ok {
65+
return sqliteErr.Code() == sqlite3.SQLITE_CORE
6666
}
6767

6868
// https://dev.mysql.com/doc/mysql-errors/5.7/en/server-error-reference.html#error_er_no_such_table

internal/database/metadata/sqlite/entity.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package sqlite
33
import (
44
"context"
55

6-
"github.com/mattn/go-sqlite3"
76
"github.com/pkg/errors"
7+
"modernc.org/sqlite"
8+
sqlite3 "modernc.org/sqlite/lib"
89

910
"github.com/oom-ai/oomstore/internal/database/metadata"
1011
)
@@ -13,11 +14,12 @@ func createEntity(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadat
1314
query := "INSERT INTO entity(name, description) VALUES(?, ?)"
1415
res, err := sqlxCtx.ExecContext(ctx, query, opt.EntityName, opt.Description)
1516
if err != nil {
16-
if er, ok := err.(sqlite3.Error); ok {
17-
if er.ExtendedCode == sqlite3.ErrConstraintUnique {
17+
if er, ok := err.(*sqlite.Error); ok {
18+
if er.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
1819
return 0, errors.Errorf("entity %s already exists", opt.EntityName)
1920
}
2021
}
22+
2123
return 0, err
2224
}
2325

internal/database/metadata/sqlite/feature.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ package sqlite
33
import (
44
"context"
55

6-
"github.com/mattn/go-sqlite3"
76
"github.com/pkg/errors"
7+
"modernc.org/sqlite"
8+
sqlite3 "modernc.org/sqlite/lib"
89

910
"github.com/oom-ai/oomstore/internal/database/metadata"
1011
)
@@ -16,8 +17,8 @@ func createFeature(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metada
1617
query := "INSERT INTO feature(name, full_name, group_id, value_type, description) VALUES (?, ?, ?, ?, ?)"
1718
res, err := sqlxCtx.ExecContext(ctx, sqlxCtx.Rebind(query), opt.FeatureName, opt.FullName, opt.GroupID, opt.ValueType, opt.Description)
1819
if err != nil {
19-
if sqliteErr, ok := err.(sqlite3.Error); ok {
20-
if sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
20+
if sqliteErr, ok := err.(*sqlite.Error); ok {
21+
if sqliteErr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
2122
return 0, errors.Errorf("feature %s already exists", opt.FeatureName)
2223
}
2324
}

internal/database/metadata/sqlite/group.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import (
44
"context"
55

66
"github.com/pkg/errors"
7+
"modernc.org/sqlite"
8+
sqlite3 "modernc.org/sqlite/lib"
79

8-
"github.com/mattn/go-sqlite3"
910
"github.com/oom-ai/oomstore/internal/database/metadata"
1011
"github.com/oom-ai/oomstore/pkg/errdefs"
1112
"github.com/oom-ai/oomstore/pkg/oomstore/types"
@@ -19,8 +20,8 @@ func createGroup(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata
1920
query := "INSERT INTO feature_group(name, entity_id, category, description) VALUES(?, ?, ?, ?)"
2021
res, err := sqlxCtx.ExecContext(ctx, query, opt.GroupName, opt.EntityID, opt.Category, opt.Description)
2122
if err != nil {
22-
if sqliteErr, ok := err.(sqlite3.Error); ok {
23-
if sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
23+
if sqliteErr, ok := err.(*sqlite.Error); ok {
24+
if sqliteErr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
2425
return 0, errors.Errorf("feature group %s already exists", opt.GroupName)
2526
}
2627
}

internal/database/metadata/sqlite/revision.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@ package sqlite
33
import (
44
"context"
55

6-
"github.com/mattn/go-sqlite3"
6+
"github.com/pkg/errors"
7+
"modernc.org/sqlite"
8+
sqlite3 "modernc.org/sqlite/lib"
9+
710
"github.com/oom-ai/oomstore/internal/database/dbutil"
811
"github.com/oom-ai/oomstore/internal/database/metadata"
9-
"github.com/pkg/errors"
1012
)
1113

1214
func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metadata.CreateRevisionOpt) (int, string, error) {
@@ -21,8 +23,8 @@ func createRevision(ctx context.Context, sqlxCtx metadata.SqlxContext, opt metad
2123
insertQuery := "INSERT INTO feature_group_revision(group_id, revision, snapshot_table, cdc_table, anchored, description) VALUES (?, ?, ?, ?, ?, ?)"
2224
res, err := sqlxCtx.ExecContext(ctx, sqlxCtx.Rebind(insertQuery), opt.GroupID, opt.Revision, snapshotTable, cdcTable, opt.Anchored, opt.Description)
2325
if err != nil {
24-
if sqliteErr, ok := err.(sqlite3.Error); ok {
25-
if sqliteErr.ExtendedCode == sqlite3.ErrConstraintUnique {
26+
if sqliteErr, ok := err.(*sqlite.Error); ok {
27+
if sqliteErr.Code() == sqlite3.SQLITE_CONSTRAINT_UNIQUE {
2628
return 0, "", errors.Errorf("revision already exists: groupID=%d, revision=%d", opt.GroupID, opt.Revision)
2729
}
2830
}

0 commit comments

Comments
 (0)