first commit
This commit is contained in:
commit
5ee9b3cf38
|
@ -0,0 +1 @@
|
|||
target/
|
File diff suppressed because it is too large
Load Diff
|
@ -0,0 +1,14 @@
|
|||
[package]
|
||||
name = "seaorm-test"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
[dependencies]
|
||||
futures = "0.3.30"
|
||||
sea-orm = { version = "^0.12.5", features = [
|
||||
"sqlx-postgres",
|
||||
"runtime-async-std-native-tls",
|
||||
"macros",
|
||||
] }
|
||||
serde = "1.0.203"
|
||||
serde_json = "1.0.117"
|
File diff suppressed because it is too large
Load Diff
|
@ -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.12.0", features = ["attributes", "tokio1"] }
|
||||
|
||||
[dependencies.sea-orm-migration]
|
||||
version = "0.12.15"
|
||||
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-postgres", # `DATABASE_DRIVER` feature
|
||||
]
|
|
@ -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
|
||||
```
|
|
@ -0,0 +1,18 @@
|
|||
pub use sea_orm_migration::prelude::*;
|
||||
|
||||
mod m20240602_100749_create_typ;
|
||||
mod m20240602_100754_create_hersteller;
|
||||
mod m20240602_100800_create_modell;
|
||||
|
||||
pub struct Migrator;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigratorTrait for Migrator {
|
||||
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
|
||||
vec![
|
||||
Box::new(m20240602_100749_create_typ::Migration),
|
||||
Box::new(m20240602_100754_create_hersteller::Migration),
|
||||
Box::new(m20240602_100800_create_modell::Migration),
|
||||
]
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Typ::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Typ::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Typ::Name).string().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(Typ::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Iden)]
|
||||
#[iden(rename = "typen")]
|
||||
pub enum Typ {
|
||||
Table,
|
||||
Id,
|
||||
Name,
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Hersteller::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Hersteller::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Hersteller::Name).string().not_null())
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(Hersteller::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Iden)]
|
||||
#[iden(rename = "hersteller")]
|
||||
pub enum Hersteller {
|
||||
Table,
|
||||
Id,
|
||||
Name,
|
||||
}
|
|
@ -0,0 +1,58 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
|
||||
use super::{m20240602_100749_create_typ::Typ, m20240602_100754_create_hersteller::Hersteller};
|
||||
|
||||
#[derive(DeriveMigrationName)]
|
||||
pub struct Migration;
|
||||
|
||||
#[async_trait::async_trait]
|
||||
impl MigrationTrait for Migration {
|
||||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.create_table(
|
||||
Table::create()
|
||||
.table(Modell::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Modell::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Modell::Name).string().not_null())
|
||||
.col(ColumnDef::new(Modell::TypId).integer().not_null())
|
||||
.col(ColumnDef::new(Modell::HerstellerId).integer().not_null())
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_typ")
|
||||
.from(Modell::Table, Modell::TypId)
|
||||
.to(Typ::Table, Typ::Id),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_hersteller")
|
||||
.from(Modell::Table, Modell::HerstellerId)
|
||||
.to(Hersteller::Table, Hersteller::Id),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(Modell::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Iden)]
|
||||
#[iden(rename = "modelle")]
|
||||
enum Modell {
|
||||
Table,
|
||||
Id,
|
||||
Name,
|
||||
TypId,
|
||||
HerstellerId,
|
||||
}
|
|
@ -0,0 +1,6 @@
|
|||
use sea_orm_migration::prelude::*;
|
||||
|
||||
#[async_std::main]
|
||||
async fn main() {
|
||||
cli::run_cli(migration::Migrator).await;
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[sea_orm(table_name = "hersteller")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::modelle::Entity")]
|
||||
Modelle,
|
||||
}
|
||||
|
||||
impl Related<super::modelle::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Modelle.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
|
@ -0,0 +1,7 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
|
||||
|
||||
pub mod prelude;
|
||||
|
||||
pub mod hersteller;
|
||||
pub mod modelle;
|
||||
pub mod typen;
|
|
@ -0,0 +1,47 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[sea_orm(table_name = "modelle")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub typ_id: i32,
|
||||
pub hersteller_id: i32,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(
|
||||
belongs_to = "super::hersteller::Entity",
|
||||
from = "Column::HerstellerId",
|
||||
to = "super::hersteller::Column::Id",
|
||||
on_update = "NoAction",
|
||||
on_delete = "NoAction"
|
||||
)]
|
||||
Hersteller,
|
||||
#[sea_orm(
|
||||
belongs_to = "super::typen::Entity",
|
||||
from = "Column::TypId",
|
||||
to = "super::typen::Column::Id",
|
||||
on_update = "NoAction",
|
||||
on_delete = "NoAction"
|
||||
)]
|
||||
Typen,
|
||||
}
|
||||
|
||||
impl Related<super::hersteller::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Hersteller.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl Related<super::typen::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Typen.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
|
@ -0,0 +1,5 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
|
||||
|
||||
pub use super::hersteller::Entity as Hersteller;
|
||||
pub use super::modelle::Entity as Modelle;
|
||||
pub use super::typen::Entity as Typen;
|
|
@ -0,0 +1,25 @@
|
|||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
|
||||
|
||||
use sea_orm::entity::prelude::*;
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
|
||||
#[sea_orm(table_name = "typen")]
|
||||
pub struct Model {
|
||||
#[sea_orm(primary_key)]
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
|
||||
pub enum Relation {
|
||||
#[sea_orm(has_many = "super::modelle::Entity")]
|
||||
Modelle,
|
||||
}
|
||||
|
||||
impl Related<super::modelle::Entity> for Entity {
|
||||
fn to() -> RelationDef {
|
||||
Relation::Modelle.def()
|
||||
}
|
||||
}
|
||||
|
||||
impl ActiveModelBehavior for ActiveModel {}
|
|
@ -0,0 +1,41 @@
|
|||
mod entities;
|
||||
use entities::{prelude::*, *};
|
||||
use futures::executor::block_on;
|
||||
// use sea_orm::{ConnectionTrait, Database, DbBackend, DbErr, Statement};
|
||||
use sea_orm::*;
|
||||
|
||||
const DATABASE_URL: &str = "postgres://peter:peter@192.168.188.249:5432";
|
||||
const DB_NAME: &str = "idb";
|
||||
|
||||
async fn run() -> Result<(), DbErr> {
|
||||
// let db = Database::connect(DATABASE_URL).await?;
|
||||
//
|
||||
// db.execute(Statement::from_string(
|
||||
// db.get_database_backend(),
|
||||
// format!("DROP DATABASE IF EXISTS \"{}\";", DB_NAME),
|
||||
// ))
|
||||
// .await?;
|
||||
// db.execute(Statement::from_string(
|
||||
// db.get_database_backend(),
|
||||
// format!("CREATE DATABASE \"{}\";", DB_NAME),
|
||||
// ))
|
||||
// .await?;
|
||||
|
||||
let url = format!("{}/{}", DATABASE_URL, DB_NAME);
|
||||
let db = Database::connect(&url).await?;
|
||||
|
||||
let alle_typen = Typen::find().into_json().all(&db).await?;
|
||||
let alle_hersteller = Hersteller::find().into_json().all(&db).await?;
|
||||
let alle_modelle = Modelle::find().into_json().all(&db).await?;
|
||||
|
||||
println!("{:?}", alle_typen);
|
||||
println!("{:?}", alle_hersteller);
|
||||
println!("{:?}", alle_modelle);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
if let Err(err) = block_on(run()) {
|
||||
panic!("{}", err);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue