From b466192ee447c3729920f9c7f6d765545421dab5 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Sun, 11 Sep 2022 16:41:12 +0000 Subject: [PATCH] Add upgrade query support --- src/config.rs | 7 +++++++ src/persistence.rs | 7 ++++++- 2 files changed, 13 insertions(+), 1 deletion(-) 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 -- 2.39.5