Skip to content

Commit 7fa4f48

Browse files
author
AztecBot
committed
Merge branch 'next' into merge-train/avm
2 parents 2b281e9 + 9b1bc1e commit 7fa4f48

31 files changed

+3806
-4
lines changed

aztec-up/bin/aztec

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,12 @@ EOF
562562
exec $(dirname $0)/.aztec-run aztec \
563563
node --no-warnings /usr/src/yarn-project/aztec/dest/bin/index.js "$@"
564564
;;
565+
migrate-ha-db)
566+
# DB migration command, inject the database URL
567+
export ENV_VARS_TO_INJECT="DATABASE_URL"
568+
exec $(dirname $0)/.aztec-run aztec \
569+
node --no-warnings /usr/src/yarn-project/aztec/dest/bin/index.js "$@"
570+
;;
565571
*)
566572
export ENV_VARS_TO_INJECT="SECRET_KEY"
567573
exec $(dirname $0)/.aztec-run aztec \

yarn-project/aztec/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"@aztec/telemetry-client": "workspace:^",
6060
"@aztec/test-wallet": "workspace:^",
6161
"@aztec/txe": "workspace:^",
62+
"@aztec/validator-ha-signer": "workspace:^",
6263
"@aztec/world-state": "workspace:^",
6364
"@types/chalk": "^2.2.0",
6465
"abitype": "^0.8.11",

yarn-project/aztec/src/bin/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import { createConsoleLogger, createLogger } from '@aztec/foundation/log';
1414

1515
import { Command } from 'commander';
1616

17+
import { injectMigrateCommand } from '../cli/cmds/migrate_ha_db.js';
1718
import { injectAztecCommands } from '../cli/index.js';
1819
import { getCliVersion } from '../cli/release_version.js';
1920

@@ -55,6 +56,7 @@ async function main() {
5556
program = injectAztecNodeCommands(program, userLog, debugLogger);
5657
program = injectMiscCommands(program, userLog);
5758
program = injectValidatorKeysCommands(program, userLog);
59+
program = injectMigrateCommand(program, userLog);
5860

5961
await program.parseAsync(process.argv);
6062
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { runMigrations } from '@aztec/validator-ha-signer/migrations';
2+
3+
import type { Command } from 'commander';
4+
5+
export function injectMigrateCommand(program: Command, log: (msg: string) => void): Command {
6+
const migrateCommand = program.command('migrate-ha-db').description('Run validator-ha-signer database migrations');
7+
8+
migrateCommand
9+
.command('up')
10+
.description('Apply pending migrations')
11+
.requiredOption('--database-url <string>', 'PostgreSQL connection string', process.env.DATABASE_URL)
12+
.option('--verbose', 'Enable verbose output', false)
13+
.action(async options => {
14+
const migrations = await runMigrations(options.databaseUrl, {
15+
direction: 'up',
16+
verbose: options.verbose,
17+
});
18+
if (migrations.length > 0) {
19+
log(`Applied migrations: ${migrations.join(', ')}`);
20+
} else {
21+
log('No migrations to apply - schema is up to date');
22+
}
23+
});
24+
25+
migrateCommand
26+
.command('down')
27+
.description('Rollback the last migration')
28+
.requiredOption('--database-url <string>', 'PostgreSQL connection string', process.env.DATABASE_URL)
29+
.option('--verbose', 'Enable verbose output', false)
30+
.action(async options => {
31+
const migrations = await runMigrations(options.databaseUrl, {
32+
direction: 'down',
33+
verbose: options.verbose,
34+
});
35+
if (migrations.length > 0) {
36+
log(`Rolled back migrations: ${migrations.join(', ')}`);
37+
} else {
38+
log('No migrations to rollback');
39+
}
40+
});
41+
42+
return program;
43+
}

yarn-project/aztec/tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@
9696
{
9797
"path": "../txe"
9898
},
99+
{
100+
"path": "../validator-ha-signer"
101+
},
99102
{
100103
"path": "../world-state"
101104
}

yarn-project/foundation/src/config/env_var.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,4 +312,15 @@ export type EnvVar =
312312
| 'FISHERMAN_MODE'
313313
| 'MAX_ALLOWED_ETH_CLIENT_DRIFT_SECONDS'
314314
| 'LEGACY_BLS_CLI'
315-
| 'DEBUG_FORCE_TX_PROOF_VERIFICATION';
315+
| 'DEBUG_FORCE_TX_PROOF_VERIFICATION'
316+
| 'SLASHING_PROTECTION_NODE_ID'
317+
| 'SLASHING_PROTECTION_POLLING_INTERVAL_MS'
318+
| 'SLASHING_PROTECTION_SIGNING_TIMEOUT_MS'
319+
| 'SLASHING_PROTECTION_ENABLED'
320+
| 'SLASHING_PROTECTION_MAX_STUCK_DUTIES_AGE_MS'
321+
| 'VALIDATOR_HA_DATABASE_URL'
322+
| 'VALIDATOR_HA_RUN_MIGRATIONS'
323+
| 'VALIDATOR_HA_POOL_MAX'
324+
| 'VALIDATOR_HA_POOL_MIN'
325+
| 'VALIDATOR_HA_POOL_IDLE_TIMEOUT_MS'
326+
| 'VALIDATOR_HA_POOL_CONNECTION_TIMEOUT_MS';

yarn-project/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
"txe",
6262
"test-wallet",
6363
"validator-client",
64+
"validator-ha-signer",
6465
"wallet-sdk",
6566
"world-state"
6667
],

