Skip to content

Commit

Permalink
Enforce declarative state migrations
Browse files Browse the repository at this point in the history
There is currently no reason to build migrations at runtime. So, this
change enforces declaring migrations for a state statically.
  • Loading branch information
Jurshsmith committed Apr 18, 2024
1 parent 1f8dc78 commit ce815fa
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 19 deletions.
9 changes: 3 additions & 6 deletions chaindexing-tests/src/tests/states.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,9 @@ impl ContractState for Nft {
}
struct NftMigrations;
impl StateMigrations for NftMigrations {
fn migrations(&self) -> Vec<&'static str> {
vec![
"CREATE TABLE IF NOT EXISTS nfts (
token_id INTEGER NOT NULL,
)",
]
fn migrations(&self) -> &'static [&'static str] {
&["CREATE TABLE IF NOT EXISTS nfts (
token_id INTEGER NOT NULL)"]
}
}

Expand Down
22 changes: 9 additions & 13 deletions chaindexing/src/states/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::STATE_VERSIONS_TABLE_PREFIX;
// easen the type strictness for consumer applications.
// Trait/Callback? this way, consumer apps can statically visualize their migrations
pub trait StateMigrations: Send + Sync {
fn migrations(&self) -> Vec<&'static str>;
fn migrations(&self) -> &'static [&'static str];

fn get_table_names(&self) -> Vec<String> {
self.migrations().iter().fold(vec![], |mut table_names, migration| {
Expand Down Expand Up @@ -411,8 +411,8 @@ mod contract_state_migrations_get_migration_test {
struct TestState;

impl StateMigrations for TestState {
fn migrations(&self) -> Vec<&'static str> {
vec![
fn migrations(&self) -> &'static [&'static str] {
&[
"CREATE TABLE IF NOT EXISTS nft_states (
token_id INTEGER NOT NULL,
contract_address VARCHAR NOT NULL,
Expand All @@ -429,29 +429,25 @@ mod contract_state_migrations_get_migration_test {
struct TestStateWithPrimaryKey;

impl StateMigrations for TestStateWithPrimaryKey {
fn migrations(&self) -> Vec<&'static str> {
vec![
"CREATE TABLE IF NOT EXISTS nft_states (
fn migrations(&self) -> &'static [&'static str] {
&["CREATE TABLE IF NOT EXISTS nft_states (
id SERIAL PRIMARY KEY,
token_id INTEGER NOT NULL,
contract_address VARCHAR NOT NULL,
owner_address VARCHAR NOT NULL
)",
]
)"]
}
}

struct TestStateWithJsonField;

impl StateMigrations for TestStateWithJsonField {
fn migrations(&self) -> Vec<&'static str> {
vec![
"CREATE TABLE IF NOT EXISTS nft_states (
fn migrations(&self) -> &'static [&'static str] {
&["CREATE TABLE IF NOT EXISTS nft_states (
id SERIAL PRIMARY KEY,
token_id INTEGER NOT NULL,
json_field JSON DEFAULT '{}',
)",
]
)"]
}
}
}

0 comments on commit ce815fa

Please sign in to comment.