diff --git a/rust/processor/src/db/common/models/fungible_asset_models/v2_fungible_asset_balances.rs b/rust/processor/src/db/common/models/fungible_asset_models/v2_fungible_asset_balances.rs index 8a22801b3..57b44f9b0 100644 --- a/rust/processor/src/db/common/models/fungible_asset_models/v2_fungible_asset_balances.rs +++ b/rust/processor/src/db/common/models/fungible_asset_models/v2_fungible_asset_balances.rs @@ -15,7 +15,7 @@ use crate::{ token_v2_models::v2_token_utils::{TokenStandard, V2_STANDARD}, }, schema::{ - current_fungible_asset_balances, current_unified_fungible_asset_balances_to_be_renamed, + current_fungible_asset_balances, current_fungible_asset_balances_legacy, fungible_asset_balances, }, utils::util::{ @@ -53,7 +53,7 @@ pub struct FungibleAssetBalance { #[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)] #[diesel(primary_key(storage_id))] -#[diesel(table_name = current_fungible_asset_balances)] +#[diesel(table_name = current_fungible_asset_balances_legacy)] pub struct CurrentFungibleAssetBalance { pub storage_id: String, pub owner_address: String, @@ -66,9 +66,11 @@ pub struct CurrentFungibleAssetBalance { pub token_standard: String, } +/// Note that this used to be called current_unified_fungible_asset_balances_to_be_renamed +/// and was renamed to current_fungible_asset_balances to facilitate migration #[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize, Default)] #[diesel(primary_key(storage_id))] -#[diesel(table_name = current_unified_fungible_asset_balances_to_be_renamed)] +#[diesel(table_name = current_fungible_asset_balances)] #[diesel(treat_none_as_null = true)] pub struct CurrentUnifiedFungibleAssetBalance { pub storage_id: String, diff --git a/rust/processor/src/db/postgres/migrations/2024-08-28-015552_fa_migration_fix_2/down.sql b/rust/processor/src/db/postgres/migrations/2024-08-28-015552_fa_migration_fix_2/down.sql new file mode 100644 index 000000000..3e8f58f6c --- /dev/null +++ b/rust/processor/src/db/postgres/migrations/2024-08-28-015552_fa_migration_fix_2/down.sql @@ -0,0 +1,7 @@ +-- This file should undo anything in `up.sql` +ALTER TABLE current_fungible_asset_balances + RENAME TO current_unified_fungible_asset_balances_to_be_renamed; +ALTER TABLE current_fungible_asset_balances_legacy + RENAME TO current_fungible_asset_balances; +ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed DROP COLUMN IF EXISTS asset_type; +ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed DROP COLUMN IF EXISTS token_standard; \ No newline at end of file diff --git a/rust/processor/src/db/postgres/migrations/2024-08-28-015552_fa_migration_fix_2/up.sql b/rust/processor/src/db/postgres/migrations/2024-08-28-015552_fa_migration_fix_2/up.sql new file mode 100644 index 000000000..ba39e4be4 --- /dev/null +++ b/rust/processor/src/db/postgres/migrations/2024-08-28-015552_fa_migration_fix_2/up.sql @@ -0,0 +1,15 @@ +-- Your SQL goes here +ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed +ADD COLUMN IF NOT EXISTS asset_type VARCHAR(1000) NOT NULL GENERATED ALWAYS AS (COALESCE(asset_type_v1, asset_type_v2)) STORED; +CREATE INDEX IF NOT EXISTS cufab_owner_at_index ON current_unified_fungible_asset_balances_to_be_renamed (owner_address, asset_type); +ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed +ADD COLUMN IF NOT EXISTS token_standard VARCHAR(10) NOT NULL GENERATED ALWAYS AS ( + CASE + WHEN asset_type_v1 IS NOT NULL THEN 'v1' + ELSE 'v2' + END + ) STORED; +ALTER TABLE current_fungible_asset_balances + RENAME TO current_fungible_asset_balances_legacy; +ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed + RENAME TO current_fungible_asset_balances; \ No newline at end of file diff --git a/rust/processor/src/db/postgres/schema.rs b/rust/processor/src/db/postgres/schema.rs index e88cc7871..d5cb7a917 100644 --- a/rust/processor/src/db/postgres/schema.rs +++ b/rust/processor/src/db/postgres/schema.rs @@ -423,6 +423,35 @@ diesel::table! { diesel::table! { current_fungible_asset_balances (storage_id) { + #[max_length = 66] + storage_id -> Varchar, + #[max_length = 66] + owner_address -> Varchar, + #[max_length = 66] + asset_type_v2 -> Nullable, + #[max_length = 1000] + asset_type_v1 -> Nullable, + is_primary -> Nullable, + is_frozen -> Bool, + amount_v1 -> Nullable, + amount_v2 -> Nullable, + amount -> Nullable, + last_transaction_version_v1 -> Nullable, + last_transaction_version_v2 -> Nullable, + last_transaction_version -> Nullable, + last_transaction_timestamp_v1 -> Nullable, + last_transaction_timestamp_v2 -> Nullable, + last_transaction_timestamp -> Nullable, + inserted_at -> Timestamp, + #[max_length = 1000] + asset_type -> Varchar, + #[max_length = 10] + token_standard -> Varchar, + } +} + +diesel::table! { + current_fungible_asset_balances_legacy (storage_id) { #[max_length = 66] storage_id -> Varchar, #[max_length = 66] @@ -651,31 +680,6 @@ diesel::table! { } } -diesel::table! { - current_unified_fungible_asset_balances_to_be_renamed (storage_id) { - #[max_length = 66] - storage_id -> Varchar, - #[max_length = 66] - owner_address -> Varchar, - #[max_length = 66] - asset_type_v2 -> Nullable, - #[max_length = 1000] - asset_type_v1 -> Nullable, - is_primary -> Nullable, - is_frozen -> Bool, - amount_v1 -> Nullable, - amount_v2 -> Nullable, - amount -> Nullable, - last_transaction_version_v1 -> Nullable, - last_transaction_version_v2 -> Nullable, - last_transaction_version -> Nullable, - last_transaction_timestamp_v1 -> Nullable, - last_transaction_timestamp_v2 -> Nullable, - last_transaction_timestamp -> Nullable, - inserted_at -> Timestamp, - } -} - diesel::table! { delegated_staking_activities (transaction_version, event_index) { transaction_version -> Int8, @@ -1300,6 +1304,7 @@ diesel::allow_tables_to_appear_in_same_query!( current_delegated_voter, current_delegator_balances, current_fungible_asset_balances, + current_fungible_asset_balances_legacy, current_objects, current_staking_pool_voter, current_table_items, @@ -1310,7 +1315,6 @@ diesel::allow_tables_to_appear_in_same_query!( current_token_pending_claims, current_token_royalty_v1, current_token_v2_metadata, - current_unified_fungible_asset_balances_to_be_renamed, delegated_staking_activities, delegated_staking_pool_balances, delegated_staking_pools, diff --git a/rust/processor/src/processors/fungible_asset_processor.rs b/rust/processor/src/processors/fungible_asset_processor.rs index f80133a42..da9a48c17 100644 --- a/rust/processor/src/processors/fungible_asset_processor.rs +++ b/rust/processor/src/processors/fungible_asset_processor.rs @@ -245,10 +245,10 @@ fn insert_current_fungible_asset_balances_query( impl QueryFragment + diesel::query_builder::QueryId + Send, Option<&'static str>, ) { - use schema::current_fungible_asset_balances::dsl::*; + use schema::current_fungible_asset_balances_legacy::dsl::*; ( - diesel::insert_into(schema::current_fungible_asset_balances::table) + diesel::insert_into(schema::current_fungible_asset_balances_legacy::table) .values(items_to_insert) .on_conflict(storage_id) .do_update() @@ -265,7 +265,7 @@ fn insert_current_fungible_asset_balances_query( inserted_at.eq(excluded(inserted_at)), ) ), - Some(" WHERE current_fungible_asset_balances.last_transaction_version <= excluded.last_transaction_version "), + Some(" WHERE current_fungible_asset_balances_legacy.last_transaction_version <= excluded.last_transaction_version "), ) } @@ -275,10 +275,10 @@ fn insert_current_unified_fungible_asset_balances_v1_query( impl QueryFragment + diesel::query_builder::QueryId + Send, Option<&'static str>, ) { - use schema::current_unified_fungible_asset_balances_to_be_renamed::dsl::*; + use schema::current_fungible_asset_balances::dsl::*; ( - diesel::insert_into(schema::current_unified_fungible_asset_balances_to_be_renamed::table) + diesel::insert_into(schema::current_fungible_asset_balances::table) .values(items_to_insert) .on_conflict(storage_id) .do_update() @@ -293,8 +293,8 @@ fn insert_current_unified_fungible_asset_balances_v1_query( inserted_at.eq(excluded(inserted_at)), ) ), - Some(" WHERE current_unified_fungible_asset_balances_to_be_renamed.last_transaction_version_v1 IS NULL \ - OR current_unified_fungible_asset_balances_to_be_renamed.last_transaction_version_v1 <= excluded.last_transaction_version_v1"), + Some(" WHERE current_fungible_asset_balances.last_transaction_version_v1 IS NULL \ + OR current_fungible_asset_balances.last_transaction_version_v1 <= excluded.last_transaction_version_v1"), ) } @@ -304,9 +304,9 @@ fn insert_current_unified_fungible_asset_balances_v2_query( impl QueryFragment + diesel::query_builder::QueryId + Send, Option<&'static str>, ) { - use schema::current_unified_fungible_asset_balances_to_be_renamed::dsl::*; + use schema::current_fungible_asset_balances::dsl::*; ( - diesel::insert_into(schema::current_unified_fungible_asset_balances_to_be_renamed::table) + diesel::insert_into(schema::current_fungible_asset_balances::table) .values(items_to_insert) .on_conflict(storage_id) .do_update() @@ -322,8 +322,8 @@ fn insert_current_unified_fungible_asset_balances_v2_query( inserted_at.eq(excluded(inserted_at)), ) ), - Some(" WHERE current_unified_fungible_asset_balances_to_be_renamed.last_transaction_version_v2 IS NULL \ - OR current_unified_fungible_asset_balances_to_be_renamed.last_transaction_version_v2 <= excluded.last_transaction_version_v2 "), + Some(" WHERE current_fungible_asset_balances.last_transaction_version_v2 IS NULL \ + OR current_fungible_asset_balances.last_transaction_version_v2 <= excluded.last_transaction_version_v2 "), ) }