Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2/2][FA migration] Complete fa migration and rename table #494

Merged
merged 3 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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_to_be_renamed,
current_fungible_asset_balances, current_fungible_asset_balances_legacy,
fungible_asset_balances,
},
utils::util::{
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Original file line number Diff line number Diff line change
@@ -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) 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) 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;
56 changes: 30 additions & 26 deletions rust/processor/src/db/postgres/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Varchar>,
#[max_length = 1000]
asset_type_v1 -> Nullable<Varchar>,
is_primary -> Nullable<Bool>,
Copy link
Collaborator

@rtso rtso Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be nullable to match with the legacy schema

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Synced offline, this will be fixed in next PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't related to this PR. only popped up due to renaming table. We'll address it next PR.

is_frozen -> Bool,
amount_v1 -> Nullable<Numeric>,
amount_v2 -> Nullable<Numeric>,
amount -> Nullable<Numeric>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be nullable

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Synced offline, this will be fixed in next PR

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't related to this PR. only popped up due to renaming table. We'll address it next PR.

last_transaction_version_v1 -> Nullable<Int8>,
last_transaction_version_v2 -> Nullable<Int8>,
last_transaction_version -> Nullable<Int8>,
last_transaction_timestamp_v1 -> Nullable<Timestamp>,
last_transaction_timestamp_v2 -> Nullable<Timestamp>,
last_transaction_timestamp -> Nullable<Timestamp>,
inserted_at -> Timestamp,
#[max_length = 1000]
asset_type -> Nullable<Varchar>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be nullable

#[max_length = 10]
token_standard -> Nullable<Varchar>,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be nullable either

}
}

diesel::table! {
current_fungible_asset_balances_legacy (storage_id) {
#[max_length = 66]
storage_id -> Varchar,
#[max_length = 66]
Expand Down Expand Up @@ -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<Varchar>,
#[max_length = 1000]
asset_type_v1 -> Nullable<Varchar>,
is_primary -> Nullable<Bool>,
is_frozen -> Bool,
amount_v1 -> Nullable<Numeric>,
amount_v2 -> Nullable<Numeric>,
amount -> Nullable<Numeric>,
last_transaction_version_v1 -> Nullable<Int8>,
last_transaction_version_v2 -> Nullable<Int8>,
last_transaction_version -> Nullable<Int8>,
last_transaction_timestamp_v1 -> Nullable<Timestamp>,
last_transaction_timestamp_v2 -> Nullable<Timestamp>,
last_transaction_timestamp -> Nullable<Timestamp>,
inserted_at -> Timestamp,
}
}

diesel::table! {
delegated_staking_activities (transaction_version, event_index) {
transaction_version -> Int8,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
22 changes: 11 additions & 11 deletions rust/processor/src/processors/fungible_asset_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,10 +245,10 @@ fn insert_current_fungible_asset_balances_query(
impl QueryFragment<Pg> + 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()
Expand All @@ -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 "),
)
}

Expand All @@ -275,10 +275,10 @@ 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_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()
Expand All @@ -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"),
)
}

Expand All @@ -304,9 +304,9 @@ 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_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()
Expand All @@ -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 "),
)
}

Expand Down
Loading