feat(store): scaffold batch import run repos

This commit is contained in:
phamnazage-jpg
2026-05-22 13:50:50 +08:00
parent 9ba1c92da2
commit d68fb9daa3
4 changed files with 472 additions and 7 deletions

View File

@@ -5,10 +5,12 @@ import (
"database/sql"
"errors"
"fmt"
"io/fs"
"path/filepath"
"testing"
_ "modernc.org/sqlite"
"sub2api-cn-relay-manager/internal/store/migrations"
"sub2api-cn-relay-manager/internal/store/sqlite"
)
@@ -103,13 +105,14 @@ func TestStoreInitRollsBackTransaction(t *testing.T) {
func TestStoreInitRecordsMigrationLedgerOnce(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "state.db")
dsn := fmt.Sprintf("file:%s?_busy_timeout=5000", filepath.ToSlash(dbPath))
wantMigrations := migrationCount(t)
store1, err := sqlite.Open(context.Background(), dsn)
if err != nil {
t.Fatalf("first sqlite.Open() error = %v", err)
}
if got := countRows(t, store1.SQLDB(), "schema_migrations"); got != 6 {
t.Fatalf("schema_migrations row count after first open = %d, want 6", got)
if got := countRows(t, store1.SQLDB(), "schema_migrations"); got != wantMigrations {
t.Fatalf("schema_migrations row count after first open = %d, want %d", got, wantMigrations)
}
if err := store1.Close(); err != nil {
t.Fatalf("first store.Close() error = %v", err)
@@ -121,14 +124,15 @@ func TestStoreInitRecordsMigrationLedgerOnce(t *testing.T) {
}
defer closeTestStore(t, store2)
if got := countRows(t, store2.SQLDB(), "schema_migrations"); got != 6 {
t.Fatalf("schema_migrations row count after second open = %d, want 6", got)
if got := countRows(t, store2.SQLDB(), "schema_migrations"); got != wantMigrations {
t.Fatalf("schema_migrations row count after second open = %d, want %d", got, wantMigrations)
}
}
func TestStoreInitBackfillsLedgerForCompletePreLedgerSchema(t *testing.T) {
dbPath := filepath.Join(t.TempDir(), "state.db")
dsn := fmt.Sprintf("file:%s?_busy_timeout=5000", filepath.ToSlash(dbPath))
wantMigrations := migrationCount(t)
rawDB := openRawSQLiteDB(t, dsn)
createLegacy0001Schema(t, rawDB)
@@ -140,8 +144,8 @@ func TestStoreInitBackfillsLedgerForCompletePreLedgerSchema(t *testing.T) {
}
defer closeTestStore(t, store)
if got := countRows(t, store.SQLDB(), "schema_migrations"); got != 6 {
t.Fatalf("schema_migrations row count after backfill = %d, want 6", got)
if got := countRows(t, store.SQLDB(), "schema_migrations"); got != wantMigrations {
t.Fatalf("schema_migrations row count after backfill = %d, want %d", got, wantMigrations)
}
}
@@ -205,6 +209,16 @@ func closeRawSQLiteDB(t *testing.T, db *sql.DB) {
}
}
func migrationCount(t *testing.T) int {
t.Helper()
names, err := fs.Glob(migrations.Files, "*.sql")
if err != nil {
t.Fatalf("fs.Glob(migrations.Files) error = %v", err)
}
return len(names)
}
func createLegacy0001Schema(t *testing.T, db *sql.DB) {
t.Helper()