From 2ea36666bc9a2fb9397ca58b708f97ec38349c2a Mon Sep 17 00:00:00 2001 From: Jamie Date: Sat, 2 Oct 2021 08:51:14 -0700 Subject: [PATCH] improved schema, started implimenting relations and init the default library --- src-tauri/src/db/entity/capture_device.rs | 21 +++++++ src-tauri/src/db/entity/dir.rs | 18 +++--- src-tauri/src/db/entity/file.rs | 33 ++++++++++- src-tauri/src/db/entity/library.rs | 28 +++++++++ src-tauri/src/db/entity/mod.rs | 6 ++ src-tauri/src/db/entity/space.rs | 24 ++++++++ src-tauri/src/db/entity/storage_device.rs | 21 +++++++ src-tauri/src/db/entity/tag.rs | 24 ++++++++ src-tauri/src/db/migrate.rs | 19 ------- .../src/db/migrations/primary/V1__initial.sql | 57 ++++++++++++------- src-tauri/src/db/mod.rs | 1 - src-tauri/src/filesystem/init.rs | 32 +++++++++++ src-tauri/src/filesystem/mod.rs | 1 + src-tauri/src/main.rs | 6 +- 14 files changed, 236 insertions(+), 55 deletions(-) create mode 100644 src-tauri/src/db/entity/capture_device.rs create mode 100644 src-tauri/src/db/entity/library.rs create mode 100644 src-tauri/src/db/entity/space.rs create mode 100644 src-tauri/src/db/entity/storage_device.rs create mode 100644 src-tauri/src/db/entity/tag.rs delete mode 100644 src-tauri/src/db/migrate.rs create mode 100644 src-tauri/src/filesystem/init.rs diff --git a/src-tauri/src/db/entity/capture_device.rs b/src-tauri/src/db/entity/capture_device.rs new file mode 100644 index 000000000..4f1fdd97c --- /dev/null +++ b/src-tauri/src/db/entity/capture_device.rs @@ -0,0 +1,21 @@ +use chrono::NaiveDateTime; +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +// ------------------------------------- +// Entity: Space +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DeriveEntityModel, Default)] +#[sea_orm(table_name = "capture_devices")] +// ------------------------------------- +pub struct Model { + #[sea_orm(primary_key)] + pub id: u32, + pub name: String, + pub date_created: Option, + pub date_modified: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/entity/dir.rs b/src-tauri/src/db/entity/dir.rs index eb8cf6fd9..014fa597b 100644 --- a/src-tauri/src/db/entity/dir.rs +++ b/src-tauri/src/db/entity/dir.rs @@ -1,33 +1,29 @@ -use crate::crypto; use chrono::NaiveDateTime; use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; // ------------------------------------- // Entity: Directory -// Represents an item discovered on the filesystem #[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DeriveEntityModel, Default)] #[sea_orm(table_name = "directories")] // ------------------------------------- -pub struct Directory { +pub struct Model { // identity #[sea_orm(primary_key)] pub id: u32, pub name: String, - pub uri String, - // calculations + pub uri: String, pub calculated_size_in_bytes: Option, pub calculated_file_count: Option, - // ownership + pub date_created: Option, + pub date_modified: Option, + pub date_indexed: Option, + pub library_id: u32, pub storage_device_id: Option, pub parent_directory_id: Option, - // date - pub date_created: DateTime, - pub date_modified: DateTime, - pub date_indexed: DateTime, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] pub enum Relation {} -impl ActiveModelBehavior for ActiveModel {} \ No newline at end of file +impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/entity/file.rs b/src-tauri/src/db/entity/file.rs index 97b095be6..7ac6779d0 100644 --- a/src-tauri/src/db/entity/file.rs +++ b/src-tauri/src/db/entity/file.rs @@ -1,4 +1,3 @@ -use crate::crypto; use chrono::NaiveDateTime; use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; @@ -38,7 +37,35 @@ pub struct Model { pub parent_file_id: Option, } -#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] -pub enum Relation {} +#[derive(Copy, Clone, Debug, EnumIter)] +pub enum Relation { + StorageDevice, + CaptureDevice, +} + +impl RelationTrait for Relation { + fn def(&self) -> RelationDef { + match self { + Self::StorageDevice => Entity::belongs_to(super::storage_device::Entity) + .from(Column::StorageDeviceId) + .to(super::storage_device::Column::Id) + .into(), + Self::CaptureDevice => Entity::belongs_to(super::capture_device::Entity) + .from(Column::CaptureDeviceId) + .to(super::capture_device::Column::Id) + .into(), + } + } +} +impl Related for Entity { + fn to() -> RelationDef { + Relation::StorageDevice.def() + } +} +impl Related for Entity { + fn to() -> RelationDef { + Relation::CaptureDevice.def() + } +} impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/entity/library.rs b/src-tauri/src/db/entity/library.rs new file mode 100644 index 000000000..0c4389e16 --- /dev/null +++ b/src-tauri/src/db/entity/library.rs @@ -0,0 +1,28 @@ +use chrono::NaiveDateTime; +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +// ------------------------------------- +// Entity: Directory +// Represents an item discovered on the filesystem +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DeriveEntityModel, Default)] +#[sea_orm(table_name = "libraries")] +// ------------------------------------- +pub struct Model { + // identity + #[sea_orm(primary_key)] + pub id: u32, + pub name: String, + pub is_primary: bool, + pub remote_id: Option, + pub total_file_count: Option, + pub total_bytes_used: Option, + pub total_byte_capacity: Option, + pub date_created: Option, + pub timezone: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/entity/mod.rs b/src-tauri/src/db/entity/mod.rs index 2e172cd0f..c7d786c40 100644 --- a/src-tauri/src/db/entity/mod.rs +++ b/src-tauri/src/db/entity/mod.rs @@ -1 +1,7 @@ +pub mod capture_device; +pub mod dir; pub mod file; +pub mod library; +pub mod space; +pub mod storage_device; +pub mod tag; diff --git a/src-tauri/src/db/entity/space.rs b/src-tauri/src/db/entity/space.rs new file mode 100644 index 000000000..1823bb29a --- /dev/null +++ b/src-tauri/src/db/entity/space.rs @@ -0,0 +1,24 @@ +use chrono::NaiveDateTime; +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +// ------------------------------------- +// Entity: Space +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DeriveEntityModel, Default)] +#[sea_orm(table_name = "spaces")] +// ------------------------------------- +pub struct Model { + #[sea_orm(primary_key)] + pub id: u32, + pub name: String, + pub calculated_size_in_bytes: Option, + pub calculated_file_count: Option, + pub library_id: String, + pub date_created: Option, + pub date_modified: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/entity/storage_device.rs b/src-tauri/src/db/entity/storage_device.rs new file mode 100644 index 000000000..e5cab98cb --- /dev/null +++ b/src-tauri/src/db/entity/storage_device.rs @@ -0,0 +1,21 @@ +use chrono::NaiveDateTime; +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +// ------------------------------------- +// Entity: Space +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DeriveEntityModel, Default)] +#[sea_orm(table_name = "storage_devices")] +// ------------------------------------- +pub struct Model { + #[sea_orm(primary_key)] + pub id: u32, + pub name: String, + pub date_created: Option, + pub date_modified: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/entity/tag.rs b/src-tauri/src/db/entity/tag.rs new file mode 100644 index 000000000..6e6eb28b7 --- /dev/null +++ b/src-tauri/src/db/entity/tag.rs @@ -0,0 +1,24 @@ +use chrono::NaiveDateTime; +use sea_orm::entity::prelude::*; +use serde::{Deserialize, Serialize}; + +// ------------------------------------- +// Entity: Tag +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, DeriveEntityModel, Default)] +#[sea_orm(table_name = "tags")] +// ------------------------------------- +pub struct Model { + #[sea_orm(primary_key)] + pub id: u32, + pub name: String, + pub total_files: Option, + pub redundancy_goal: Option, + pub library_id: String, + pub date_created: Option, + pub date_modified: Option, +} + +#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] +pub enum Relation {} + +impl ActiveModelBehavior for ActiveModel {} diff --git a/src-tauri/src/db/migrate.rs b/src-tauri/src/db/migrate.rs deleted file mode 100644 index c38db3b30..000000000 --- a/src-tauri/src/db/migrate.rs +++ /dev/null @@ -1,19 +0,0 @@ -use crate::db::connection; -use rusqlite::Connection; -use std::io; - -pub async fn migrate_primary() -> io::Result<()> { - let mut conn = Connection::open(connection::get_primary_db_url()).unwrap(); - - println!("Running migrations"); - mod embedded_primary { - use refinery::embed_migrations; - embed_migrations!("src/db/migrations/primary"); - } - - embedded_primary::migrations::runner() - .run(&mut conn) - .unwrap(); - - Ok(()) -} diff --git a/src-tauri/src/db/migrations/primary/V1__initial.sql b/src-tauri/src/db/migrations/primary/V1__initial.sql index 405060f7b..08857bacb 100644 --- a/src-tauri/src/db/migrations/primary/V1__initial.sql +++ b/src-tauri/src/db/migrations/primary/V1__initial.sql @@ -1,8 +1,24 @@ CREATE TABLE IF NOT EXISTS libraries ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, - shared BOOLEAN, - encryption INTEGER DEFAULT 0 + remote_id TEXT, + is_primary BOOLEAN NOT NULL DEFAULT TRUE, + encryption INTEGER NOT NULL DEFAULT 0, + total_file_count INTEGER NOT NULL DEFAULT "0", + total_bytes_used TEXT NOT NULL DEFAULT "0", + total_byte_capacity TEXT NOT NULL DEFAULT "0", + total_unique_bytes TEXT NOT NULL DEFAULT "0", + date_created DATE NOT NULL DEFAULT (datetime('now')), + timezone TEXT +); +CREATE TABLE IF NOT EXISTS spaces ( + id INTEGER PRIMARY KEY AUTOINCREMENT, + name TEXT NOT NULL, + encryption INTEGER DEFAULT 0, + library_id TEXT NOT NULL, + date_created DATE NOT NULL DEFAULT (datetime('now')), + date_modified DATE NOT NULL DEFAULT (datetime('now')), + FOREIGN KEY(library_id) REFERENCES libraries(id) ); CREATE TABLE IF NOT EXISTS files ( id INTEGER PRIMARY KEY AUTOINCREMENT, @@ -12,11 +28,11 @@ CREATE TABLE IF NOT EXISTS files ( name TEXT, extension TEXT, size_in_bytes TEXT NOT NULL, - encryption INTEGER DEFAULT 0, + encryption INTEGER NOT NULL DEFAULT 0, ipfs_id TEXT, - date_created TEXT NOT NULL, - date_modified TEXT NOT NULL, - date_indexed TEXT NOT NULL, + date_created DATE NOT NULL DEFAULT (datetime('now')), + date_modified DATE NOT NULL DEFAULT (datetime('now')), + date_indexed DATE NOT NULL DEFAULT (datetime('now')), library_id INTEGER NOT NULL, storage_device_id INTEGER NOT NULL, directory_id INTEGER, @@ -33,11 +49,11 @@ CREATE TABLE IF NOT EXISTS directories ( name TEXT, uri TEXT NOT NULL, encryption INTEGER DEFAULT 0, - calculated_size_in_bytes TEXT, - calculated_file_count INTEGER, - date_created TEXT NOT NULL, - date_modified TEXT NOT NULL, - date_indexed TEXT NOT NULL, + calculated_size_in_bytes TEXT DEFAULT "0", + calculated_file_count INTEGER DEFAULT 0, + date_created DATE NOT NULL DEFAULT (datetime('now')), + date_modified DATE NOT NULL DEFAULT (datetime('now')), + date_indexed DATE NOT NULL DEFAULT (datetime('now')), library_id INTEGER NOT NULL, storage_device_id INTEGER, parent_directory_id INTEGER, @@ -48,32 +64,35 @@ CREATE TABLE IF NOT EXISTS directories ( CREATE TABLE IF NOT EXISTS tags ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, - date_created TEXT NOT NULL, - date_modified TEXT NOT NULL + encryption INTEGER DEFAULT 0, + total_files INTEGER DEFAULT 0, + redundancy_goal INTEGER default 1, + date_created DATE NOT NULL DEFAULT (datetime('now')), + date_modified DATE NOT NULL DEFAULT (datetime('now')) ); CREATE TABLE IF NOT EXISTS tags_files ( tag_id INTEGER NOT NULL, file_id INTEGER NOT NULL, - date_created TEXT NOT NULL, + date_created DATE NOT NULL DEFAULT (datetime('now')), FOREIGN KEY(tag_id) REFERENCES tags(id), FOREIGN KEY(file_id) REFERENCES files(id) ); CREATE TABLE IF NOT EXISTS tags_directories ( tag_id INTEGER NOT NULL, directory_id INTEGER NOT NULL, - date_created TEXT NOT NULL, + date_created DATE NOT NULL DEFAULT (datetime('now')), FOREIGN KEY(tag_id) REFERENCES tags(id), FOREIGN KEY(directory_id) REFERENCES files(id) ); CREATE TABLE IF NOT EXISTS storage_devices ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, - date_created TEXT NOT NULL, - date_modified TEXT NOT NULL + date_created DATE NOT NULL DEFAULT (datetime('now')), + date_modified DATE NOT NULL DEFAULT (datetime('now')) ); CREATE TABLE IF NOT EXISTS capture_devices ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, - date_created TEXT NOT NULL, - date_modified TEXT NOT NULL + date_created DATE NOT NULL DEFAULT (datetime('now')), + date_modified DATE NOT NULL DEFAULT (datetime('now')) ); \ No newline at end of file diff --git a/src-tauri/src/db/mod.rs b/src-tauri/src/db/mod.rs index 1ba757968..182d773a6 100644 --- a/src-tauri/src/db/mod.rs +++ b/src-tauri/src/db/mod.rs @@ -1,3 +1,2 @@ pub mod connection; pub mod entity; -// pub mod migrate; diff --git a/src-tauri/src/filesystem/init.rs b/src-tauri/src/filesystem/init.rs new file mode 100644 index 000000000..dffae4e34 --- /dev/null +++ b/src-tauri/src/filesystem/init.rs @@ -0,0 +1,32 @@ +use crate::db::connection; +use crate::db::entity::library; +use sea_orm::entity::*; +use sea_orm::DbErr; +use sea_orm::QueryFilter; + +pub async fn init_library() -> Result<(), Box> { + let db = connection::get_connection().await?; + + let existing_libs = library::Entity::find() + .filter(library::Column::IsPrimary.eq(true)) + .all(&db) + .await + .unwrap(); + + if existing_libs.len() == 0 { + let library = library::ActiveModel { + name: Set("Primary".to_owned()), + is_primary: Set(true), + ..Default::default() + }; + + let library = library.save(&db).await?; + + println!("created library {:?}", library); + } else { + let existing_lib = &existing_libs[0]; + println!("library loaded {:?}", existing_lib); + }; + + Ok(()) +} diff --git a/src-tauri/src/filesystem/mod.rs b/src-tauri/src/filesystem/mod.rs index ca7d53824..a8d6b378b 100644 --- a/src-tauri/src/filesystem/mod.rs +++ b/src-tauri/src/filesystem/mod.rs @@ -1,3 +1,4 @@ pub mod checksum; pub mod directory; pub mod file; +pub mod init; diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index a75f20676..abb1e5868 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -20,8 +20,10 @@ fn main() { // .is_test(true) // .init(); - let connection = db::connection::create_primary_db(); - let primary_db = block_on(connection).unwrap(); + // create primary data base if not exists + block_on(db::connection::create_primary_db()).unwrap(); + // init filesystem and create library if missing + block_on(filesystem::init::init_library()).unwrap(); tauri::Builder::default() .invoke_handler(tauri::generate_handler![