improved schema, started implimenting relations and init the default library

This commit is contained in:
Jamie 2021-10-02 08:51:14 -07:00
parent ede01967c2
commit 2ea36666bc
14 changed files with 236 additions and 55 deletions

View file

@ -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<NaiveDateTime>,
pub date_modified: Option<NaiveDateTime>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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<String>,
pub calculated_file_count: Option<u32>,
// ownership
pub date_created: Option<NaiveDateTime>,
pub date_modified: Option<NaiveDateTime>,
pub date_indexed: Option<NaiveDateTime>,
pub library_id: u32,
pub storage_device_id: Option<u32>,
pub parent_directory_id: Option<u32>,
// date
pub date_created: DateTime<Utc>,
pub date_modified: DateTime<Utc>,
pub date_indexed: DateTime<Utc>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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<u32>,
}
#[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<super::storage_device::Entity> for Entity {
fn to() -> RelationDef {
Relation::StorageDevice.def()
}
}
impl Related<super::capture_device::Entity> for Entity {
fn to() -> RelationDef {
Relation::CaptureDevice.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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<String>,
pub total_file_count: Option<u32>,
pub total_bytes_used: Option<String>,
pub total_byte_capacity: Option<String>,
pub date_created: Option<NaiveDateTime>,
pub timezone: Option<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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;

View file

@ -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<String>,
pub calculated_file_count: Option<u32>,
pub library_id: String,
pub date_created: Option<NaiveDateTime>,
pub date_modified: Option<NaiveDateTime>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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<NaiveDateTime>,
pub date_modified: Option<NaiveDateTime>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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<String>,
pub redundancy_goal: Option<u32>,
pub library_id: String,
pub date_created: Option<NaiveDateTime>,
pub date_modified: Option<NaiveDateTime>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View file

@ -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(())
}

View file

@ -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'))
);

View file

@ -1,3 +1,2 @@
pub mod connection;
pub mod entity;
// pub mod migrate;

View file

@ -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<DbErr>> {
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(())
}

View file

@ -1,3 +1,4 @@
pub mod checksum;
pub mod directory;
pub mod file;
pub mod init;

View file

@ -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![