From 405fc2b4d2e4768e0b9a950767c9c1007fc53c36 Mon Sep 17 00:00:00 2001 From: Steven Date: Sun, 5 Nov 2023 15:49:57 +0800 Subject: [PATCH] chore: simplify find migration history --- store/db/mysql/migration_history.go | 14 +++----------- store/db/sqlite/migration_history.go | 22 +++------------------- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/store/db/mysql/migration_history.go b/store/db/mysql/migration_history.go index 8eca1768..4dc79c13 100644 --- a/store/db/mysql/migration_history.go +++ b/store/db/mysql/migration_history.go @@ -2,7 +2,6 @@ package mysql import ( "context" - "strings" ) type MigrationHistory struct { @@ -15,18 +14,11 @@ type MigrationHistoryUpsert struct { } type MigrationHistoryFind struct { - Version *string } -func (d *DB) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) { - where, args := []string{"1 = 1"}, []any{} - - if v := find.Version; v != nil { - where, args = append(where, "`version` = ?"), append(args, *v) - } - - query := "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` WHERE " + strings.Join(where, " AND ") + " ORDER BY `created_ts` DESC" - rows, err := d.db.QueryContext(ctx, query, args...) +func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFind) ([]*MigrationHistory, error) { + query := "SELECT `version`, UNIX_TIMESTAMP(`created_ts`) FROM `migration_history` ORDER BY `created_ts` DESC" + rows, err := d.db.QueryContext(ctx, query) if err != nil { return nil, err } diff --git a/store/db/sqlite/migration_history.go b/store/db/sqlite/migration_history.go index 0c5224e7..922500d7 100644 --- a/store/db/sqlite/migration_history.go +++ b/store/db/sqlite/migration_history.go @@ -2,7 +2,6 @@ package sqlite import ( "context" - "strings" ) type MigrationHistory struct { @@ -15,26 +14,11 @@ type MigrationHistoryUpsert struct { } type MigrationHistoryFind struct { - Version *string } -func (d *DB) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) { - where, args := []string{"1 = 1"}, []any{} - - if v := find.Version; v != nil { - where, args = append(where, "version = ?"), append(args, *v) - } - - query := ` - SELECT - version, - created_ts - FROM - migration_history - WHERE ` + strings.Join(where, " AND ") + ` - ORDER BY created_ts DESC - ` - rows, err := d.db.QueryContext(ctx, query, args...) +func (d *DB) FindMigrationHistoryList(ctx context.Context, _ *MigrationHistoryFind) ([]*MigrationHistory, error) { + query := "SELECT `version`, `created_ts` FROM `migration_history` ORDER BY `created_ts` DESC" + rows, err := d.db.QueryContext(ctx, query) if err != nil { return nil, err }