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

feat: add sqlite migration #14

Merged
merged 1 commit into from
May 8, 2024
Merged
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
27 changes: 23 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 8 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
@@ -265,13 +265,16 @@ impl Application for App {
dialog_text_input: widget::Id::unique(),
};

let commands = vec![Command::perform(
todo::fetch_lists(),
|result| match result {
let commands = vec![
Command::perform(Service::migrate(), |result| match result {
Ok(_) => message::none(),
Err(_) => message::none(),
}),
Command::perform(todo::fetch_lists(), |result| match result {
Ok(data) => message::app(Message::PopulateLists(data)),
Err(_) => message::none(),
},
)];
}),
];

(app, Command::batch(commands))
}
4 changes: 3 additions & 1 deletion src/core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -20,4 +20,6 @@ async-trait = "0.1.68"
url = "2.3.1"
futures = "0.3"
libset = "0.1.6"
emojis = "0.6.1"
emojis = "0.6.1"
ron = "0.6.1"
dirs = "5.0.1"
124 changes: 73 additions & 51 deletions src/core/src/service.rs
Original file line number Diff line number Diff line change
@@ -6,77 +6,99 @@ use strum::IntoEnumIterator;
use strum_macros::{EnumIter, EnumString};

use crate::{
services::{
local::service::ComputerStorage,
},
task_service::TodoProvider,
models::{list::List, task::Task},
services::local::service::ComputerStorage,
task_service::TodoProvider,
};

static APP_ID: OnceLock<&str> = OnceLock::new();

pub struct Services;

impl Services {
pub fn init(app_id: &'static str) {
APP_ID.get_or_init(|| app_id);
}
pub fn init(app_id: &'static str) {
APP_ID.get_or_init(|| app_id);
}
}

#[derive(
Debug,
Default,
EnumIter,
EnumString,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Serialize,
Deserialize,
Debug,
Default,
EnumIter,
EnumString,
Clone,
Copy,
PartialEq,
Eq,
PartialOrd,
Ord,
Serialize,
Deserialize,
)]
pub enum Service {
#[default]
Computer,
#[default]
Computer,
}

impl Service {
/// Finds the requested service and returns it.
/// After implementing the Service trait in your service
/// struct, register your service here.
pub fn get_service(&self) -> Box<dyn TodoProvider> {
if APP_ID.get().is_none() {
panic!("Must call Service::init before trying to get a service");
}
/// Finds the requested service and returns it.
/// After implementing the Service trait in your service
/// struct, register your service here.
pub fn get_service(&self) -> Box<dyn TodoProvider> {
if APP_ID.get().is_none() {
panic!("Must call Service::init before trying to get a service");
}

let app_id = APP_ID.get().unwrap().to_string();
let app_id = APP_ID.get().unwrap().to_string();

match self {
Service::Computer => Box::new(ComputerStorage::new(app_id)),
}
}
match self {
Service::Computer => Box::new(ComputerStorage::new(app_id)),
}
}

/// Convenience method to get the list of services.
pub fn list() -> Vec<Self> {
Self::iter().collect()
}
/// Convenience method to get the list of services.
pub fn list() -> Vec<Self> {
Self::iter().collect()
}

/// Returns the icon for the service.
pub fn icon(&self) -> &str {
match self {
Service::Computer => {
"/dev/edfloreshz/Done/icons/scalable/services/computer.png"
},
}
}
/// Returns the icon for the service.
pub fn icon(&self) -> &str {
match self {
Service::Computer => "/dev/edfloreshz/Done/icons/scalable/services/computer.png",
}
}

pub async fn migrate() -> Result<(), Box<dyn std::error::Error>> {
let mut service = Service::Computer.get_service();
let lists = service.read_lists().await?;
let tasks = service.read_tasks().await?;
let migration = MigrationData::new(lists, tasks);
let contents = ron::to_string(&migration)?;
let path = dirs::config_dir()
.unwrap()
.join(APP_ID.get().unwrap())
.join("v1/database/migration.ron");
std::fs::write(path, contents)?;
Ok(())
}
}

#[derive(Debug, Serialize, Deserialize)]
pub struct MigrationData {
lists: Vec<List>,
tasks: Vec<Task>,
}
impl MigrationData {
fn new(lists: Vec<List>, tasks: Vec<Task>) -> Self {
Self { lists, tasks }
}
}

impl Display for Service {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
Service::Computer => "Computer".to_string(),
};
write!(f, "{}", str)
}
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let str = match self {
Service::Computer => "Computer".to_string(),
};
write!(f, "{}", str)
}
}
3 changes: 1 addition & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// Copyright 2023 System76 <info@system76.com>
// SPDX-License-Identifier: GPL-3.0-only

use crate::app::App;
mod app;
mod content;
mod details;
@@ -10,6 +9,6 @@ mod todo;
#[rustfmt::skip]
pub fn main() -> Result<(), Box<dyn std::error::Error>> {
let (settings, flags) = app::settings::init();
cosmic::app::run::<App>(settings, flags)?;
cosmic::app::run::<app::App>(settings, flags)?;
Ok(())
}