Skip to content

Commit

Permalink
Add flags to rebuild or refresh State DB
Browse files Browse the repository at this point in the history
  • Loading branch information
Ouziel committed Dec 26, 2024
1 parent f7691ae commit c10b8c3
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions counterparty-core/counterpartycore/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,22 @@ def float_range_checker(arg):
"help": "the URL of the Electrs server",
},
],
[
("--refresh-state-db",),
{
"help": "On startup, rebuild non rollbackable tables in the state database",
"action": "store_true",
"default": False,
},
],
[
("--rebuild-state-db",),
{
"help": "On startup, rebuild all tables in the state database",
"action": "store_true",
"default": False,
},
],
]


Expand Down
11 changes: 11 additions & 0 deletions counterparty-core/counterpartycore/lib/api/dbbuilder.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,14 @@ def rollback_state_db(state_db, block_index):
reapply_migrations(state_db, MIGRATIONS_AFTER_ROLLBACK)

logger.info(f"State DB rolled back in {time.time() - start_time:.2f} seconds")


def refresh_state_db(state_db):
logger.info("Rebuilding non rollbackable tables in State DB...")
start_time = time.time()

with state_db:
with log.Spinner("Re-applying migrations..."):
reapply_migrations(state_db, MIGRATIONS_AFTER_ROLLBACK)

logger.info(f"State DB refreshed in {time.time() - start_time:.2f} seconds")
7 changes: 7 additions & 0 deletions counterparty-core/counterpartycore/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,6 +741,13 @@ def start_all(args):
blocks.check_database_version(db)
database.optimize(db)

if args.rebuild_state_db:
dbbuilder.build_state_db()
elif args.refresh_state_db:
state_db = database.get_db_connection(config.STATE_DATABASE, read_only=False)
dbbuilder.refresh_state_db(state_db)
state_db.close()

# Check software version
check.software_version()

Expand Down
1 change: 1 addition & 0 deletions release-notes/release-notes-v10.9.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@

- Add `--cache-dir` flag
- Add `severity` field to JSON logs for compatibility
- Add `--refresh-state-db` and `--rebuild-state-db` flags for `start` command

# Credits

Expand Down

0 comments on commit c10b8c3

Please sign in to comment.