mirror of
https://github.com/Start9Labs/start-os.git
synced 2026-03-30 12:11:56 +00:00
Seed patchdb UI data (#1835)
* adjust types for patchdb ui data and create seed file * feat: For init and the migration use defaults * fix update path * update build for ui seed file * fix accidential revert * chore: Convert to do during the init * chore: Update the commit message Co-authored-by: BluJ <mogulslayer@gmail.com>
This commit is contained in:
@@ -147,6 +147,44 @@ impl serde::ser::Serialize for CharSet {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait MergeWith {
|
||||
fn merge_with(&mut self, other: &serde_json::Value);
|
||||
}
|
||||
|
||||
impl MergeWith for serde_json::Value {
|
||||
fn merge_with(&mut self, other: &serde_json::Value) {
|
||||
use serde_json::Value::Object;
|
||||
if let (Object(orig), Object(ref other)) = (self, other) {
|
||||
for (key, val) in other.into_iter() {
|
||||
match (orig.get_mut(key), val) {
|
||||
(Some(new_orig @ Object(_)), other @ Object(_)) => {
|
||||
new_orig.merge_with(other);
|
||||
}
|
||||
(None, _) => {
|
||||
orig.insert(key.clone(), val.clone());
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn merge_with_tests() {
|
||||
use serde_json::json;
|
||||
|
||||
let mut a = json!(
|
||||
{"a": 1, "c": {"d": "123"}, "i": [1,2,3], "j": {}, "k":[1,2,3], "l": "test"}
|
||||
);
|
||||
a.merge_with(
|
||||
&json!({"a":"a", "b": "b", "c":{"d":"d", "e":"e"}, "f":{"g":"g"}, "h": [1,2,3], "i":"i", "j":[1,2,3], "k":{}}),
|
||||
);
|
||||
assert_eq!(
|
||||
a,
|
||||
json!({"a": 1, "c": {"d": "123", "e":"e"}, "b":"b", "f": {"g":"g"}, "h":[1,2,3], "i":[1,2,3], "j": {}, "k":[1,2,3], "l": "test"})
|
||||
)
|
||||
}
|
||||
pub mod serde_regex {
|
||||
use regex::Regex;
|
||||
use serde::*;
|
||||
|
||||
@@ -69,7 +69,8 @@ impl Database {
|
||||
},
|
||||
package_data: AllPackageData::default(),
|
||||
recovered_packages: BTreeMap::new(),
|
||||
ui: Value::Object(Default::default()),
|
||||
ui: serde_json::from_str(include_str!("../../../frontend/patchdb-ui-seed.json"))
|
||||
.unwrap(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ use helpers::NonDetachingJoinHandle;
|
||||
use patch_db::{DbHandle, LockReceipt, LockType};
|
||||
use tokio::process::Command;
|
||||
|
||||
use crate::config::util::MergeWith;
|
||||
use crate::context::rpc::RpcContextConfig;
|
||||
use crate::db::model::ServerStatus;
|
||||
use crate::install::PKG_DOCKER_DIR;
|
||||
@@ -192,6 +193,25 @@ pub async fn init(cfg: &RpcContextConfig) -> Result<InitResult, Error> {
|
||||
.server_info()
|
||||
.lock(&mut handle, LockType::Write)
|
||||
.await?;
|
||||
|
||||
let defaults: serde_json::Value =
|
||||
serde_json::from_str(include_str!("../../frontend/patchdb-ui-seed.json")).map_err(|x| {
|
||||
Error::new(
|
||||
eyre!("Deserialization error {:?}", x),
|
||||
crate::ErrorKind::Deserialization,
|
||||
)
|
||||
})?;
|
||||
let mut ui = crate::db::DatabaseModel::new()
|
||||
.ui()
|
||||
.get(&mut handle, false)
|
||||
.await?
|
||||
.clone();
|
||||
ui.merge_with(&defaults);
|
||||
crate::db::DatabaseModel::new()
|
||||
.ui()
|
||||
.put(&mut handle, &ui)
|
||||
.await?;
|
||||
|
||||
let receipts = InitReceipts::new(&mut handle).await?;
|
||||
|
||||
let should_rebuild = tokio::fs::metadata(SYSTEM_REBUILD_PATH).await.is_ok()
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
use emver::VersionRange;
|
||||
|
||||
use crate::config::util::MergeWith;
|
||||
use crate::hostname::{generate_id, get_hostname, sync_hostname};
|
||||
use crate::ErrorKind;
|
||||
|
||||
use super::v0_3_0::V0_3_0_COMPAT;
|
||||
use super::*;
|
||||
@@ -35,28 +37,9 @@ impl VersionT for Version {
|
||||
.await?;
|
||||
|
||||
sync_hostname(db).await?;
|
||||
let mut ui = crate::db::DatabaseModel::new()
|
||||
.ui()
|
||||
.get(db, false)
|
||||
.await?
|
||||
.clone();
|
||||
if let serde_json::Value::Object(ref mut ui) = ui {
|
||||
ui.insert("ack-instructions".to_string(), serde_json::json!({}));
|
||||
}
|
||||
crate::db::DatabaseModel::new().ui().put(db, &ui).await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
async fn down<Db: DbHandle>(&self, db: &mut Db) -> Result<(), Error> {
|
||||
let mut ui = crate::db::DatabaseModel::new()
|
||||
.ui()
|
||||
.get(db, false)
|
||||
.await?
|
||||
.clone();
|
||||
if let serde_json::Value::Object(ref mut ui) = ui {
|
||||
ui.remove("ack-instructions");
|
||||
}
|
||||
crate::db::DatabaseModel::new().ui().put(db, &ui).await?;
|
||||
async fn down<Db: DbHandle>(&self, _db: &mut Db) -> Result<(), Error> {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user