yarn-project/tsconfig.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
{ "path": "aztec.js/tsconfig.json" },
3030
{ "path": "aztec-node/tsconfig.json" },
3131
{ "path": "validator-client/tsconfig.json" },
32+
{ "path": "validator-ha-signer/tsconfig.json" },
3233
{ "path": "bb-prover/tsconfig.json" },
3334
{ "path": "bot/tsconfig.json" },
3435
{ "path": "constants/tsconfig.json" },
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
# Database Migrations Guide
2+
3+
This package uses [node-pg-migrate](https://github.com/salsita/node-pg-migrate) for managing database schema changes.
4+
5+
## Quick Reference
6+
7+
```bash
8+
# Run pending migrations
9+
aztec migrate-ha-db up --database-url postgresql://...
10+
11+
# Rollback last migration
12+
aztec migrate-ha-db down --database-url postgresql://...
13+
```
14+
15+
## Migration Files
16+
17+
Migrations are located in the `migrations/` directory and are named with timestamps:
18+
19+
```
20+
migrations/
21+
└── 1_initial-schema.ts
22+
```
23+
24+
## Creating New Migrations
25+
26+
When you need to modify the database schema:
27+
28+
```bash
29+
# Generate a new migration file
30+
npx node-pg-migrate create add-new-field
31+
32+
# This creates: migrations/[timestamp]_add-new-field.ts
33+
```
34+
35+
Edit the generated file:
36+
37+
```typescript
38+
import type { MigrationBuilder } from 'node-pg-migrate';
39+
40+
export async function up(pgm: MigrationBuilder): Promise<void> {
41+
// Add your schema changes here
42+
pgm.addColumn('validator_duties', {
43+
new_field: { type: 'text', notNull: false },
44+
});
45+
}
46+
47+
export async function down(pgm: MigrationBuilder): Promise<void> {
48+
// Reverse the changes
49+
pgm.dropColumn('validator_duties', 'new_field');
50+
}
51+
```
52+
53+
## Production Deployment
54+
55+
### Option 1: Kubernetes Init Container
56+
57+
```yaml
58+
apiVersion: apps/v1
59+
kind: Deployment
60+
metadata:
61+
name: validator
62+
spec:
63+
template:
64+
spec:
65+
initContainers:
66+
- name: db-migrate
67+
image: aztecprotocol/aztec:<image_tag>
68+
command: ['node', '--no-warnings', '/usr/src/yarn-project/aztec/dest/bin/index.js', 'migrate-ha-db', 'up']
69+
env:
70+
- name: DATABASE_URL
71+
valueFrom:
72+
secretKeyRef:
73+
name: db-credentials
74+
key: connection-string
75+
containers:
76+
- name: validator
77+
image: aztecprotocol/aztec:<image_tag>
78+
# ... validator config
79+
```
80+
81+
### Option 2: Separate Migration Job
82+
83+
```yaml
84+
apiVersion: batch/v1
85+
kind: Job
86+
metadata:
87+
name: validator-migrate-v1
88+
spec:
89+
template:
90+
spec:
91+
containers:
92+
- name: migrate
93+
image: aztecprotocol/aztec:<image_tag>
94+
command: ['node', '--no-warnings', '/usr/src/yarn-project/aztec/dest/bin/index.js', 'migrate-ha-db', 'up']
95+
env:
96+
- name: DATABASE_URL
97+
valueFrom:
98+
secretKeyRef:
99+
name: db-credentials
100+
key: connection-string
101+
restartPolicy: Never
102+
```
103+
104+
### Option 3: CI/CD Pipeline
105+
106+
```yaml
107+
# GitHub Actions example
108+
- name: Run Database Migrations
109+
run: |
110+
docker run --rm \
111+
-e DATABASE_URL=${{ secrets.DATABASE_URL }} \
112+
aztecprotocol/aztec:<image_tag> \
113+
migrate-ha-db up
114+
```
115+
116+
## High Availability Considerations
117+
118+
The migrations use idempotent SQL operations (`IF NOT EXISTS`, `ON CONFLICT`, etc.), making them safe to run concurrently from multiple nodes. However, for cleaner logs and faster deployments, we recommend:
119+
120+
1. **Run migrations once** from an init container or migration job
121+
2. **Then start** multiple validator nodes
122+
123+
If multiple nodes run migrations simultaneously, they will all succeed, but you'll see redundant log output.
124+
125+
## Development Workflow
126+
127+
```bash
128+
# 1. Create migration
129+
npx node-pg-migrate create my-feature
130+
131+
# 2. Edit migrations/[timestamp]_my-feature.ts
132+
133+
# 3. Test migration locally
134+
aztec migrate-ha-db up --database-url postgresql://localhost:5432/validator_dev
135+
136+
# 4. Test rollback
137+
aztec migrate-ha-db down --database-url postgresql://localhost:5432/validator_dev
138+
139+
# 5. Re-apply
140+
aztec migrate-ha-db up --database-url postgresql://localhost:5432/validator_dev
141+
142+
# 6. Run tests
143+
yarn test
144+
```
145+
146+
## Troubleshooting
147+
148+
### Migration Failed Midway
149+
150+
If a migration fails partway through:
151+
152+
```bash
153+
# The failed migration will be marked as running
154+
# Fix the issue and re-run
155+
aztec migrate-ha-db up --database-url postgresql://...
156+
```
157+
158+
### Reset Development Database
159+
160+
```bash
161+
# Drop all migrations
162+
while aztec migrate-ha-db down --database-url postgresql://localhost:5432/validator_dev; do :; done
163+
164+
# Or drop the database entirely
165+
psql -c "DROP DATABASE validator_dev;"
166+
psql -c "CREATE DATABASE validator_dev;"
167+
168+
# Re-run migrations
169+
aztec migrate-ha-db up --database-url postgresql://localhost:5432/validator_dev
170+
```
171+
172+
### Check Applied Migrations
173+
174+
```bash
175+
# Query the migrations table
176+
psql $DATABASE_URL -c "SELECT * FROM pgmigrations ORDER BY id;"
177+
```
178+
179+
## Migration Best Practices
180+
181+
1. **Always provide `down()` migrations** for rollback capability
182+
2. **Test migrations on a copy of production data** before deploying
183+
3. **Make migrations backward compatible** when possible
184+
4. **Avoid data migrations in schema migrations** - use separate data migration scripts
185+
5. **Keep migrations small and focused** - one logical change per migration
186+
6. **Never modify committed migrations** - create a new migration instead

0 commit comments

Comments
 (0)