Skip to content

Commit b04bc68

Browse files
authored
[2/2][FA migration] Complete fa migration and rename table (#494)
* complete fa migration and rename table * add index back * set fields to nonnull
1 parent fc65c59 commit b04bc68

File tree

5 files changed

+68
-40
lines changed

5 files changed

+68
-40
lines changed

rust/processor/src/db/common/models/fungible_asset_models/v2_fungible_asset_balances.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use crate::{
1515
token_v2_models::v2_token_utils::{TokenStandard, V2_STANDARD},
1616
},
1717
schema::{
18-
current_fungible_asset_balances, current_unified_fungible_asset_balances_to_be_renamed,
18+
current_fungible_asset_balances, current_fungible_asset_balances_legacy,
1919
fungible_asset_balances,
2020
},
2121
utils::util::{
@@ -53,7 +53,7 @@ pub struct FungibleAssetBalance {
5353

5454
#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize)]
5555
#[diesel(primary_key(storage_id))]
56-
#[diesel(table_name = current_fungible_asset_balances)]
56+
#[diesel(table_name = current_fungible_asset_balances_legacy)]
5757
pub struct CurrentFungibleAssetBalance {
5858
pub storage_id: String,
5959
pub owner_address: String,
@@ -66,9 +66,11 @@ pub struct CurrentFungibleAssetBalance {
6666
pub token_standard: String,
6767
}
6868

69+
/// Note that this used to be called current_unified_fungible_asset_balances_to_be_renamed
70+
/// and was renamed to current_fungible_asset_balances to facilitate migration
6971
#[derive(Clone, Debug, Deserialize, FieldCount, Identifiable, Insertable, Serialize, Default)]
7072
#[diesel(primary_key(storage_id))]
71-
#[diesel(table_name = current_unified_fungible_asset_balances_to_be_renamed)]
73+
#[diesel(table_name = current_fungible_asset_balances)]
7274
#[diesel(treat_none_as_null = true)]
7375
pub struct CurrentUnifiedFungibleAssetBalance {
7476
pub storage_id: String,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- This file should undo anything in `up.sql`
2+
ALTER TABLE current_fungible_asset_balances
3+
RENAME TO current_unified_fungible_asset_balances_to_be_renamed;
4+
ALTER TABLE current_fungible_asset_balances_legacy
5+
RENAME TO current_fungible_asset_balances;
6+
ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed DROP COLUMN IF EXISTS asset_type;
7+
ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed DROP COLUMN IF EXISTS token_standard;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- Your SQL goes here
2+
ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed
3+
ADD COLUMN IF NOT EXISTS asset_type VARCHAR(1000) NOT NULL GENERATED ALWAYS AS (COALESCE(asset_type_v1, asset_type_v2)) STORED;
4+
CREATE INDEX IF NOT EXISTS cufab_owner_at_index ON current_unified_fungible_asset_balances_to_be_renamed (owner_address, asset_type);
5+
ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed
6+
ADD COLUMN IF NOT EXISTS token_standard VARCHAR(10) NOT NULL GENERATED ALWAYS AS (
7+
CASE
8+
WHEN asset_type_v1 IS NOT NULL THEN 'v1'
9+
ELSE 'v2'
10+
END
11+
) STORED;
12+
ALTER TABLE current_fungible_asset_balances
13+
RENAME TO current_fungible_asset_balances_legacy;
14+
ALTER TABLE current_unified_fungible_asset_balances_to_be_renamed
15+
RENAME TO current_fungible_asset_balances;

rust/processor/src/db/postgres/schema.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,35 @@ diesel::table! {
423423

424424
diesel::table! {
425425
current_fungible_asset_balances (storage_id) {
426+
#[max_length = 66]
427+
storage_id -> Varchar,
428+
#[max_length = 66]
429+
owner_address -> Varchar,
430+
#[max_length = 66]
431+
asset_type_v2 -> Nullable<Varchar>,
432+
#[max_length = 1000]
433+
asset_type_v1 -> Nullable<Varchar>,
434+
is_primary -> Nullable<Bool>,
435+
is_frozen -> Bool,
436+
amount_v1 -> Nullable<Numeric>,
437+
amount_v2 -> Nullable<Numeric>,
438+
amount -> Nullable<Numeric>,
439+
last_transaction_version_v1 -> Nullable<Int8>,
440+
last_transaction_version_v2 -> Nullable<Int8>,
441+
last_transaction_version -> Nullable<Int8>,
442+
last_transaction_timestamp_v1 -> Nullable<Timestamp>,
443+
last_transaction_timestamp_v2 -> Nullable<Timestamp>,
444+
last_transaction_timestamp -> Nullable<Timestamp>,
445+
inserted_at -> Timestamp,
446+
#[max_length = 1000]
447+
asset_type -> Varchar,
448+
#[max_length = 10]
449+
token_standard -> Varchar,
450+
}
451+
}
452+
453+
diesel::table! {
454+
current_fungible_asset_balances_legacy (storage_id) {
426455
#[max_length = 66]
427456
storage_id -> Varchar,
428457
#[max_length = 66]
@@ -651,31 +680,6 @@ diesel::table! {
651680
}
652681
}
653682

654-
diesel::table! {
655-
current_unified_fungible_asset_balances_to_be_renamed (storage_id) {
656-
#[max_length = 66]
657-
storage_id -> Varchar,
658-
#[max_length = 66]
659-
owner_address -> Varchar,
660-
#[max_length = 66]
661-
asset_type_v2 -> Nullable<Varchar>,
662-
#[max_length = 1000]
663-
asset_type_v1 -> Nullable<Varchar>,
664-
is_primary -> Nullable<Bool>,
665-
is_frozen -> Bool,
666-
amount_v1 -> Nullable<Numeric>,
667-
amount_v2 -> Nullable<Numeric>,
668-
amount -> Nullable<Numeric>,
669-
last_transaction_version_v1 -> Nullable<Int8>,
670-
last_transaction_version_v2 -> Nullable<Int8>,
671-
last_transaction_version -> Nullable<Int8>,
672-
last_transaction_timestamp_v1 -> Nullable<Timestamp>,
673-
last_transaction_timestamp_v2 -> Nullable<Timestamp>,
674-
last_transaction_timestamp -> Nullable<Timestamp>,
675-
inserted_at -> Timestamp,
676-
}
677-
}
678-
679683
diesel::table! {
680684
delegated_staking_activities (transaction_version, event_index) {
681685
transaction_version -> Int8,
@@ -1300,6 +1304,7 @@ diesel::allow_tables_to_appear_in_same_query!(
13001304
current_delegated_voter,
13011305
current_delegator_balances,
13021306
current_fungible_asset_balances,
1307+
current_fungible_asset_balances_legacy,
13031308
current_objects,
13041309
current_staking_pool_voter,
13051310
current_table_items,
@@ -1310,7 +1315,6 @@ diesel::allow_tables_to_appear_in_same_query!(
13101315
current_token_pending_claims,
13111316
current_token_royalty_v1,
13121317
current_token_v2_metadata,
1313-
current_unified_fungible_asset_balances_to_be_renamed,
13141318
delegated_staking_activities,
13151319
delegated_staking_pool_balances,
13161320
delegated_staking_pools,

rust/processor/src/processors/fungible_asset_processor.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -245,10 +245,10 @@ fn insert_current_fungible_asset_balances_query(
245245
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
246246
Option<&'static str>,
247247
) {
248-
use schema::current_fungible_asset_balances::dsl::*;
248+
use schema::current_fungible_asset_balances_legacy::dsl::*;
249249

250250
(
251-
diesel::insert_into(schema::current_fungible_asset_balances::table)
251+
diesel::insert_into(schema::current_fungible_asset_balances_legacy::table)
252252
.values(items_to_insert)
253253
.on_conflict(storage_id)
254254
.do_update()
@@ -265,7 +265,7 @@ fn insert_current_fungible_asset_balances_query(
265265
inserted_at.eq(excluded(inserted_at)),
266266
)
267267
),
268-
Some(" WHERE current_fungible_asset_balances.last_transaction_version <= excluded.last_transaction_version "),
268+
Some(" WHERE current_fungible_asset_balances_legacy.last_transaction_version <= excluded.last_transaction_version "),
269269
)
270270
}
271271

@@ -275,10 +275,10 @@ fn insert_current_unified_fungible_asset_balances_v1_query(
275275
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
276276
Option<&'static str>,
277277
) {
278-
use schema::current_unified_fungible_asset_balances_to_be_renamed::dsl::*;
278+
use schema::current_fungible_asset_balances::dsl::*;
279279

280280
(
281-
diesel::insert_into(schema::current_unified_fungible_asset_balances_to_be_renamed::table)
281+
diesel::insert_into(schema::current_fungible_asset_balances::table)
282282
.values(items_to_insert)
283283
.on_conflict(storage_id)
284284
.do_update()
@@ -293,8 +293,8 @@ fn insert_current_unified_fungible_asset_balances_v1_query(
293293
inserted_at.eq(excluded(inserted_at)),
294294
)
295295
),
296-
Some(" WHERE current_unified_fungible_asset_balances_to_be_renamed.last_transaction_version_v1 IS NULL \
297-
OR current_unified_fungible_asset_balances_to_be_renamed.last_transaction_version_v1 <= excluded.last_transaction_version_v1"),
296+
Some(" WHERE current_fungible_asset_balances.last_transaction_version_v1 IS NULL \
297+
OR current_fungible_asset_balances.last_transaction_version_v1 <= excluded.last_transaction_version_v1"),
298298
)
299299
}
300300

@@ -304,9 +304,9 @@ fn insert_current_unified_fungible_asset_balances_v2_query(
304304
impl QueryFragment<Pg> + diesel::query_builder::QueryId + Send,
305305
Option<&'static str>,
306306
) {
307-
use schema::current_unified_fungible_asset_balances_to_be_renamed::dsl::*;
307+
use schema::current_fungible_asset_balances::dsl::*;
308308
(
309-
diesel::insert_into(schema::current_unified_fungible_asset_balances_to_be_renamed::table)
309+
diesel::insert_into(schema::current_fungible_asset_balances::table)
310310
.values(items_to_insert)
311311
.on_conflict(storage_id)
312312
.do_update()
@@ -322,8 +322,8 @@ fn insert_current_unified_fungible_asset_balances_v2_query(
322322
inserted_at.eq(excluded(inserted_at)),
323323
)
324324
),
325-
Some(" WHERE current_unified_fungible_asset_balances_to_be_renamed.last_transaction_version_v2 IS NULL \
326-
OR current_unified_fungible_asset_balances_to_be_renamed.last_transaction_version_v2 <= excluded.last_transaction_version_v2 "),
325+
Some(" WHERE current_fungible_asset_balances.last_transaction_version_v2 IS NULL \
326+
OR current_fungible_asset_balances.last_transaction_version_v2 <= excluded.last_transaction_version_v2 "),
327327
)
328328
}
329329

0 commit comments

Comments
 (0)