channeldb: extract before migration func
This commit is contained in:
parent
f47de09850
commit
31f72f6c7d
@ -24,6 +24,43 @@ var (
|
||||
testCltvDelta = int32(50)
|
||||
)
|
||||
|
||||
// beforeMigrationFuncV11 insert the test invoices in the database.
|
||||
func beforeMigrationFuncV11(t *testing.T, d *DB, invoices []Invoice) {
|
||||
err := d.Update(func(tx *bbolt.Tx) error {
|
||||
invoicesBucket, err := tx.CreateBucketIfNotExists(
|
||||
invoiceBucket,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
invoiceNum := uint32(1)
|
||||
for _, invoice := range invoices {
|
||||
var invoiceKey [4]byte
|
||||
byteOrder.PutUint32(invoiceKey[:], invoiceNum)
|
||||
invoiceNum++
|
||||
|
||||
var buf bytes.Buffer
|
||||
err := serializeInvoiceLegacy(&buf, &invoice) // nolint:scopelint
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = invoicesBucket.Put(
|
||||
invoiceKey[:], buf.Bytes(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// TestMigrateInvoices checks that invoices are migrated correctly.
|
||||
func TestMigrateInvoices(t *testing.T) {
|
||||
t.Parallel()
|
||||
@ -49,42 +86,6 @@ func TestMigrateInvoices(t *testing.T) {
|
||||
},
|
||||
}
|
||||
|
||||
beforeMigrationFunc := func(d *DB) {
|
||||
err := d.Update(func(tx *bbolt.Tx) error {
|
||||
invoicesBucket, err := tx.CreateBucketIfNotExists(
|
||||
invoiceBucket,
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
invoiceNum := uint32(1)
|
||||
for _, invoice := range invoices {
|
||||
var invoiceKey [4]byte
|
||||
byteOrder.PutUint32(invoiceKey[:], invoiceNum)
|
||||
invoiceNum++
|
||||
|
||||
var buf bytes.Buffer
|
||||
err := serializeInvoiceLegacy(&buf, &invoice)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
err = invoicesBucket.Put(
|
||||
invoiceKey[:], buf.Bytes(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
// Verify that all invoices were migrated.
|
||||
afterMigrationFunc := func(d *DB) {
|
||||
meta, err := d.FetchMeta(nil)
|
||||
@ -120,7 +121,7 @@ func TestMigrateInvoices(t *testing.T) {
|
||||
}
|
||||
|
||||
applyMigration(t,
|
||||
beforeMigrationFunc,
|
||||
func(d *DB) { beforeMigrationFuncV11(t, d, invoices) },
|
||||
afterMigrationFunc,
|
||||
migrateInvoices,
|
||||
false)
|
||||
|
Loading…
Reference in New Issue
Block a user