Skip to content

Commit

Permalink
refactor: rename and added todo.
Browse files Browse the repository at this point in the history
  • Loading branch information
adharshmk96 committed Oct 31, 2023
1 parent c9f5e09 commit 7be1641
Show file tree
Hide file tree
Showing 6 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions pkg/sqlMigrator/dbrepo/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func NewSQLiteRepo(filePath string) sqlmigrator.DBRepo {
return repo
}

// TODO: handle the result
func (db *sqliteDb) Exec(query string) error {
_, err := db.conn.Exec(query)
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions pkg/sqlMigrator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,14 @@ func (g *Generator) Generate(ctx *Context) ([]string, error) {
func (g *Generator) Clean(ctx *Context) ([]string, error) {
removedFiles := []string{}

uncommitedMigrations := LoadUncommitedMigrations(ctx)
unappliedMigrations := LoadUnappliedMigrations(ctx)

if ctx.DryRun {
dryRunGeneration(uncommitedMigrations)
dryRunGeneration(unappliedMigrations)
return removedFiles, nil
}

for _, migration := range uncommitedMigrations {
for _, migration := range unappliedMigrations {
upFileName, downFileName := migration.FileNames(SelectExtention(ctx.Database))

upFilePath := path.Join(ctx.WorkDir, upFileName)
Expand All @@ -94,7 +94,7 @@ func (g *Generator) Clean(ctx *Context) ([]string, error) {
removedFiles = append(removedFiles, upFilePath, downFilePath)
}

ctx.Migrations = ctx.Migrations[:len(ctx.Migrations)-len(uncommitedMigrations)]
ctx.Migrations = ctx.Migrations[:len(ctx.Migrations)-len(unappliedMigrations)]
return removedFiles, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/sqlMigrator/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func InitializeMigrationsFolder(ctx *Context) error {
}

// TODO: optimize to read from last, and break when commit status is true
func LoadUncommitedMigrations(ctx *Context) []*MigrationFileEntry {
func LoadUnappliedMigrations(ctx *Context) []*MigrationFileEntry {
migrations := []*MigrationFileEntry{}

for _, migration := range ctx.Migrations {
Expand All @@ -43,7 +43,7 @@ func LoadUncommitedMigrations(ctx *Context) []*MigrationFileEntry {
return migrations
}

func LoadCommittedMigrations(ctx *Context) []*MigrationFileEntry {
func LoadAppliedMigrations(ctx *Context) []*MigrationFileEntry {
migrations := []*MigrationFileEntry{}

for _, migration := range ctx.Migrations {
Expand Down
8 changes: 4 additions & 4 deletions pkg/sqlMigrator/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestInitializeMigrationsFolder(t *testing.T) {
func TestLoadUncommitedMigration(t *testing.T) {
t.Run("returns an empty migration entry if the log file is empty", func(t *testing.T) {
ctx := sqlmigrator.NewContext(t.TempDir(), sqlmigrator.SQLiteDB, "migrator.log", false)
migration := sqlmigrator.LoadUncommitedMigrations(ctx)
migration := sqlmigrator.LoadUnappliedMigrations(ctx)
assert.Empty(t, migration)
assert.Equal(t, 0, len(migration))
})
Expand All @@ -70,7 +70,7 @@ func TestLoadUncommitedMigration(t *testing.T) {
assert.NoError(t, err)

ctx.LoadMigrationEntries()
migrations := sqlmigrator.LoadUncommitedMigrations(ctx)
migrations := sqlmigrator.LoadUnappliedMigrations(ctx)
assert.NotEmpty(t, migrations)

expected := func() []*sqlmigrator.MigrationFileEntry {
Expand Down Expand Up @@ -98,7 +98,7 @@ func TestLoadUncommitedMigration(t *testing.T) {
func TestLoadCommittedMigration(t *testing.T) {
t.Run("returns an empty migration entry if the log file is empty", func(t *testing.T) {
ctx := sqlmigrator.NewContext(t.TempDir(), sqlmigrator.SQLiteDB, "migrator.log", false)
migration := sqlmigrator.LoadCommittedMigrations(ctx)
migration := sqlmigrator.LoadAppliedMigrations(ctx)
assert.Empty(t, migration)
})

Expand All @@ -120,7 +120,7 @@ func TestLoadCommittedMigration(t *testing.T) {
assert.NoError(t, err)

ctx.LoadMigrationEntries()
migrations := sqlmigrator.LoadCommittedMigrations(ctx)
migrations := sqlmigrator.LoadAppliedMigrations(ctx)
assert.NotEmpty(t, migrations)

expected := func() []*sqlmigrator.MigrationFileEntry {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sqlMigrator/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func NewMigrator(dbRepo DBRepo) *migrator {

func (m *migrator) MigrateUp(ctx *Context, num int) ([]*MigrationFileEntry, error) {
appliedMigrations := []*MigrationFileEntry{}
migrationToApply := LoadUncommitedMigrations(ctx)
migrationToApply := LoadUnappliedMigrations(ctx)

if len(migrationToApply) == 0 {
fmt.Println("no migrations to apply")
Expand Down Expand Up @@ -63,7 +63,7 @@ func (m *migrator) MigrateUp(ctx *Context, num int) ([]*MigrationFileEntry, erro

func (m *migrator) MigrateDown(ctx *Context, num int) ([]*MigrationFileEntry, error) {
rolledBackMigrations := []*MigrationFileEntry{}
migrationToApply := LoadCommittedMigrations(ctx)
migrationToApply := LoadAppliedMigrations(ctx)

if len(migrationToApply) == 0 {
fmt.Println("no migrations to rollback")
Expand Down
2 changes: 1 addition & 1 deletion pkg/sqlMigrator/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
)

func checkUnappliedMigrations(t *testing.T, ctx *sqlmigrator.Context, expected int) {
unappliedMigrations := sqlmigrator.LoadUncommitedMigrations(ctx)
unappliedMigrations := sqlmigrator.LoadUnappliedMigrations(ctx)
assert.Equal(t, expected, len(unappliedMigrations))
}

Expand Down

0 comments on commit 7be1641

Please sign in to comment.