-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip: setup for sqllite3 * sqlite support via bun sqlite dialect - Keep same version of bun.DB and dialects - Move (shared) SQL code into `datastore/sql` - update instances of postgres.New* to cfg.GetDatabase().New* * reset config chore: run lint * chore: integrate sqllite tests chore: cleanup * refactor bun initiation * refactor integration tests * remove integration test for sqlite * switch to `logrusbun` to logQueries * fix driver example in config.yml
- Loading branch information
1 parent
c406796
commit c87b72b
Showing
26 changed files
with
261 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package sql | ||
|
||
import ( | ||
"database/sql" | ||
|
||
"github.com/adelowo/sdump/config" | ||
"github.com/oiime/logrusbun" | ||
"github.com/sirupsen/logrus" | ||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/dialect/pgdialect" | ||
"github.com/uptrace/bun/dialect/sqlitedialect" | ||
"github.com/uptrace/bun/driver/pgdriver" | ||
"github.com/uptrace/bun/driver/sqliteshim" | ||
"github.com/uptrace/bun/extra/bunotel" | ||
) | ||
|
||
func newPsql(cfg config.DatabaseConfig) (*bun.DB, error) { | ||
pgdb := sql.OpenDB(pgdriver.NewConnector(pgdriver.WithDSN(cfg.DSN))) | ||
|
||
db := bun.NewDB(pgdb, pgdialect.New()) | ||
log := logrus.New() | ||
|
||
db.AddQueryHook(bunotel.NewQueryHook(bunotel.WithDBName("getclaimclaim"))) | ||
|
||
if cfg.LogQueries { | ||
db.AddQueryHook(logrusbun.NewQueryHook(logrusbun.QueryHookOptions{Logger: log})) | ||
} | ||
|
||
return db, db.Ping() | ||
} | ||
|
||
func newSqlite(cfg config.DatabaseConfig) (*bun.DB, error) { | ||
sqlite, err := sql.Open(sqliteshim.ShimName, cfg.DSN) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
db := bun.NewDB(sqlite, sqlitedialect.New()) | ||
log := logrus.New() | ||
|
||
if cfg.LogQueries { | ||
db.AddQueryHook(logrusbun.NewQueryHook(logrusbun.QueryHookOptions{Logger: log})) | ||
} | ||
|
||
return db, db.Ping() | ||
} | ||
|
||
func New(cfg config.DatabaseConfig) (*bun.DB, error) { | ||
if cfg.Driver == config.DatabaseTypeSqlite { | ||
return newSqlite(cfg) | ||
} | ||
|
||
return newPsql(cfg) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package postgres | ||
package sql | ||
|
||
import ( | ||
"context" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
//go:build integration | ||
// +build integration | ||
|
||
package sql | ||
|
||
import ( | ||
"database/sql" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/adelowo/sdump/config" | ||
testfixtures "github.com/go-testfixtures/testfixtures/v3" | ||
"github.com/stretchr/testify/require" | ||
"github.com/uptrace/bun" | ||
"github.com/uptrace/bun/driver/sqliteshim" | ||
|
||
"github.com/golang-migrate/migrate/v4" | ||
"github.com/golang-migrate/migrate/v4/database/sqlite3" | ||
) | ||
|
||
func prepareSqliteTestDatabase(t *testing.T, dsn string) { | ||
t.Helper() | ||
|
||
var err error | ||
|
||
db, err := sql.Open(sqliteshim.ShimName, dsn) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, db.Ping()) | ||
|
||
driver, err := sqlite3.WithInstance(db, &sqlite3.Config{}) | ||
require.NoError(t, err) | ||
|
||
migrator, err := migrate.NewWithDatabaseInstance( | ||
fmt.Sprintf("file://%s", "migrations"), "sqllite3", driver) | ||
require.NoError(t, err) | ||
|
||
if err := migrator.Up(); err != nil && err != migrate.ErrNoChange { | ||
require.NoError(t, err) | ||
} | ||
|
||
fixtures, err := testfixtures.New( | ||
testfixtures.Database(db), | ||
testfixtures.Dialect("sqlite3"), | ||
testfixtures.Directory("testdata/fixtures"), | ||
) | ||
require.NoError(t, err) | ||
|
||
require.NoError(t, fixtures.Load()) | ||
} | ||
|
||
func setupSqliteDatabase(t *testing.T) (*bun.DB, func()) { | ||
t.Helper() | ||
|
||
dsn := "file::memory:?cache=shared" | ||
|
||
prepareSqliteTestDatabase(t, dsn) | ||
|
||
client, err := New(config.DatabaseConfig{ | ||
DSN: dsn, | ||
Driver: config.DatabaseTypeSqlite, | ||
}) | ||
require.NoError(t, err) | ||
|
||
return client, func() { | ||
} | ||
} |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package postgres | ||
package sql | ||
|
||
import ( | ||
"context" | ||
|
Oops, something went wrong.