From: Matt Corallo Date: Sun, 11 Sep 2022 16:41:12 +0000 (+0000) Subject: Add upgrade query support X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=commitdiff_plain;h=b466192ee447c3729920f9c7f6d765545421dab5;p=rapid-gossip-sync-server Add upgrade query support --- diff --git a/src/config.rs b/src/config.rs index afc9871..e7ba89c 100644 --- a/src/config.rs +++ b/src/config.rs @@ -84,6 +84,13 @@ pub(crate) fn db_index_creation_query() -> &'static str { " } +pub(crate) async fn upgrade_db(schema: i32, client: &mut tokio_postgres::Client) { + match schema { + SCHEMA_VERSION => {}, + _ => panic!("Unknown schema in db: {}, we support up to {}", schema, SCHEMA_VERSION), + } +} + /// EDIT ME pub(crate) fn ln_peers() -> Vec<(PublicKey, SocketAddr)> { vec![ diff --git a/src/persistence.rs b/src/persistence.rs index 00ca70d..d961b30 100644 --- a/src/persistence.rs +++ b/src/persistence.rs @@ -27,7 +27,7 @@ impl GossipPersister { pub(crate) async fn persist_gossip(&mut self) { let connection_config = config::db_connection_config(); - let (client, connection) = + let (mut client, connection) = connection_config.connect(NoTls).await.unwrap(); tokio::spawn(async move { @@ -45,6 +45,11 @@ impl GossipPersister { panic!("db init error: {}", initialization_error); } + let cur_schema = client.query("SELECT db_schema FROM config WHERE id = $1", &[&1]).await.unwrap(); + if !cur_schema.is_empty() { + config::upgrade_db(cur_schema[0].get(0), &mut client).await; + } + let initialization = client .execute( // TODO: figure out a way to fix the id value without Postgres complaining about