Persistent ledger migration

This complete test uses disposable PostgreSQL to migrate the earlier schema twice and preserve spending, reservations and unknown legacy ages. Run :credentials:plugins:verifiable-intent:test --tests "*LedgerMigrationDocumentationTest" with Docker available. The fixture CREATE/INSERT statements belong only in the disposable test database. Production deployment calls initializeSchema using a migration identity; runtime uses restricted DML credentials. Backup/PITR and authenticated payment-journal recovery require separate operational qualification.

The source block is an exact copy of a compiled SDK file. Documentation CI checks synchronization, and required test gates check execution.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package org.trustweave.credential.vi

import org.junit.jupiter.api.Test
import org.postgresql.ds.PGSimpleDataSource
import org.testcontainers.containers.PostgreSQLContainer
import org.trustweave.credential.vi.verification.PostgresIntentLedger
import kotlin.test.assertEquals
import kotlin.test.assertNull
import kotlin.test.assertTrue

class LedgerMigrationDocumentationTest {
    @Test
    fun `documented schema upgrade is idempotent and preserves legacy reservations`() {
        PostgreSQLContainer<Nothing>("postgres:16-alpine").use { postgres ->
            postgres.start()
            val source =
                PGSimpleDataSource().apply {
                    setURL(postgres.jdbcUrl)
                    user = postgres.username
                    password = postgres.password
                }
            // This fixture represents the earlier schema; run only in this disposable database.
            source.connection.use { connection ->
                connection.createStatement().use { sql ->
                    sql.execute(
                        "CREATE TABLE vi_budget_accounts(scope VARCHAR(43) PRIMARY KEY, currency VARCHAR(3) NOT NULL, maximum BIGINT NOT NULL, spent BIGINT NOT NULL)",
                    )
                    sql.execute(
                        "CREATE TABLE vi_budget_reservations(scope VARCHAR(43) REFERENCES vi_budget_accounts(scope), transaction_hash VARCHAR(43), challenge_hash VARCHAR(43) UNIQUE, amount BIGINT NOT NULL, PRIMARY KEY(scope,transaction_hash))",
                    )
                    sql.execute("INSERT INTO vi_budget_accounts VALUES ('legacy','USD',100,5)")
                    sql.execute("INSERT INTO vi_budget_reservations VALUES ('legacy','transaction','challenge',5)")
                }
            }
            val ledger = PostgresIntentLedger(source)
            repeat(2) {
                // Deployment identity requires migration privileges; runtime identity needs only DML.
                ledger.initializeSchema()
                assertEquals(1L, ledger.healthSnapshot().pending)
                assertNull(ledger.healthSnapshot().oldestPendingEpochSeconds)
            }
            source.connection.use { connection ->
                connection.createStatement().use { sql ->
                    sql
                        .executeQuery(
                            "SELECT a.spent,r.amount,r.settlement_state,r.created_at FROM vi_budget_accounts a JOIN vi_budget_reservations r ON a.scope=r.scope",
                        ).use { row ->
                            assertTrue(row.next())
                            assertEquals(5L, row.getLong("spent"))
                            assertEquals(5L, row.getLong("amount"))
                            assertEquals("RESERVED", row.getString("settlement_state"))
                            assertNull(row.getTimestamp("created_at"))
                        }
                }
            }
        }
    }
}

All verified examples ? Testing acceptance


This site uses Just the Docs, a documentation theme for Jekyll.