Skip to content

Commit c008737

Browse files
authored
Merge pull request #927 from oom-ai/refactor/remove-cgo
Refactor/remove cgo
2 parents 1900471 + 9f7e1a3 commit c008737

File tree

8 files changed

+178
-28
lines changed

8 files changed

+178
-28
lines changed

.goreleaser.yml

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ builds:
99
id: "oomcli"
1010
binary: oomcli
1111
env:
12-
- CGO_ENABLED=1
12+
- CGO_ENABLED=0
1313
goos:
1414
- linux
1515
- darwin
16+
goarch:
17+
- amd64
1618
ldflags:
1719
- >
1820
-s -w
@@ -23,22 +25,19 @@ builds:
2325
id: "oomagent"
2426
binary: oomagent
2527
env:
26-
- CGO_ENABLED=1
28+
- CGO_ENABLED=0
2729
goos:
2830
- linux
2931
- darwin
32+
goarch:
33+
- amd64
3034
ldflags:
3135
- >
3236
-s -w
3337
-X github.com/oom-ai/oomstore/version.Version={{.Version}}
3438
-X github.com/oom-ai/oomstore/version.Commit={{.Commit}}
3539
-X github.com/oom-ai/oomstore/version.Built={{.CommitDate}}
3640
37-
archives:
38-
- replacements:
39-
darwin: Darwin
40-
linux: Linux
41-
amd64: x86_64
4241
checksum:
4342
name_template: "checksums.txt"
4443
snapshot:

go.mod

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@ require (
1818
github.com/jackc/pgerrcode v0.0.0-20201024163028-a0d42d470451
1919
github.com/jmoiron/sqlx v1.3.4
2020
github.com/lib/pq v1.10.4
21-
github.com/mattn/go-sqlite3 v1.14.9
2221
github.com/mitchellh/mapstructure v1.4.3
2322
github.com/olekukonko/tablewriter v0.0.5
23+
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3
2424
github.com/pingcap/log v0.0.0-20211215031037-e024ba4eb0ee
25+
github.com/pkg/errors v0.9.1
2526
github.com/snowflakedb/gosnowflake v1.6.5
2627
github.com/spf13/cast v1.4.1
2728
github.com/spf13/cobra v1.3.0
@@ -33,6 +34,7 @@ require (
3334
google.golang.org/grpc v1.43.0
3435
google.golang.org/protobuf v1.27.1
3536
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b
37+
modernc.org/sqlite v1.14.3
3638
)
3739

3840
require (
@@ -82,23 +84,24 @@ require (
8284
github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect
8385
github.com/inconshreveable/mousetrap v1.0.0 // indirect
8486
github.com/jmespath/go-jmespath v0.4.0 // indirect
87+
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 // indirect
8588
github.com/klauspost/compress v1.13.6 // indirect
8689
github.com/kr/text v0.2.0 // indirect
8790
github.com/mattn/go-ieproxy v0.0.1 // indirect
91+
github.com/mattn/go-isatty v0.0.14 // indirect
8892
github.com/mattn/go-runewidth v0.0.13 // indirect
8993
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
9094
github.com/opentracing/opentracing-go v1.2.0 // indirect
9195
github.com/pierrec/lz4/v4 v4.1.12 // indirect
92-
github.com/pingcap/errors v0.11.5-0.20201126102027-b0a155152ca3 // indirect
9396
github.com/pingcap/failpoint v0.0.0-20210918120811-547c13e3eb00 // indirect
9497
github.com/pingcap/kvproto v0.0.0-20211229082925-7a8280c36daf // indirect
9598
github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect
96-
github.com/pkg/errors v0.9.1 // indirect
9799
github.com/pmezard/go-difflib v1.0.0 // indirect
98100
github.com/prometheus/client_golang v1.11.0 // indirect
99101
github.com/prometheus/client_model v0.2.0 // indirect
100102
github.com/prometheus/common v0.32.1 // indirect
101103
github.com/prometheus/procfs v0.7.3 // indirect
104+
github.com/remyoudompheng/bigfft v0.0.0-20200410134404-eec4a21b6bb0 // indirect
102105
github.com/rivo/uniseg v0.2.0 // indirect
103106
github.com/sirupsen/logrus v1.8.1 // indirect
104107
github.com/spf13/pflag v1.0.5 // indirect
@@ -108,13 +111,24 @@ require (
108111
go.uber.org/atomic v1.9.0 // indirect
109112
go.uber.org/multierr v1.7.0 // indirect
110113
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3 // indirect
114+
golang.org/x/mod v0.5.0 // indirect
111115
golang.org/x/net v0.0.0-20211216030914-fe4d6282115f // indirect
112116
golang.org/x/oauth2 v0.0.0-20211104180415-d3ed0bb246c8 // indirect
113117
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
114118
golang.org/x/sys v0.0.0-20211216021012-1d35b9e2eb4e // indirect
115119
golang.org/x/text v0.3.7 // indirect
120+
golang.org/x/tools v0.1.5 // indirect
116121
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
117122
google.golang.org/appengine v1.6.7 // indirect
118123
gopkg.in/inf.v0 v0.9.1 // indirect
119124
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
125+
lukechampine.com/uint128 v1.1.1 // indirect
126+
modernc.org/cc/v3 v3.35.18 // indirect
127+
modernc.org/ccgo/v3 v3.12.95 // indirect
128+
modernc.org/libc v1.11.104 // indirect
129+
modernc.org/mathutil v1.4.1 // indirect
130+
modernc.org/memory v1.0.5 // indirect
131+
modernc.org/opt v0.1.1 // indirect
132+
modernc.org/strutil v1.1.1 // indirect
133+
modernc.org/token v1.0.0 // indirect
120134
)

go.sum

Lines changed: 131 additions & 0 deletions
Large diffs are not rendered by default.

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)