From 4d0007ee87d78730cc70f7d3f50118510ee1198e Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 21 May 2020 15:12:08 +0300 Subject: [PATCH] Reduce DB migrations --- .../lib/explorer/chain/staking_pool.ex | 85 ++++++++++----- .../explorer/chain/staking_pools_delegator.ex | 42 +++++--- .../20190521104412_create_staking_pools.exs | 4 +- ...112839_create_staking_pools_delegators.exs | 8 +- ...0190605125829_delegators_deleted_colum.exs | 10 -- ..._add_validator_reward_percent_to_pools.exs | 9 -- ...20_add_reward_ratio_to_pool_delegators.exs | 9 -- ...0190807111216_remove_duplicate_indexes.exs | 4 +- ...12841_add_unremovable_to_staking_pools.exs | 9 -- ...additional_ban_fields_to_staking_pools.exs | 11 -- ...d_snapshotting_fields_to_staking_pools.exs | 12 --- ...snapshotting_fields_to_pool_delegators.exs | 10 -- ...add_index_for_snapshotted_stake_amount.exs | 12 --- ...20200521090250_recreate_staking_tables.exs | 101 ++++++++++++++++++ 14 files changed, 194 insertions(+), 132 deletions(-) delete mode 100644 apps/explorer/priv/repo/migrations/20190605125829_delegators_deleted_colum.exs delete mode 100644 apps/explorer/priv/repo/migrations/20190718175620_add_validator_reward_percent_to_pools.exs delete mode 100644 apps/explorer/priv/repo/migrations/20190731172720_add_reward_ratio_to_pool_delegators.exs delete mode 100644 apps/explorer/priv/repo/migrations/20190820212841_add_unremovable_to_staking_pools.exs delete mode 100644 apps/explorer/priv/repo/migrations/20190904170014_add_additional_ban_fields_to_staking_pools.exs delete mode 100644 apps/explorer/priv/repo/migrations/20191024135401_add_snapshotting_fields_to_staking_pools.exs delete mode 100644 apps/explorer/priv/repo/migrations/20191101133819_add_snapshotting_fields_to_pool_delegators.exs delete mode 100644 apps/explorer/priv/repo/migrations/20191128144250_add_index_for_snapshotted_stake_amount.exs create mode 100644 apps/explorer/priv/repo/migrations/20200521090250_recreate_staking_tables.exs diff --git a/apps/explorer/lib/explorer/chain/staking_pool.ex b/apps/explorer/lib/explorer/chain/staking_pool.ex index af91c6c4d5..48b1cc4328 100644 --- a/apps/explorer/lib/explorer/chain/staking_pool.ex +++ b/apps/explorer/lib/explorer/chain/staking_pool.ex @@ -13,65 +13,92 @@ defmodule Explorer.Chain.StakingPool do } @type t :: %__MODULE__{ - staking_address_hash: Hash.Address.t(), - mining_address_hash: Hash.Address.t(), - banned_until: integer, + are_delegators_banned: boolean, banned_delegators_until: integer, + banned_until: integer, + ban_reason: String.t(), delegators_count: integer, is_active: boolean, is_banned: boolean, - is_validator: boolean, + is_deleted: boolean, is_unremovable: boolean, - are_delegators_banned: boolean, + is_validator: boolean, likelihood: Decimal.t(), - validator_reward_percent: Decimal.t(), - stakes_ratio: Decimal.t(), - validator_reward_ratio: Decimal.t(), - snapshotted_validator_reward_ratio: Decimal.t(), + mining_address_hash: Hash.Address.t(), self_staked_amount: Decimal.t(), - total_staked_amount: Decimal.t(), snapshotted_self_staked_amount: Decimal.t(), snapshotted_total_staked_amount: Decimal.t(), - ban_reason: String.t(), + snapshotted_validator_reward_ratio: Decimal.t(), + stakes_ratio: Decimal.t(), + staking_address_hash: Hash.Address.t(), + total_staked_amount: Decimal.t(), + validator_reward_percent: Decimal.t(), + validator_reward_ratio: Decimal.t(), was_banned_count: integer, - was_validator_count: integer, - is_deleted: boolean + was_validator_count: integer } @attrs ~w( - is_active delegators_count total_staked_amount self_staked_amount snapshotted_total_staked_amount snapshotted_self_staked_amount is_validator - was_validator_count is_banned are_delegators_banned ban_reason was_banned_count banned_until banned_delegators_until likelihood - stakes_ratio validator_reward_ratio snapshotted_validator_reward_ratio staking_address_hash mining_address_hash validator_reward_percent + are_delegators_banned + banned_delegators_until + banned_until + ban_reason + delegators_count + is_active + is_banned is_unremovable + is_validator + likelihood + mining_address_hash + self_staked_amount + snapshotted_self_staked_amount + snapshotted_total_staked_amount + snapshotted_validator_reward_ratio + stakes_ratio + staking_address_hash + total_staked_amount + validator_reward_percent + validator_reward_ratio + was_banned_count + was_validator_count )a @req_attrs ~w( - is_active delegators_count total_staked_amount self_staked_amount is_validator - was_validator_count is_banned was_banned_count banned_until - staking_address_hash mining_address_hash is_unremovable + banned_until + delegators_count + is_active + is_banned + is_unremovable + is_validator + mining_address_hash + self_staked_amount + staking_address_hash + total_staked_amount + was_banned_count + was_validator_count )a schema "staking_pools" do - field(:banned_until, :integer) + field(:are_delegators_banned, :boolean, default: false) field(:banned_delegators_until, :integer) + field(:banned_until, :integer) + field(:ban_reason, :string) field(:delegators_count, :integer) field(:is_active, :boolean, default: false) field(:is_banned, :boolean, default: false) - field(:is_validator, :boolean, default: false) + field(:is_deleted, :boolean, default: false) field(:is_unremovable, :boolean, default: false) - field(:are_delegators_banned, :boolean, default: false) + field(:is_validator, :boolean, default: false) field(:likelihood, :decimal) - field(:validator_reward_percent, :decimal) - field(:stakes_ratio, :decimal) - field(:validator_reward_ratio, :decimal) - field(:snapshotted_validator_reward_ratio, :decimal) field(:self_staked_amount, :decimal) - field(:total_staked_amount, :decimal) + field(:stakes_ratio, :decimal) field(:snapshotted_self_staked_amount, :decimal) field(:snapshotted_total_staked_amount, :decimal) - field(:ban_reason, :string) + field(:snapshotted_validator_reward_ratio, :decimal) + field(:total_staked_amount, :decimal) + field(:validator_reward_percent, :decimal) + field(:validator_reward_ratio, :decimal) field(:was_banned_count, :integer) field(:was_validator_count, :integer) - field(:is_deleted, :boolean, default: false) has_many(:delegators, StakingPoolsDelegator, foreign_key: :staking_address_hash) belongs_to( diff --git a/apps/explorer/lib/explorer/chain/staking_pools_delegator.ex b/apps/explorer/lib/explorer/chain/staking_pools_delegator.ex index fee81afa61..77c523c98a 100644 --- a/apps/explorer/lib/explorer/chain/staking_pools_delegator.ex +++ b/apps/explorer/lib/explorer/chain/staking_pools_delegator.ex @@ -13,42 +13,56 @@ defmodule Explorer.Chain.StakingPoolsDelegator do } @type t :: %__MODULE__{ - staking_address_hash: Hash.Address.t(), address_hash: Hash.Address.t(), + is_active: boolean(), + is_deleted: boolean(), max_ordered_withdraw_allowed: Decimal.t(), max_withdraw_allowed: Decimal.t(), ordered_withdraw: Decimal.t(), ordered_withdraw_epoch: integer(), - stake_amount: Decimal.t(), - snapshotted_stake_amount: Decimal.t(), reward_ratio: Decimal.t(), snapshotted_reward_ratio: Decimal.t(), - is_active: boolean(), - is_deleted: boolean() + snapshotted_stake_amount: Decimal.t(), + stake_amount: Decimal.t(), + staking_address_hash: Hash.Address.t() } @attrs ~w( - staking_address_hash address_hash max_ordered_withdraw_allowed - max_withdraw_allowed ordered_withdraw stake_amount snapshotted_stake_amount ordered_withdraw_epoch - reward_ratio snapshotted_reward_ratio is_active is_deleted + address_hash + is_active + is_deleted + max_ordered_withdraw_allowed + max_withdraw_allowed + ordered_withdraw + ordered_withdraw_epoch + reward_ratio + snapshotted_reward_ratio + snapshotted_stake_amount + stake_amount + staking_address_hash )a @req_attrs ~w( - staking_address_hash address_hash max_ordered_withdraw_allowed - max_withdraw_allowed ordered_withdraw stake_amount ordered_withdraw_epoch + address_hash + max_ordered_withdraw_allowed + max_withdraw_allowed + ordered_withdraw + ordered_withdraw_epoch + stake_amount + staking_address_hash )a schema "staking_pools_delegators" do + field(:is_active, :boolean, default: true) + field(:is_deleted, :boolean, default: false) field(:max_ordered_withdraw_allowed, :decimal) field(:max_withdraw_allowed, :decimal) field(:ordered_withdraw, :decimal) field(:ordered_withdraw_epoch, :integer) - field(:stake_amount, :decimal) - field(:snapshotted_stake_amount, :decimal) field(:reward_ratio, :decimal) field(:snapshotted_reward_ratio, :decimal) - field(:is_active, :boolean, default: true) - field(:is_deleted, :boolean, default: false) + field(:snapshotted_stake_amount, :decimal) + field(:stake_amount, :decimal) belongs_to( :staking_pool, diff --git a/apps/explorer/priv/repo/migrations/20190521104412_create_staking_pools.exs b/apps/explorer/priv/repo/migrations/20190521104412_create_staking_pools.exs index ce78745053..94483112f2 100644 --- a/apps/explorer/priv/repo/migrations/20190521104412_create_staking_pools.exs +++ b/apps/explorer/priv/repo/migrations/20190521104412_create_staking_pools.exs @@ -6,7 +6,7 @@ defmodule Explorer.Repo.Migrations.CreateStakingPools do add(:is_active, :boolean, default: false, null: false) add(:is_deleted, :boolean, default: false, null: false) add(:delegators_count, :integer) - add(:total_staked_amount, :numeric, precision: 100) + add(:staked_amount, :numeric, precision: 100) add(:self_staked_amount, :numeric, precision: 100) add(:is_validator, :boolean, default: false, null: false) add(:was_validator_count, :integer) @@ -14,7 +14,7 @@ defmodule Explorer.Repo.Migrations.CreateStakingPools do add(:was_banned_count, :integer) add(:banned_until, :bigint) add(:likelihood, :decimal, precision: 5, scale: 2) - add(:stakes_ratio, :decimal, precision: 5, scale: 2) + add(:staked_ratio, :decimal, precision: 5, scale: 2) add(:staking_address_hash, :bytea) add(:mining_address_hash, :bytea) diff --git a/apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs b/apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs index 68cc3adbec..ccf9c8c372 100644 --- a/apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs +++ b/apps/explorer/priv/repo/migrations/20190523112839_create_staking_pools_delegators.exs @@ -3,8 +3,8 @@ defmodule Explorer.Repo.Migrations.CreateStakingPoolsDelegator do def change do create table(:staking_pools_delegators) do - add(:address_hash, :bytea) - add(:staking_address_hash, :bytea) + add(:delegator_address_hash, :bytea) + add(:pool_address_hash, :bytea) add(:stake_amount, :numeric, precision: 100) add(:ordered_withdraw, :numeric, precision: 100) add(:max_withdraw_allowed, :numeric, precision: 100) @@ -14,10 +14,10 @@ defmodule Explorer.Repo.Migrations.CreateStakingPoolsDelegator do timestamps(null: false, type: :utc_datetime_usec) end - create(index(:staking_pools_delegators, [:address_hash])) + create(index(:staking_pools_delegators, [:delegator_address_hash])) create( - index(:staking_pools_delegators, [:address_hash, :staking_address_hash], + index(:staking_pools_delegators, [:delegator_address_hash, :pool_address_hash], unique: true, name: :pools_delegator_index ) diff --git a/apps/explorer/priv/repo/migrations/20190605125829_delegators_deleted_colum.exs b/apps/explorer/priv/repo/migrations/20190605125829_delegators_deleted_colum.exs deleted file mode 100644 index 36d0b8e837..0000000000 --- a/apps/explorer/priv/repo/migrations/20190605125829_delegators_deleted_colum.exs +++ /dev/null @@ -1,10 +0,0 @@ -defmodule Explorer.Repo.Migrations.DelegatorsDeletedColum do - use Ecto.Migration - - def change do - alter table(:staking_pools_delegators) do - add(:is_active, :boolean, default: true) - add(:is_deleted, :boolean, default: false) - end - end -end diff --git a/apps/explorer/priv/repo/migrations/20190718175620_add_validator_reward_percent_to_pools.exs b/apps/explorer/priv/repo/migrations/20190718175620_add_validator_reward_percent_to_pools.exs deleted file mode 100644 index 6a8e698e23..0000000000 --- a/apps/explorer/priv/repo/migrations/20190718175620_add_validator_reward_percent_to_pools.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Explorer.Repo.Migrations.AddValidatorRewardPercentToPools do - use Ecto.Migration - - def change do - alter table(:staking_pools) do - add(:validator_reward_percent, :decimal, precision: 5, scale: 2) - end - end -end diff --git a/apps/explorer/priv/repo/migrations/20190731172720_add_reward_ratio_to_pool_delegators.exs b/apps/explorer/priv/repo/migrations/20190731172720_add_reward_ratio_to_pool_delegators.exs deleted file mode 100644 index 132e12dc26..0000000000 --- a/apps/explorer/priv/repo/migrations/20190731172720_add_reward_ratio_to_pool_delegators.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Explorer.Repo.Migrations.AddBlockRewardToPoolDelegators do - use Ecto.Migration - - def change do - alter table(:staking_pools_delegators) do - add(:reward_ratio, :decimal, precision: 5, scale: 2) - end - end -end diff --git a/apps/explorer/priv/repo/migrations/20190807111216_remove_duplicate_indexes.exs b/apps/explorer/priv/repo/migrations/20190807111216_remove_duplicate_indexes.exs index 45d873e1de..ae07c8b1e5 100644 --- a/apps/explorer/priv/repo/migrations/20190807111216_remove_duplicate_indexes.exs +++ b/apps/explorer/priv/repo/migrations/20190807111216_remove_duplicate_indexes.exs @@ -7,7 +7,9 @@ defmodule Explorer.Repo.Migrations.RemoveDuplicateIndexes do ) drop_if_exists( - index(:staking_pools_delegators, [:address_hash], name: "staking_pools_delegators_address_hash_index") + index(:staking_pools_delegators, [:delegator_address_hash], + name: "staking_pools_delegators_delegator_address_hash_index" + ) ) drop_if_exists(index(:transactions, [:to_address_hash], name: "transactions_to_address_hash_index")) diff --git a/apps/explorer/priv/repo/migrations/20190820212841_add_unremovable_to_staking_pools.exs b/apps/explorer/priv/repo/migrations/20190820212841_add_unremovable_to_staking_pools.exs deleted file mode 100644 index a7d82d31b6..0000000000 --- a/apps/explorer/priv/repo/migrations/20190820212841_add_unremovable_to_staking_pools.exs +++ /dev/null @@ -1,9 +0,0 @@ -defmodule Explorer.Repo.Migrations.AddUnremovableToStakingPools do - use Ecto.Migration - - def change do - alter table(:staking_pools) do - add(:is_unremovable, :boolean, default: false, null: false) - end - end -end diff --git a/apps/explorer/priv/repo/migrations/20190904170014_add_additional_ban_fields_to_staking_pools.exs b/apps/explorer/priv/repo/migrations/20190904170014_add_additional_ban_fields_to_staking_pools.exs deleted file mode 100644 index b0a6d1b929..0000000000 --- a/apps/explorer/priv/repo/migrations/20190904170014_add_additional_ban_fields_to_staking_pools.exs +++ /dev/null @@ -1,11 +0,0 @@ -defmodule Explorer.Repo.Migrations.AddAdditionalBanFieldsToStakingPools do - use Ecto.Migration - - def change do - alter table(:staking_pools) do - add(:are_delegators_banned, :boolean, default: false) - add(:ban_reason, :string) - add(:banned_delegators_until, :bigint) - end - end -end diff --git a/apps/explorer/priv/repo/migrations/20191024135401_add_snapshotting_fields_to_staking_pools.exs b/apps/explorer/priv/repo/migrations/20191024135401_add_snapshotting_fields_to_staking_pools.exs deleted file mode 100644 index ecc44951f5..0000000000 --- a/apps/explorer/priv/repo/migrations/20191024135401_add_snapshotting_fields_to_staking_pools.exs +++ /dev/null @@ -1,12 +0,0 @@ -defmodule Explorer.Repo.Migrations.AddSnapshottingFieldsToStakingPools do - use Ecto.Migration - - def change do - alter table(:staking_pools) do - add(:snapshotted_total_staked_amount, :numeric, precision: 100) - add(:snapshotted_self_staked_amount, :numeric, precision: 100) - add(:validator_reward_ratio, :decimal, precision: 5, scale: 2) - add(:snapshotted_validator_reward_ratio, :decimal, precision: 5, scale: 2) - end - end -end diff --git a/apps/explorer/priv/repo/migrations/20191101133819_add_snapshotting_fields_to_pool_delegators.exs b/apps/explorer/priv/repo/migrations/20191101133819_add_snapshotting_fields_to_pool_delegators.exs deleted file mode 100644 index 14f3997cfe..0000000000 --- a/apps/explorer/priv/repo/migrations/20191101133819_add_snapshotting_fields_to_pool_delegators.exs +++ /dev/null @@ -1,10 +0,0 @@ -defmodule Explorer.Repo.Migrations.AddSnapshottingFieldsToPoolDelegators do - use Ecto.Migration - - def change do - alter table(:staking_pools_delegators) do - add(:snapshotted_stake_amount, :numeric, precision: 100) - add(:snapshotted_reward_ratio, :decimal, precision: 5, scale: 2) - end - end -end diff --git a/apps/explorer/priv/repo/migrations/20191128144250_add_index_for_snapshotted_stake_amount.exs b/apps/explorer/priv/repo/migrations/20191128144250_add_index_for_snapshotted_stake_amount.exs deleted file mode 100644 index e3b5432f9f..0000000000 --- a/apps/explorer/priv/repo/migrations/20191128144250_add_index_for_snapshotted_stake_amount.exs +++ /dev/null @@ -1,12 +0,0 @@ -defmodule Explorer.Repo.Migrations.AddIndexForSnapshottedStakeAmount do - use Ecto.Migration - - def change do - create( - index(:staking_pools_delegators, [:staking_address_hash, :snapshotted_stake_amount, :is_active], - unique: false, - name: :snapshotted_stake_amount_index - ) - ) - end -end diff --git a/apps/explorer/priv/repo/migrations/20200521090250_recreate_staking_tables.exs b/apps/explorer/priv/repo/migrations/20200521090250_recreate_staking_tables.exs new file mode 100644 index 0000000000..cb6fb80fc4 --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20200521090250_recreate_staking_tables.exs @@ -0,0 +1,101 @@ +defmodule Explorer.Repo.Migrations.RecreateStakingTables do + use Ecto.Migration + + def change do + # rename table(:staking_pools), :staked_amount, to: :total_staked_amount + # rename table(:staking_pools), :staked_ratio, to: :stakes_ratio + # alter table(:staking_pools) do + # add(:validator_reward_percent, :decimal, precision: 5, scale: 2) + # add(:is_unremovable, :boolean, default: false, null: false) + # add(:are_delegators_banned, :boolean, default: false) + # add(:ban_reason, :string) + # add(:banned_delegators_until, :bigint) + # add(:snapshotted_total_staked_amount, :numeric, precision: 100) + # add(:snapshotted_self_staked_amount, :numeric, precision: 100) + # add(:validator_reward_ratio, :decimal, precision: 5, scale: 2) + # add(:snapshotted_validator_reward_ratio, :decimal, precision: 5, scale: 2) + # end + + # rename table(:staking_pools_delegators), :delegator_address_hash, to: :address_hash + # rename table(:staking_pools_delegators), :pool_address_hash, to: :staking_address_hash + # alter table(:staking_pools_delegators) do + # add(:is_active, :boolean, default: true) + # add(:is_deleted, :boolean, default: false) + # add(:reward_ratio, :decimal, precision: 5, scale: 2) + # add(:snapshotted_stake_amount, :numeric, precision: 100) + # add(:snapshotted_reward_ratio, :decimal, precision: 5, scale: 2) + # end + + # create( + # index(:staking_pools_delegators, [:staking_address_hash, :snapshotted_stake_amount, :is_active], + # unique: false, + # name: :snapshotted_stake_amount_index + # ) + # ) + + drop_if_exists(table(:staking_pools)) + drop_if_exists(table(:staking_pools_delegators)) + + create table(:staking_pools) do + add(:are_delegators_banned, :boolean, default: false) + add(:banned_delegators_until, :bigint) + add(:banned_until, :bigint) + add(:ban_reason, :string) + add(:delegators_count, :integer) + add(:is_active, :boolean, default: false, null: false) + add(:is_banned, :boolean, default: false, null: false) + add(:is_deleted, :boolean, default: false, null: false) + add(:is_unremovable, :boolean, default: false, null: false) + add(:is_validator, :boolean, default: false, null: false) + add(:likelihood, :decimal, precision: 5, scale: 2) + add(:mining_address_hash, :bytea) + add(:self_staked_amount, :numeric, precision: 100) + add(:snapshotted_self_staked_amount, :numeric, precision: 100) + add(:snapshotted_total_staked_amount, :numeric, precision: 100) + add(:snapshotted_validator_reward_ratio, :decimal, precision: 5, scale: 2) + add(:stakes_ratio, :decimal, precision: 5, scale: 2) + add(:staking_address_hash, :bytea) + add(:total_staked_amount, :numeric, precision: 100) + add(:validator_reward_percent, :decimal, precision: 5, scale: 2) + add(:validator_reward_ratio, :decimal, precision: 5, scale: 2) + add(:was_banned_count, :integer) + add(:was_validator_count, :integer) + + timestamps(null: false, type: :utc_datetime_usec) + end + + create(index(:staking_pools, [:staking_address_hash], unique: true)) + create(index(:staking_pools, [:mining_address_hash])) + + create table(:staking_pools_delegators) do + add(:address_hash, :bytea) + add(:is_active, :boolean, default: true) + add(:is_deleted, :boolean, default: false) + add(:max_ordered_withdraw_allowed, :numeric, precision: 100) + add(:max_withdraw_allowed, :numeric, precision: 100) + add(:ordered_withdraw, :numeric, precision: 100) + add(:ordered_withdraw_epoch, :integer) + add(:reward_ratio, :decimal, precision: 5, scale: 2) + add(:snapshotted_reward_ratio, :decimal, precision: 5, scale: 2) + add(:snapshotted_stake_amount, :numeric, precision: 100) + add(:stake_amount, :numeric, precision: 100) + add(:staking_address_hash, :bytea) + + timestamps(null: false, type: :utc_datetime_usec) + end + + create( + index(:staking_pools_delegators, [:address_hash, :staking_address_hash], + unique: true, + name: :pools_delegator_index + ) + ) + + create( + index(:staking_pools_delegators, [:staking_address_hash, :snapshotted_stake_amount, :is_active], + unique: false, + name: :snapshotted_stake_amount_index + ) + ) + end +end