aboutsummaryrefslogtreecommitdiffstats
path: root/internal/repositories
diff options
context:
space:
mode:
authorEugene Triguba <eugenetriguba@gmail.com>2024-03-10 15:54:35 -0400
committerEugene Triguba <eugenetriguba@gmail.com>2024-03-10 15:54:35 -0400
commit57689dfd7dfe2e3c6efea53b67ad57820683b3b9 (patch)
tree3e008fc4c0a0baa5d1c44ced357110be223de82a /internal/repositories
parentUpdate readme (diff)
downloadcli-57689dfd7dfe2e3c6efea53b67ad57820683b3b9.zip
Add adapters and remove upperdb
Diffstat (limited to 'internal/repositories')
-rw-r--r--internal/repositories/migration_db_repo.go36
-rw-r--r--internal/repositories/migration_db_repo_test.go62
2 files changed, 50 insertions, 48 deletions
diff --git a/internal/repositories/migration_db_repo.go b/internal/repositories/migration_db_repo.go
index 56bff0c..27644ae 100644
--- a/internal/repositories/migration_db_repo.go
+++ b/internal/repositories/migration_db_repo.go
@@ -8,7 +8,6 @@ import (
"github.com/eugenetriguba/bolt/internal/models"
"github.com/eugenetriguba/bolt/internal/storage"
- "github.com/upper/db/v4"
)
type MigrationDBRepo interface {
@@ -29,7 +28,7 @@ type migrationDBRepo struct {
// operates on exists. If it is unable to create or confirm
// the table exists, an error is returned.
func NewMigrationDBRepo(db storage.DB) (MigrationDBRepo, error) {
- _, err := db.Session.SQL().Exec(`
+ _, err := db.Exec(`
CREATE TABLE IF NOT EXISTS bolt_migrations(
version CHARACTER(14) PRIMARY KEY NOT NULL
);
@@ -49,7 +48,7 @@ func NewMigrationDBRepo(db storage.DB) (MigrationDBRepo, error) {
// will be ones that have been applied, and their message
// will always be an empty string.
func (mr migrationDBRepo) List() (map[string]*models.Migration, error) {
- rows, err := mr.db.Session.SQL().Select("version").From("bolt_migrations").Query()
+ rows, err := mr.db.Query("SELECT version FROM bolt_migrations;")
if err != nil {
return nil, fmt.Errorf(
"unable to execute query to select versions from "+
@@ -89,12 +88,9 @@ func (mr migrationDBRepo) List() (map[string]*models.Migration, error) {
// when the version might be applied, but there was an error.
// Check err first before looking at whether the version is applied.
func (mr migrationDBRepo) IsApplied(version string) (bool, error) {
- row, err := mr.db.Session.SQL().Select(1).From("bolt_migrations").Where("version = ?", version).QueryRow()
- if err != nil {
- return false, fmt.Errorf("unable to execute query to check if version exists in bolt_migrations: %w", err)
- }
var scanResult int
- err = row.Scan(&scanResult)
+ err := mr.db.QueryRow("SELECT 1 FROM bolt_migrations WHERE version = ?", version).
+ Scan(&scanResult)
if errors.Is(err, sql.ErrNoRows) {
return false, nil
}
@@ -116,7 +112,7 @@ func (mr migrationDBRepo) Apply(
upgradeScript string,
migration *models.Migration,
) error {
- err := applyMigration(mr.db.Session, upgradeScript, *migration)
+ err := applyMigration(mr.db, upgradeScript, *migration)
if err != nil {
return err
}
@@ -131,8 +127,8 @@ func (mr migrationDBRepo) ApplyWithTx(
upgradeScript string,
migration *models.Migration,
) error {
- err := mr.db.Session.Tx(func(sess db.Session) error {
- err := applyMigration(sess, upgradeScript, *migration)
+ err := mr.db.Tx(func(db storage.DB) error {
+ err := applyMigration(db, upgradeScript, *migration)
if err != nil {
return err
}
@@ -147,16 +143,16 @@ func (mr migrationDBRepo) ApplyWithTx(
}
func applyMigration(
- db db.Session,
+ db storage.DB,
upgradeScript string,
migration models.Migration,
) error {
- _, err := db.SQL().Exec(upgradeScript)
+ _, err := db.Exec(upgradeScript)
if err != nil {
return fmt.Errorf("unable to execute upgrade script: %w", err)
}
- _, err = db.SQL().InsertInto("bolt_migrations").Columns("version").Values(migration.Version).Exec()
+ _, err = db.Exec("INSERT INTO bolt_migrations(version) VALUES(?)", migration.Version)
if err != nil {
return fmt.Errorf(
"unable to insert migration: %w",
@@ -174,7 +170,7 @@ func (mr migrationDBRepo) Revert(
downgradeScript string,
migration *models.Migration,
) error {
- err := revertMigration(mr.db.Session, downgradeScript, *migration)
+ err := revertMigration(mr.db, downgradeScript, *migration)
if err != nil {
return err
}
@@ -189,8 +185,8 @@ func (mr migrationDBRepo) RevertWithTx(
downgradeScript string,
migration *models.Migration,
) error {
- err := mr.db.Session.Tx(func(sess db.Session) error {
- err := revertMigration(sess, downgradeScript, *migration)
+ err := mr.db.Tx(func(db storage.DB) error {
+ err := revertMigration(db, downgradeScript, *migration)
if err != nil {
return err
}
@@ -205,16 +201,16 @@ func (mr migrationDBRepo) RevertWithTx(
}
func revertMigration(
- db db.Session,
+ db storage.DB,
downgradeScript string,
migration models.Migration,
) error {
- _, err := db.SQL().Exec(downgradeScript)
+ _, err := db.Exec(downgradeScript)
if err != nil {
return fmt.Errorf("unable to execute downgrade script: %w", err)
}
- _, err = db.SQL().DeleteFrom("bolt_migrations").Where("version = ?", migration.Version).Exec()
+ _, err = db.Exec("DELETE FROM bolt_migrations WHERE version = ?", migration.Version)
if err != nil {
return fmt.Errorf(
"unable to remove reverted migration from bolt_migrations table: %w",
diff --git a/internal/repositories/migration_db_repo_test.go b/internal/repositories/migration_db_repo_test.go
index b9fb16e..d54fb24 100644
--- a/internal/repositories/migration_db_repo_test.go
+++ b/internal/repositories/migration_db_repo_test.go
@@ -8,40 +8,38 @@ import (
"github.com/eugenetriguba/bolt/internal/models"
"github.com/eugenetriguba/bolt/internal/repositories"
"github.com/eugenetriguba/checkmate/assert"
- "github.com/upper/db/v4"
)
func TestNewMigrationDBRepo_CreatesTable(t *testing.T) {
testdb := bolttest.NewTestDB(t)
- exists, err := testdb.Session.Collection("bolt_migrations").Exists()
- assert.ErrorIs(t, err, db.ErrCollectionDoesNotExist)
+ exists, err := testdb.TableExists("bolt_migrations")
+ assert.Nil(t, err)
assert.False(t, exists)
_, err = repositories.NewMigrationDBRepo(testdb)
assert.Nil(t, err)
- exists, err = testdb.Session.Collection("bolt_migrations").Exists()
+ exists, err = testdb.TableExists("bolt_migrations")
assert.Nil(t, err)
assert.True(t, exists)
}
func TestNewMigrationDBRepo_TableAlreadyExists(t *testing.T) {
testdb := bolttest.NewTestDB(t)
- _, err := testdb.Session.SQL().Exec(`CREATE TABLE bolt_migrations(id INT NOT NULL PRIMARY KEY)`)
+ _, err := testdb.Exec(`CREATE TABLE bolt_migrations(id INT NOT NULL PRIMARY KEY)`)
assert.Nil(t, err)
- _, err = testdb.Session.SQL().Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`)
+ _, err = testdb.Exec(`INSERT INTO bolt_migrations(id) VALUES (1);`)
assert.Nil(t, err)
_, err = repositories.NewMigrationDBRepo(testdb)
assert.Nil(t, err)
- count, err := testdb.Session.Collection("bolt_migrations").Find().Count()
- assert.Nil(t, err)
- assert.Equal(t, count, uint64(1))
- row, err := testdb.Session.SQL().Select("id").From("bolt_migrations").QueryRow()
+ var count int
+ err = testdb.QueryRow("SELECT count(*) FROM bolt_migrations;").Scan(&count)
assert.Nil(t, err)
+ assert.Equal(t, count, 1)
var scanResult int
- err = row.Scan(&scanResult)
+ err = testdb.QueryRow("SELECT id FROM bolt_migrations;").Scan(&scanResult)
assert.Nil(t, err)
assert.Equal(t, scanResult, 1)
}
@@ -62,7 +60,7 @@ func TestList_SingleResult(t *testing.T) {
assert.Nil(t, err)
version := "20230101000000"
- _, err = db.Session.SQL().InsertInto("bolt_migrations").Columns("version").Values(version).Exec()
+ _, err = db.Exec("INSERT INTO bolt_migrations(version) VALUES(?)", version)
assert.Nil(t, err)
migrations, err := repo.List()
@@ -81,7 +79,7 @@ func TestList_ShortVersion(t *testing.T) {
assert.Nil(t, err)
version := "20230101"
- _, err = db.Session.SQL().InsertInto("bolt_migrations").Columns("version").Values(version).Exec()
+ _, err = db.Exec("INSERT INTO bolt_migrations(version) VALUES(?)", version)
assert.Nil(t, err)
migrations, err := repo.List()
@@ -111,7 +109,7 @@ func TestIsApplied_WithApplied(t *testing.T) {
assert.Nil(t, err)
version := "20230101010101"
- _, err = db.Session.SQL().InsertInto("bolt_migrations").Columns("version").Values(version).Exec()
+ _, err = db.Exec("INSERT INTO bolt_migrations(version) VALUES(?)", version)
assert.Nil(t, err)
applied, err := repo.IsApplied(version)
@@ -132,7 +130,7 @@ func TestApply(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, migration.Applied, true)
- exists, err := testdb.Session.Collection("tmp").Exists()
+ exists, err := testdb.TableExists("tmp")
assert.Nil(t, err)
assert.True(t, exists)
applied, err := repo.IsApplied(migration.Version)
@@ -176,7 +174,7 @@ func TestApplyWithTx_SuccessfullyApplied(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, migration.Applied, true)
- exists, err := testdb.Session.Collection("tmp").Exists()
+ exists, err := testdb.TableExists("tmp")
assert.Nil(t, err)
assert.True(t, exists)
applied, err := repo.IsApplied(migration.Version)
@@ -192,11 +190,14 @@ func TestRevert(t *testing.T) {
bolttest.DropTable(t, testdb, "tmp")
})
- _, err = testdb.Session.SQL().Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
+ _, err = testdb.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "test")
- _, err = testdb.Session.SQL().InsertInto("bolt_migrations").Columns("version").Values(migration.Version).Exec()
+ _, err = testdb.Exec(
+ "INSERT INTO bolt_migrations(version) VALUES(?)",
+ migration.Version,
+ )
assert.Nil(t, err)
migration.Applied = true
@@ -204,12 +205,13 @@ func TestRevert(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, migration.Applied, false)
- exists, err := testdb.Session.Collection("tmp").Exists()
- assert.ErrorIs(t, err, db.ErrCollectionDoesNotExist)
+ exists, err := testdb.TableExists("tmp")
+ assert.Nil(t, err)
assert.False(t, exists)
- count, err := testdb.Session.Collection("bolt_migrations").Count()
+ var count int
+ err = testdb.QueryRow("SELECT count(*) FROM bolt_migrations;").Scan(&count)
assert.Nil(t, err)
- assert.Equal(t, count, uint64(0))
+ assert.Equal(t, count, 0)
}
func TestRevert_MalformedSql(t *testing.T) {
@@ -243,11 +245,14 @@ func TestRevertWithTx_SuccessfullyReverted(t *testing.T) {
bolttest.DropTable(t, testdb, "tmp")
})
- _, err = testdb.Session.SQL().Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
+ _, err = testdb.Exec(`CREATE TABLE tmp(id INT NOT NULL PRIMARY KEY)`)
assert.Nil(t, err)
migration := models.NewTimestampMigration(time.Now(), "test")
- _, err = testdb.Session.SQL().InsertInto("bolt_migrations").Columns("version").Values(migration.Version).Exec()
+ _, err = testdb.Exec(
+ "INSERT INTO bolt_migrations(version) VALUES(?)",
+ migration.Version,
+ )
assert.Nil(t, err)
migration.Applied = true
@@ -255,10 +260,11 @@ func TestRevertWithTx_SuccessfullyReverted(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, migration.Applied, false)
- exists, err := testdb.Session.Collection("tmp").Exists()
- assert.ErrorIs(t, err, db.ErrCollectionDoesNotExist)
+ exists, err := testdb.TableExists("tmp")
+ assert.Nil(t, err)
assert.False(t, exists)
- count, err := testdb.Session.Collection("bolt_migrations").Count()
+ var count int
+ err = testdb.QueryRow("SELECT count(*) FROM bolt_migrations;").Scan(&count)
assert.Nil(t, err)
- assert.Equal(t, count, uint64(0))
+ assert.Equal(t, count, 0)
}