Skip to content

Commit

Permalink
tapdb: add unit test for sqlite pre-migration backup functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ffranr committed Jun 27, 2024
1 parent 09daf8a commit fdcaf2a
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions tapdb/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package tapdb

import (
"context"
"os"
"path/filepath"
"strings"
"testing"

"github.com/btcsuite/btcd/wire"
Expand Down Expand Up @@ -130,3 +133,89 @@ func TestMigrationDowngrade(t *testing.T) {
err := db.ExecuteMigrations(TargetLatest, WithLatestVersion(1))
require.ErrorIs(t, err, ErrMigrationDowngrade)
}

// findDbBackupFilePath walks the directory of the given database file path and
// returns the path to the backup file.
func findDbBackupFilePath(t *testing.T, dbFilePath string) string {
var dbBackupFilePath string
dir := filepath.Dir(dbFilePath)

err := filepath.Walk(dir,
func(path string, info os.FileInfo, err error) error {
if err != nil {
return err
}

hasSuffix := strings.HasSuffix(info.Name(), ".backup")
if !info.IsDir() && hasSuffix {
dbBackupFilePath = path
}
return nil
})
require.NoError(t, err)

return dbBackupFilePath
}

// TestSqliteMigrationBackup tests that the sqlite database backup and migration
// functionality works.
//
// In this test we will load from file a database that is at version 14. The
// on file database is already populated with asset data. We will create a
// database backup, migrate the source db, and then check the following:
//
// 1. The asset data is present in the migrated database.
// 2. The asset data is present in the backup database.
func TestSqliteMigrationBackup(t *testing.T) {
ctx := context.Background()

db := NewTestDBWithVersion(t, 14)

// We need to insert some test data that will be affected by the
// migration number 15.
InsertTestdata(t, db.BaseDB, "migrations_test_00015_dummy_data.sql")

// And now that we have test data inserted, we can create a backup and
// migrate to the latest version.
err := db.ExecuteMigrations(db.backupAndMigrate)

Check failure on line 180 in tapdb/migrations_test.go

View workflow job for this annotation

GitHub Actions / run unit tests (unit dbbackend=postgres)

db.backupAndMigrate undefined (type *PostgresStore has no field or method backupAndMigrate)
require.NoError(t, err)

// Inspect the migrated database. Make sure the single asset that was
// inserted actually has two witnesses with the correct order.
_, assetStore := newAssetStoreFromDB(db.BaseDB)
assets, err := assetStore.FetchAllAssets(ctx, false, false, nil)
require.NoError(t, err)

require.Len(t, assets, 1)
require.Len(t, assets[0].PrevWitnesses, 2)
require.Equal(
t, wire.TxWitness{{0xaa}}, assets[0].PrevWitnesses[0].TxWitness,
)
require.Equal(
t, wire.TxWitness{{0xbb}}, assets[0].PrevWitnesses[1].TxWitness,
)

// Now we will inspect the backup database (which should not have been
// modified by the migration).
//
// Find the backup database file.
dbBackupFilePath := findDbBackupFilePath(t, db.cfg.DatabaseFileName)

Check failure on line 202 in tapdb/migrations_test.go

View workflow job for this annotation

GitHub Actions / run unit tests (unit dbbackend=postgres)

db.cfg.DatabaseFileName undefined (type *PostgresConfig has no field or method DatabaseFileName)

// Construct a new database handle for the backup database.
backupDb := NewTestSqliteDbHandleFromPath(t, dbBackupFilePath)
require.NoError(t, err)

// Inspect the backup database.
_, assetStore = newAssetStoreFromDB(backupDb.BaseDB)
assets, err = assetStore.FetchAllAssets(ctx, false, false, nil)
require.NoError(t, err)

require.Len(t, assets, 1)
require.Len(t, assets[0].PrevWitnesses, 2)
require.Equal(
t, wire.TxWitness{{0xaa}}, assets[0].PrevWitnesses[0].TxWitness,
)
require.Equal(
t, wire.TxWitness{{0xbb}}, assets[0].PrevWitnesses[1].TxWitness,
)
}

0 comments on commit fdcaf2a

Please sign in to comment.