Skip to content

Commit

Permalink
Modify current_unified_fungible_asset_balances for smoother migration…
Browse files Browse the repository at this point in the history
… later (#406)

* modify current unified fungible asset balances for migration

* fix typo
  • Loading branch information
bowenyang007 authored Jun 13, 2024
1 parent f7808ad commit 88b0dbe
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
current_fungible_asset_balances, current_unified_fungible_asset_balances_to_be_renamed,
fungible_asset_balances,
},
utils::util::{
Expand Down Expand Up @@ -68,14 +68,14 @@ pub struct CurrentFungibleAssetBalance {

#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize, Default)]
#[diesel(primary_key(storage_id))]
#[diesel(table_name = current_unified_fungible_asset_balances)]
#[diesel(table_name = current_unified_fungible_asset_balances_to_be_renamed)]
#[diesel(treat_none_as_null = true)]
pub struct CurrentUnifiedFungibleAssetBalance {
pub storage_id: String,
pub owner_address: String,
// metadata address for (paired) Fungible Asset
pub asset_type: String,
pub coin_type: Option<String>,
pub asset_type_v1: Option<String>,
pub asset_type_v2: Option<String>,
pub is_primary: Option<bool>,
pub is_frozen: bool,
pub amount_v1: Option<BigDecimal>,
Expand Down Expand Up @@ -113,8 +113,8 @@ impl From<&CurrentFungibleAssetBalance> for CurrentUnifiedFungibleAssetBalance {
Self {
storage_id: cfab.storage_id.clone(),
owner_address: cfab.owner_address.clone(),
asset_type: cfab.asset_type.clone(),
coin_type: None,
asset_type_v2: Some(cfab.asset_type.clone()),
asset_type_v1: None,
is_primary: Some(cfab.is_primary),
is_frozen: cfab.is_frozen,
amount_v1: None,
Expand All @@ -131,8 +131,8 @@ impl From<&CurrentFungibleAssetBalance> for CurrentUnifiedFungibleAssetBalance {
Self {
storage_id: pfs_addr,
owner_address: cfab.owner_address.clone(),
asset_type: metadata_addr,
coin_type: Some(cfab.asset_type.clone()),
asset_type_v2: None,
asset_type_v1: Some(cfab.asset_type.clone()),
is_primary: None,
is_frozen: cfab.is_frozen,
amount_v1: Some(cfab.amount.clone()),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- This file should undo anything in `up.sql`
ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed
RENAME TO current_unified_fungible_asset_balances;
DROP INDEX IF EXISTS cufab_owner_at_index;
ALTER TABLE current_unified_fungible_asset_balances DROP COLUMN asset_type;
ALTER TABLE current_unified_fungible_asset_balances
RENAME COLUMN asset_type_v2 TO asset_type;
ALTER TABLE current_unified_fungible_asset_balances
RENAME COLUMN asset_type_v1 TO coin_type;
ALTER TABLE current_unified_fungible_asset_balances
ALTER COLUMN asset_type
SET NOT NULL;
CREATE INDEX IF NOT EXISTS cufab_owner_at_index ON current_unified_fungible_asset_balances (owner_address, asset_type);
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Your SQL goes here
-- Rename asset_type and coin_type to v1 and v2, and make a generated asset_type to be v2 if exists, else v1.
DROP INDEX IF EXISTS cufab_owner_at_index;
ALTER TABLE current_unified_fungible_asset_balances
ALTER COLUMN asset_type DROP NOT NULL;
ALTER TABLE current_unified_fungible_asset_balances
RENAME COLUMN asset_type TO asset_type_v2;
ALTER TABLE current_unified_fungible_asset_balances
RENAME COLUMN coin_type TO asset_type_v1;
ALTER TABLE current_unified_fungible_asset_balances
ADD COLUMN asset_type VARCHAR(1000) GENERATED ALWAYS AS (COALESCE(asset_type_v2, asset_type_v1)) STORED;
CREATE INDEX IF NOT EXISTS cufab_owner_at_index ON current_unified_fungible_asset_balances (owner_address, asset_type);
-- Rename table to set expectation that we'll rename this table to current_fungible_asset_balances after testing
ALTER TABLE current_unified_fungible_asset_balances
RENAME TO current_unified_fungible_asset_balances_to_be_renamed;
19 changes: 6 additions & 13 deletions rust/processor/src/db/postgres/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -650,15 +650,15 @@ diesel::table! {
}

diesel::table! {
current_unified_fungible_asset_balances (storage_id) {
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 -> Varchar,
asset_type_v2 -> Nullable<Varchar>,
#[max_length = 1000]
coin_type -> Nullable<Varchar>,
asset_type_v1 -> Nullable<Varchar>,
is_primary -> Nullable<Bool>,
is_frozen -> Bool,
amount_v1 -> Nullable<Numeric>,
Expand All @@ -671,6 +671,8 @@ diesel::table! {
last_transaction_timestamp_v2 -> Nullable<Timestamp>,
last_transaction_timestamp -> Nullable<Timestamp>,
inserted_at -> Timestamp,
#[max_length = 1000]
asset_type -> Nullable<Varchar>,
}
}

Expand Down Expand Up @@ -1000,14 +1002,6 @@ diesel::table! {
}
}

diesel::table! {
test (token_data_id) {
#[max_length = 66]
token_data_id -> Varchar,
inserted_at -> Timestamp,
}
}

diesel::table! {
token_activities (transaction_version, event_account_address, event_creation_number, event_sequence_number) {
transaction_version -> Int8,
Expand Down Expand Up @@ -1316,7 +1310,7 @@ 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,
current_unified_fungible_asset_balances_to_be_renamed,
delegated_staking_activities,
delegated_staking_pool_balances,
delegated_staking_pools,
Expand All @@ -1338,7 +1332,6 @@ diesel::allow_tables_to_appear_in_same_query!(
spam_assets,
table_items,
table_metadatas,
test,
token_activities,
token_activities_v2,
token_datas,
Expand Down
19 changes: 10 additions & 9 deletions rust/processor/src/processors/fungible_asset_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,26 +266,26 @@ fn insert_current_unified_fungible_asset_balances_v1_query(
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Option<&'static str>,
) {
use schema::current_unified_fungible_asset_balances::dsl::*;
use schema::current_unified_fungible_asset_balances_to_be_renamed::dsl::*;

(
diesel::insert_into(schema::current_unified_fungible_asset_balances::table)
diesel::insert_into(schema::current_unified_fungible_asset_balances_to_be_renamed::table)
.values(items_to_insert)
.on_conflict(storage_id)
.do_update()
.set(
(
owner_address.eq(excluded(owner_address)),
asset_type.eq(excluded(asset_type)),
coin_type.eq(excluded(coin_type)),
asset_type_v1.eq(excluded(asset_type_v1)),
is_frozen.eq(excluded(is_frozen)),
amount_v1.eq(excluded(amount_v1)),
last_transaction_timestamp_v1.eq(excluded(last_transaction_timestamp_v1)),
last_transaction_version_v1.eq(excluded(last_transaction_version_v1)),
inserted_at.eq(excluded(inserted_at)),
)
),
Some(" WHERE current_unified_fungible_asset_balances.last_transaction_version_v1 IS NULL OR current_unified_fungible_asset_balances.last_transaction_version_v1 <= excluded.last_transaction_version_v1"),
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"),
)
}

Expand All @@ -295,16 +295,16 @@ fn insert_current_unified_fungible_asset_balances_v2_query(
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
Option<&'static str>,
) {
use schema::current_unified_fungible_asset_balances::dsl::*;
use schema::current_unified_fungible_asset_balances_to_be_renamed::dsl::*;
(
diesel::insert_into(schema::current_unified_fungible_asset_balances::table)
diesel::insert_into(schema::current_unified_fungible_asset_balances_to_be_renamed::table)
.values(items_to_insert)
.on_conflict(storage_id)
.do_update()
.set(
(
owner_address.eq(excluded(owner_address)),
asset_type.eq(excluded(asset_type)),
asset_type_v2.eq(excluded(asset_type_v2)),
is_primary.eq(excluded(is_primary)),
is_frozen.eq(excluded(is_frozen)),
amount_v2.eq(excluded(amount_v2)),
Expand All @@ -313,7 +313,8 @@ fn insert_current_unified_fungible_asset_balances_v2_query(
inserted_at.eq(excluded(inserted_at)),
)
),
Some(" WHERE current_unified_fungible_asset_balances.last_transaction_version_v2 IS NULL OR current_unified_fungible_asset_balances.last_transaction_version_v2 <= excluded.last_transaction_version_v2 "),
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 "),
)
}

Expand Down

0 comments on commit 88b0dbe

Please sign in to comment.