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

bugfix: fixed migration. Added migration saving to be more clear #434

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2065,7 +2065,7 @@ pub fn input_network_name(
network_config
.linkdrop_account_id
.as_ref()
.map_or(false, |linkdrop_account_id| {
.is_some_and(|linkdrop_account_id| {
account_ids.iter().any(|account_id| {
account_id.as_str().ends_with(linkdrop_account_id.as_str())
})
Expand Down
14 changes: 12 additions & 2 deletions src/config/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl From<ConfigV2> for ConfigV3 {
.network_connection
.into_iter()
.map(|(network_name, mut network_config)| {
if network_name == "testnet" && network_config.faucet_url.is_none() {
if network_name == "testnet" && network_config.fastnear_url.is_none() {
network_config.fastnear_url =
Some("https://test.api.fastnear.com/".parse().unwrap());
}
Expand Down Expand Up @@ -112,7 +112,7 @@ impl From<NetworkConfigV1> for NetworkConfigV2 {
}
}

#[derive(serde::Serialize, serde::Deserialize)]
#[derive(serde::Serialize, serde::Deserialize, Debug)]
#[serde(tag = "version")]
pub enum ConfigVersion {
#[serde(rename = "1")]
Expand All @@ -123,3 +123,13 @@ pub enum ConfigVersion {
#[serde(rename = "3")]
V3(ConfigV3),
}

impl ConfigVersion {
pub fn is_latest_version(&self) -> bool {
// Used match instead of matches! to compile fail if new version is added
match self {
ConfigVersion::V3(_) => true,
ConfigVersion::V2(_) | ConfigVersion::V1(_) => false,
}
}
}
13 changes: 11 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl Default for Config {
near_social_db_contract_account_id: Some("v1.social08.testnet".parse().unwrap()),
faucet_url: Some("https://helper.nearprotocol.com/account".parse().unwrap()),
meta_transaction_relayer_url: None,
fastnear_url: None,
fastnear_url: Some("https://test.api.fastnear.com/".parse().unwrap()),
staking_pools_factory_account_id: Some("pool.f863973.m0".parse().unwrap()),
coingecko_url: None,
},
Expand Down Expand Up @@ -99,7 +99,14 @@ impl Config {
}
})?;

Ok(config_version.into())
let is_latest_version = config_version.is_latest_version();
let config: Config = config_version.into();

if !is_latest_version {
Self::write_config_toml(config.clone())?;
}

Ok(config)
} else {
Ok(crate::config::Config::default())
}
Expand Down Expand Up @@ -190,9 +197,11 @@ impl From<migrations::ConfigVersion> for Config {
loop {
config_version = match config_version {
migrations::ConfigVersion::V1(config_v1) => {
eprintln!("Migrating config.toml from V1 to V2...");
migrations::ConfigVersion::V2(config_v1.into())
}
migrations::ConfigVersion::V2(config_v2) => {
eprintln!("Migrating config.toml from V2 to V3...");
migrations::ConfigVersion::V3(config_v2.into())
}
migrations::ConfigVersion::V3(config_v3) => {
Expand Down
Loading