Skip to content

Commit

Permalink
store: prepare for breaking changes of the database
Browse files Browse the repository at this point in the history
  • Loading branch information
zorancv committed Jan 9, 2025
1 parent 1d29605 commit 48c9487
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
2 changes: 1 addition & 1 deletion node/src/store_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ impl StoreBuilder {
);
block_store
.update_db_version()
.expect("Updating `db_version` works");
.expect("Updating `db_version` should work");

Arc::new(DieselStore::new(subgraph_store, block_store))
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-- we don't rename the column name back in order to avoid the accidental downgrade of the binary
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
alter table public.db_version
rename column db_version to database_version;
16 changes: 16 additions & 0 deletions store/postgres/src/block_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ use self::primary::Chain;
#[cfg(debug_assertions)]
pub const FAKE_NETWORK_SHARED: &str = "fake_network_shared";

// Highest version of the database that the executable supports.
// To be incremented on each breaking change to the database.
const SUPPORTED_DB_VERSION: i64 = 3;

/// The status of a chain: whether we can only read from the chain, or
/// whether it is ok to ingest from it, too
#[derive(Copy, Clone)]
Expand Down Expand Up @@ -531,6 +535,18 @@ impl BlockStore {
.set(dbv::version.eq(3))
.execute(&mut conn)?;
};
if version < SUPPORTED_DB_VERSION {
// Bump it to make sure that all executables are working with the same DB format
diesel::update(dbv::table)
.set(dbv::version.eq(SUPPORTED_DB_VERSION))
.execute(&mut conn)?;
};
if version > SUPPORTED_DB_VERSION {
panic!(
"The executable is too old and doesn't support the database version: {}",
version
)
}
Ok(())
}

Expand Down
2 changes: 1 addition & 1 deletion store/postgres/src/primary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ table! {

table! {
public.db_version(version) {
#[sql_name = "db_version"]
#[sql_name = "database_version"]
version -> BigInt,
}
}
Expand Down

0 comments on commit 48c9487

Please sign in to comment.