Compare commits
No commits in common. "d93d3a83d1fbf285a55cb02a591ba21d5320f73b" and "37feb713be701bf2bedd70f5cc9e6394d4447fe9" have entirely different histories.
d93d3a83d1
...
37feb713be
22 changed files with 43 additions and 252 deletions
|
@ -1,5 +1,5 @@
|
||||||
[workspace]
|
[workspace]
|
||||||
members = [ "examples/*", "lazy-supplements", "lazy-supplements-*" ]
|
members = [ "examples/timestamper", "lazy-supplements", "lazy-supplements-*" ]
|
||||||
|
|
||||||
[workspace.package]
|
[workspace.package]
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
|
|
|
@ -1,20 +0,0 @@
|
||||||
[package]
|
|
||||||
name = "simple-list"
|
|
||||||
edition.workspace = true
|
|
||||||
version.workspace = true
|
|
||||||
description.workspace = true
|
|
||||||
license.workspace = true
|
|
||||||
repository.workspace = true
|
|
||||||
|
|
||||||
[dependencies]
|
|
||||||
chrono = "0.4.41"
|
|
||||||
clap = { version = "4.5.38", features = ["derive"] }
|
|
||||||
lazy-supplements.workspace = true
|
|
||||||
lazy-supplements-migration.workspace = true
|
|
||||||
sea-orm = "1.1.11"
|
|
||||||
sea-orm-migration.workspace = true
|
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
|
||||||
tokio = "1.45.1"
|
|
||||||
|
|
||||||
[dev-dependencies]
|
|
||||||
lazy-supplements = { workspace = true, features = [ "test" ] }
|
|
|
@ -1,16 +0,0 @@
|
||||||
use clap::{Parser, Subcommand};
|
|
||||||
use lazy_supplements::{cli::ServerArgs, config::PartialServerConfig};
|
|
||||||
|
|
||||||
#[derive(Debug, Parser)]
|
|
||||||
pub struct Cli {
|
|
||||||
#[command(subcommand)]
|
|
||||||
command: CliCommand
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Debug, Subcommand)]
|
|
||||||
pub enum CliCommand {
|
|
||||||
Add,
|
|
||||||
Delete,
|
|
||||||
List,
|
|
||||||
Server(ServerArgs)
|
|
||||||
}
|
|
|
@ -1,58 +0,0 @@
|
||||||
use chrono::Local;
|
|
||||||
use sea_orm::entity::{
|
|
||||||
*,
|
|
||||||
prelude::*
|
|
||||||
};
|
|
||||||
use serde::{Deserialize, Serialize};
|
|
||||||
|
|
||||||
|
|
||||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
|
|
||||||
#[sea_orm(table_name = "list_item")]
|
|
||||||
pub struct Model {
|
|
||||||
#[sea_orm(primary_key, auto_increment = false)]
|
|
||||||
pub id: Uuid,
|
|
||||||
#[sea_orm(indexed)]
|
|
||||||
pub created_at: DateTimeUtc,
|
|
||||||
#[sea_orm(indexed)]
|
|
||||||
pub updated_at: DateTimeUtc,
|
|
||||||
#[sea_orm(indexed)]
|
|
||||||
pub is_trashed: bool,
|
|
||||||
#[sea_orm(indexed)]
|
|
||||||
pub content: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Copy, Clone, Debug, DeriveRelation, EnumIter)]
|
|
||||||
pub enum Relation {}
|
|
||||||
|
|
||||||
impl ActiveModelBehavior for ActiveModel {}
|
|
||||||
|
|
||||||
impl ActiveModel {
|
|
||||||
pub fn new() -> Self {
|
|
||||||
let timestamp: DateTimeUtc = Local::now().to_utc();
|
|
||||||
Self{
|
|
||||||
id: Set(Uuid::new_v4()),
|
|
||||||
created_at: Set(timestamp),
|
|
||||||
updated_at: Set(timestamp),
|
|
||||||
is_trashed: Set(false),
|
|
||||||
..Default::default()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
use crate::global::GLOBAL;
|
|
||||||
|
|
||||||
#[tokio::test]
|
|
||||||
async fn check_insert_node() {
|
|
||||||
let db = crate::tests::get_or_init_temporary_database().await;
|
|
||||||
|
|
||||||
ActiveModel{
|
|
||||||
content: Set("test note".to_owned()),
|
|
||||||
..ActiveModel::new()
|
|
||||||
}.insert(db).await.unwrap();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
|
@ -1,10 +0,0 @@
|
||||||
mod list_item;
|
|
||||||
pub use lazy_supplements::entity::*;
|
|
||||||
|
|
||||||
pub use list_item::{
|
|
||||||
ActiveModel as ListItemActiveModel,
|
|
||||||
Column as ListItemColumn,
|
|
||||||
Entity as ListItemEntity,
|
|
||||||
Model as ListItemModel,
|
|
||||||
};
|
|
||||||
|
|
|
@ -1,28 +0,0 @@
|
||||||
use clap::Parser;
|
|
||||||
|
|
||||||
mod migration;
|
|
||||||
mod cli;
|
|
||||||
mod entity;
|
|
||||||
|
|
||||||
pub use lazy_supplements::error;
|
|
||||||
pub use lazy_supplements::global;
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
pub mod tests {
|
|
||||||
use sea_orm::DatabaseConnection;
|
|
||||||
use sea_orm_migration::MigratorTrait;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
pub async fn get_or_init_temporary_database() -> &'static DatabaseConnection {
|
|
||||||
global::GLOBAL.get_or_try_init_temporary_database( |x| async {
|
|
||||||
migration::Migrator::up(&x, None).await?;
|
|
||||||
Ok(x)
|
|
||||||
}).await.unwrap()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[tokio::main]
|
|
||||||
async fn main() {
|
|
||||||
let args = cli::Cli::parse();
|
|
||||||
println!("{:?}", args);
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
use sea_orm_migration::{prelude::*, schema::*};
|
|
||||||
|
|
||||||
use lazy_supplements_migration::TableMigration;
|
|
||||||
|
|
||||||
#[derive(DeriveMigrationName)]
|
|
||||||
pub struct Migration;
|
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
|
||||||
impl MigrationTrait for Migration {
|
|
||||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
||||||
ListItem::up(manager).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
||||||
ListItem::down(manager).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(DeriveIden)]
|
|
||||||
enum ListItem {
|
|
||||||
Table,
|
|
||||||
Id,
|
|
||||||
CreatedAt,
|
|
||||||
UpdatedAt,
|
|
||||||
IsTrashed,
|
|
||||||
Content,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
|
||||||
impl TableMigration for ListItem {
|
|
||||||
async fn up<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr> {
|
|
||||||
manager.create_table(
|
|
||||||
Table::create()
|
|
||||||
.table(Self::Table)
|
|
||||||
.if_not_exists()
|
|
||||||
.col(pk_uuid(Self::Id))
|
|
||||||
.col(timestamp(Self::CreatedAt))
|
|
||||||
.col(timestamp(Self::UpdatedAt))
|
|
||||||
.col(boolean(Self::IsTrashed))
|
|
||||||
.col(string_len(Self::Content, 255))
|
|
||||||
.to_owned()
|
|
||||||
).await?;
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
async fn down<'a>(manager: &'a SchemaManager<'a>) -> Result<(), DbErr>{
|
|
||||||
manager.drop_table(Table::drop().table(Self::Table).to_owned()).await
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,15 +0,0 @@
|
||||||
use sea_orm_migration::{prelude::*, schema::*};
|
|
||||||
use lazy_supplements_migration::m20220101_000001_create_lazy_supplements_tables;
|
|
||||||
mod m20220101_000002_create_simple_list_tables;
|
|
||||||
|
|
||||||
pub struct Migrator;
|
|
||||||
|
|
||||||
#[async_trait::async_trait]
|
|
||||||
impl MigratorTrait for Migrator {
|
|
||||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
|
||||||
vec![
|
|
||||||
Box::new(m20220101_000002_create_simple_list_tables::Migration),
|
|
||||||
Box::new(m20220101_000001_create_lazy_supplements_tables::Migration)
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
10
examples/timestamper/Cargo.toml
Normal file
10
examples/timestamper/Cargo.toml
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
[package]
|
||||||
|
name = "timestamper"
|
||||||
|
edition.workspace = true
|
||||||
|
version.workspace = true
|
||||||
|
description.workspace = true
|
||||||
|
license.workspace = true
|
||||||
|
repository.workspace = true
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
lazy-supplements.workspace = true
|
3
examples/timestamper/src/main.rs
Normal file
3
examples/timestamper/src/main.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
fn main() {
|
||||||
|
println!("Hello, world!");
|
||||||
|
}
|
|
@ -1,6 +1,6 @@
|
||||||
use sea_orm_migration::{prelude::*, schema::*};
|
use sea_orm_migration::{prelude::*, schema::*};
|
||||||
|
|
||||||
pub mod m20220101_000001_create_lazy_supplements_tables;
|
mod m20220101_000001_create_lazy_supplements_tables;
|
||||||
|
|
||||||
pub struct Migrator;
|
pub struct Migrator;
|
||||||
|
|
||||||
|
|
|
@ -6,10 +6,6 @@ description.workspace = true
|
||||||
license.workspace = true
|
license.workspace = true
|
||||||
repository.workspace = true
|
repository.workspace = true
|
||||||
|
|
||||||
[features]
|
|
||||||
default = []
|
|
||||||
test = ["dep:tempfile"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
base64 = "0.22.1"
|
base64 = "0.22.1"
|
||||||
chrono = "0.4.41"
|
chrono = "0.4.41"
|
||||||
|
@ -17,8 +13,9 @@ chrono-tz = "0.10.3"
|
||||||
clap = { version = "4.5.38", features = ["derive"] }
|
clap = { version = "4.5.38", features = ["derive"] }
|
||||||
libp2p.workspace = true
|
libp2p.workspace = true
|
||||||
sea-orm = { version = "1.1.11", features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros", "with-chrono", "with-uuid"] }
|
sea-orm = { version = "1.1.11", features = ["sqlx-sqlite", "runtime-tokio-native-tls", "macros", "with-chrono", "with-uuid"] }
|
||||||
|
sea-orm-migration.workspace = true
|
||||||
serde = { version = "1.0.219", features = ["derive"] }
|
serde = { version = "1.0.219", features = ["derive"] }
|
||||||
tempfile = { version = "3.20.0", optional = true }
|
tempfile = "3.20.0"
|
||||||
thiserror = "2.0.12"
|
thiserror = "2.0.12"
|
||||||
tokio = { version = "1.45.0", features = ["macros", "rt"] }
|
tokio = { version = "1.45.0", features = ["macros", "rt"] }
|
||||||
toml = "0.8.22"
|
toml = "0.8.22"
|
||||||
|
@ -26,5 +23,3 @@ uuid = { version = "1.17.0", features = ["v4"] }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
lazy-supplements-migration.workspace = true
|
lazy-supplements-migration.workspace = true
|
||||||
sea-orm-migration.workspace = true
|
|
||||||
tempfile = "3.20.0"
|
|
|
@ -4,8 +4,6 @@ mod connect;
|
||||||
mod init;
|
mod init;
|
||||||
mod server;
|
mod server;
|
||||||
|
|
||||||
pub use server::ServerArgs;
|
|
||||||
|
|
||||||
pub fn default_config_path() -> PathBuf {
|
pub fn default_config_path() -> PathBuf {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
|
@ -49,7 +49,7 @@ mod tests {
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn check_insert_node() {
|
async fn check_insert_node() {
|
||||||
let db = crate::global::get_or_init_temporary_database().await;
|
let db = GLOBAL.get_or_init_temporary_database().await;
|
||||||
|
|
||||||
ActiveModel{
|
ActiveModel{
|
||||||
peer_id: Set(identity::Keypair::generate_ed25519().public().to_peer_id().to_string()),
|
peer_id: Set(identity::Keypair::generate_ed25519().public().to_peer_id().to_string()),
|
||||||
|
|
|
@ -38,11 +38,11 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
use uuid::Uuid;
|
use uuid::Uuid;
|
||||||
use crate::global::get_or_init_temporary_database;
|
use crate::global::GLOBAL;
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn check_insert_record_deletion() {
|
async fn check_insert_record_deletion() {
|
||||||
let db = get_or_init_temporary_database().await;
|
let db = GLOBAL.get_or_init_temporary_database().await;
|
||||||
|
|
||||||
assert!(ActiveModel{
|
assert!(ActiveModel{
|
||||||
table_name: Set("test_table".to_string()),
|
table_name: Set("test_table".to_string()),
|
||||||
|
|
|
@ -1,70 +1,55 @@
|
||||||
use std::path::Path;
|
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
|
||||||
|
use sea_orm_migration::MigratorTrait;
|
||||||
use sea_orm::{ConnectOptions, Database, DbErr, DatabaseConnection};
|
|
||||||
use crate::error::Error;
|
use crate::error::Error;
|
||||||
use tokio::sync::OnceCell;
|
use tokio::sync::OnceCell;
|
||||||
|
|
||||||
use super::Global;
|
use super::Global;
|
||||||
|
|
||||||
#[cfg(any(test, feature="test"))]
|
|
||||||
pub static TEST_DATABASE_URL: std::sync::LazyLock<tempfile::TempPath> = std::sync::LazyLock::new(|| {
|
|
||||||
let mut temp_path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
|
|
||||||
temp_path.disable_cleanup(true);
|
|
||||||
println!("{}", temp_path.as_os_str().to_str().unwrap());
|
|
||||||
temp_path
|
|
||||||
});
|
|
||||||
|
|
||||||
impl Global {
|
impl Global {
|
||||||
fn get_database(&self) -> Option<&DatabaseConnection> {
|
fn get_database(&self) -> Option<&DatabaseConnection> {
|
||||||
self.database.get()
|
self.database.get()
|
||||||
}
|
}
|
||||||
async fn get_or_try_init_database<T, F, Fut>(&self, path: T, migration: F) -> Result<&DatabaseConnection, Error>
|
async fn get_or_try_init_database<T, U>(&self, options: T, _: U) -> Result<&DatabaseConnection, Error>
|
||||||
where
|
where
|
||||||
T: AsRef<Path>,
|
T: Into<ConnectOptions>,
|
||||||
F: FnOnce(DatabaseConnection) -> Fut,
|
U: MigratorTrait,
|
||||||
Fut: Future<Output = Result<DatabaseConnection, DbErr>>
|
|
||||||
{
|
{
|
||||||
let url = "sqlite://".to_string() + path.as_ref().to_str().unwrap() + "?mode=rwc";
|
|
||||||
|
|
||||||
Ok(self.database.get_or_try_init(|| async {
|
Ok(self.database.get_or_try_init(|| async {
|
||||||
let db = Database::connect(&url).await?;
|
let db = Database::connect(options).await?;
|
||||||
Ok::<DatabaseConnection, DbErr>(migration(db).await?)
|
U::up(&db, None).await?;
|
||||||
|
Ok::<DatabaseConnection, Error>(db)
|
||||||
}).await?)
|
}).await?)
|
||||||
}
|
}
|
||||||
#[cfg(any(test, feature="test"))]
|
|
||||||
pub async fn get_or_try_init_temporary_database<F, Fut>(&self, migration: F) -> Result<&DatabaseConnection, Error>
|
|
||||||
where
|
|
||||||
F: FnOnce(DatabaseConnection) -> Fut,
|
|
||||||
Fut: Future<Output = Result<DatabaseConnection, DbErr>>
|
|
||||||
{
|
|
||||||
self.get_or_try_init_database(&*TEST_DATABASE_URL, migration).await
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
pub mod tests {
|
pub mod tests {
|
||||||
use std::sync::LazyLock;
|
use std::sync::LazyLock;
|
||||||
|
|
||||||
use lazy_supplements_migration::Migrator;
|
use lazy_supplements_migration::Migrator;
|
||||||
use sea_orm_migration::MigratorTrait;
|
|
||||||
|
|
||||||
use crate::global::GLOBAL;
|
use crate::global::GLOBAL;
|
||||||
|
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
pub async fn get_or_init_temporary_database() -> &'static DatabaseConnection {
|
pub static TEST_DATABASE_URL: LazyLock<String> = LazyLock::new(|| {
|
||||||
GLOBAL.get_or_try_init_temporary_database( |x| async {
|
let mut temp_path = tempfile::NamedTempFile::new().unwrap().into_temp_path();
|
||||||
Migrator::up(&x, None).await?;
|
temp_path.disable_cleanup(true);
|
||||||
Ok(x)
|
let url = "sqlite://".to_string() + temp_path.as_os_str().to_str().unwrap() + "?mode=rwc";
|
||||||
}).await.unwrap()
|
println!("{}", &url);
|
||||||
|
url
|
||||||
|
});
|
||||||
|
|
||||||
|
impl Global {
|
||||||
|
pub async fn get_or_init_temporary_database(&self) -> &DatabaseConnection {
|
||||||
|
|
||||||
|
self.get_or_try_init_database(&*TEST_DATABASE_URL, Migrator).await.unwrap()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[tokio::test]
|
#[tokio::test]
|
||||||
async fn connect_database () {
|
async fn connect_database () {
|
||||||
let db = get_or_init_temporary_database().await;
|
let db = GLOBAL.get_or_init_temporary_database().await;
|
||||||
assert!(db.ping().await.is_ok());
|
assert!(db.ping().await.is_ok());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,3 @@ pub struct Global {
|
||||||
server_config: OnceCell<ServerConfig>,
|
server_config: OnceCell<ServerConfig>,
|
||||||
database: OnceCell<DatabaseConnection>,
|
database: OnceCell<DatabaseConnection>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
pub use database::tests::get_or_init_temporary_database;
|
|
|
@ -2,4 +2,4 @@ pub mod cli;
|
||||||
pub mod config;
|
pub mod config;
|
||||||
pub mod entity;
|
pub mod entity;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod global;
|
pub mod global;
|
Loading…
Add table
Reference in a new issue