Skip to content

[WIP] Sea ORM #7

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

Draft
wants to merge 4 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ default = ["std"]
std = ["crypto/std", "objects/std"]
testing = ["objects/testing", "mock"]

[workspace]
members = [".", "entity", "migration"]

[dependencies]
clap = { version = "4.3" , features = ["derive"] }
crypto = { package = "miden-crypto", git = "https://github.com/0xPolygonMiden/crypto", branch = "next", default-features = false }
Expand All @@ -26,6 +29,11 @@ rusqlite_migration = { version = "1.0" }
rand = { version="0.8.5" }
serde = {version="1.0", features = ["derive"]}
serde_json = { version = "1.0", features = ["raw_value"] }
futures = "0.3.28"
sea-orm = { version = "^0.12.0", features = [ "sqlx-sqlite", "runtime-tokio-rustls", "macros"], default-features = false }
entity = { path = "entity" }
migration = { path = "migration" }
tokio = {version = "1.34.0", features = ["macros", "rt-multi-thread"] }

[dev-dependencies]
uuid = { version = "1.6.1", features = ["serde", "v4"] }
Expand Down
9 changes: 9 additions & 0 deletions entity/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "entity"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
sea-orm = { version = "^0.12.0"}
32 changes: 32 additions & 0 deletions entity/src/account_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "account_code")]
pub struct Model {
#[sea_orm(
primary_key,
auto_increment = false,
column_type = "Binary(BlobSize::Blob(None))"
)]
pub root: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub procedures: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub module: Vec<u8>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::accounts::Entity")]
Accounts,
}

impl Related<super::accounts::Entity> for Entity {
fn to() -> RelationDef {
Relation::Accounts.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
32 changes: 32 additions & 0 deletions entity/src/account_keys.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "account_keys")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub account_id: i64,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub key_pair: Vec<u8>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::accounts::Entity",
from = "Column::AccountId",
to = "super::accounts::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
Accounts,
}

impl Related<super::accounts::Entity> for Entity {
fn to() -> RelationDef {
Relation::Accounts.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
30 changes: 30 additions & 0 deletions entity/src/account_storage.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "account_storage")]
pub struct Model {
#[sea_orm(
primary_key,
auto_increment = false,
column_type = "Binary(BlobSize::Blob(None))"
)]
pub root: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub slots: Vec<u8>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::accounts::Entity")]
Accounts,
}

impl Related<super::accounts::Entity> for Entity {
fn to() -> RelationDef {
Relation::Accounts.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
30 changes: 30 additions & 0 deletions entity/src/account_vaults.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "account_vaults")]
pub struct Model {
#[sea_orm(
primary_key,
auto_increment = false,
column_type = "Binary(BlobSize::Blob(None))"
)]
pub root: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub assets: Vec<u8>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::accounts::Entity")]
Accounts,
}

impl Related<super::accounts::Entity> for Entity {
fn to() -> RelationDef {
Relation::Accounts.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
74 changes: 74 additions & 0 deletions entity/src/accounts.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "accounts")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i64,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub code_root: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub storage_root: Vec<u8>,
#[sea_orm(column_type = "Binary(BlobSize::Blob(None))")]
pub vault_root: Vec<u8>,
pub nonce: i64,
pub committed: bool,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::account_code::Entity",
from = "Column::CodeRoot",
to = "super::account_code::Column::Root",
on_update = "NoAction",
on_delete = "NoAction"
)]
AccountCode,
#[sea_orm(has_many = "super::account_keys::Entity")]
AccountKeys,
#[sea_orm(
belongs_to = "super::account_storage::Entity",
from = "Column::StorageRoot",
to = "super::account_storage::Column::Root",
on_update = "NoAction",
on_delete = "NoAction"
)]
AccountStorage,
#[sea_orm(
belongs_to = "super::account_vaults::Entity",
from = "Column::VaultRoot",
to = "super::account_vaults::Column::Root",
on_update = "NoAction",
on_delete = "NoAction"
)]
AccountVaults,
}

impl Related<super::account_code::Entity> for Entity {
fn to() -> RelationDef {
Relation::AccountCode.def()
}
}

impl Related<super::account_keys::Entity> for Entity {
fn to() -> RelationDef {
Relation::AccountKeys.def()
}
}

impl Related<super::account_storage::Entity> for Entity {
fn to() -> RelationDef {
Relation::AccountStorage.def()
}
}

impl Related<super::account_vaults::Entity> for Entity {
fn to() -> RelationDef {
Relation::AccountVaults.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
9 changes: 9 additions & 0 deletions entity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

pub mod prelude;

pub mod account_code;
pub mod account_keys;
pub mod account_storage;
pub mod account_vaults;
pub mod accounts;
7 changes: 7 additions & 0 deletions entity/src/prelude.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.6

pub use super::account_code::Entity as AccountCode;
pub use super::account_keys::Entity as AccountKeys;
pub use super::account_storage::Entity as AccountStorage;
pub use super::account_vaults::Entity as AccountVaults;
pub use super::accounts::Entity as Accounts;
22 changes: 22 additions & 0 deletions migration/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
[package]
name = "migration"
version = "0.1.0"
edition = "2021"
publish = false

[lib]
name = "migration"
path = "src/lib.rs"

[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }

[dependencies.sea-orm-migration]
version = "0.12.0"
features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
# e.g.
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
"sqlx-sqlite", # `DATABASE_DRIVER` feature
]
41 changes: 41 additions & 0 deletions migration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Running Migrator CLI

- Generate a new migration file
```sh
cargo run -- generate MIGRATION_NAME
```
- Apply all pending migrations
```sh
cargo run
```
```sh
cargo run -- up
```
- Apply first 10 pending migrations
```sh
cargo run -- up -n 10
```
- Rollback last applied migrations
```sh
cargo run -- down
```
- Rollback last 10 applied migrations
```sh
cargo run -- down -n 10
```
- Drop all tables from the database, then reapply all migrations
```sh
cargo run -- fresh
```
- Rollback all applied migrations, then reapply all migrations
```sh
cargo run -- refresh
```
- Rollback all applied migrations
```sh
cargo run -- reset
```
- Check the status of all migrations
```sh
cargo run -- status
```
12 changes: 12 additions & 0 deletions migration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub use sea_orm_migration::prelude::*;

mod m20220101_000001_create_table;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_create_table::Migration)]
}
}
Loading