+
Skip to content

Migration #335

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

Merged
merged 21 commits into from
Feb 5, 2022
Merged
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
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ once_cell = "1.8"
[dev-dependencies]
smol = { version = "^1.2" }
smol-potat = { version = "^1.1" }
async-std = { version = "^1.9", features = ["attributes"] }
async-std = { version = "^1.9", features = ["attributes", "tokio1"] }
tokio = { version = "^1.6", features = ["full"] }
actix-rt = { version = "2.2.0" }
maplit = { version = "^1" }
Expand Down
2 changes: 1 addition & 1 deletion examples/basic/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
publish = false

[dependencies]
async-std = { version = "^1.9", features = [ "attributes" ] }
async-std = { version = "^1.9", features = [ "attributes", "tokio1" ] }
sea-orm = { path = "../../", features = [ "sqlx-all", "runtime-async-std-native-tls" ] }
serde_json = { version = "^1" }
futures = { version = "^0.3" }
Expand Down
14 changes: 3 additions & 11 deletions examples/rocket_example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
publish = false

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

[dependencies]
async-stream = { version = "^0.3" }
Expand All @@ -19,18 +20,9 @@ rocket_dyn_templates = { version = "0.1.0-rc.1", features = [
"tera",
] }
serde_json = { version = "^1" }

[dependencies.sea-orm]
path = "../../" # remove this line in your own project
version = "^0.5.0"
features = ["macros", "runtime-tokio-native-tls"]
default-features = false
entity = { path = "entity" }
migration = { path = "migration" }

[dependencies.sea-orm-rocket]
path = "../../sea-orm-rocket/lib" # remove this line in your own project and use the git line
# git = "https://github.com/SeaQL/sea-orm"

[features]
default = ["sqlx-postgres"]
sqlx-mysql = ["sea-orm/sqlx-mysql"]
sqlx-postgres = ["sea-orm/sqlx-postgres"]
2 changes: 1 addition & 1 deletion examples/rocket_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

1. Modify the `url` var in `Rocket.toml` to point to your chosen database

1. Turn on the appropriate database feature for your chosen db in `Cargo.toml` (the `default = ["sqlx-postgres"]` line)
1. Turn on the appropriate database feature for your chosen db in `entity/Cargo.toml` (the `"sqlx-postgres",` line)

1. `cargo run` to start the server

Expand Down
27 changes: 27 additions & 0 deletions examples/rocket_example/entity/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "entity"
version = "0.1.0"
edition = "2021"
publish = false

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

[dependencies]
rocket = { version = "0.5.0-rc.1", features = [
"json",
] }

[dependencies.sea-orm]
# path = "../../../" # remove this line in your own project
git = "https://github.com/SeaQL/sea-orm"
branch = "migration"
version = "^0.5.0"
features = [
"macros",
"runtime-tokio-native-tls",
"sqlx-postgres",
# "sqlx-mysql",
]
default-features = false
6 changes: 6 additions & 0 deletions examples/rocket_example/entity/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#[macro_use]
extern crate rocket;

pub mod post;

pub use sea_orm;
14 changes: 14 additions & 0 deletions examples/rocket_example/migration/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "migration"
version = "0.1.0"
edition = "2021"
publish = false

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

[dependencies]
sea-schema = { git = "https://github.com/SeaQL/sea-schema.git", branch = "restructure-sea-query-dep", default-features = false, features = [ "migration", "debug-print" ] }
entity = { path = "../entity" }
rocket = { version = "0.5.0-rc.1" }
37 changes: 37 additions & 0 deletions examples/rocket_example/migration/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Running Migrator CLI

- 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 examples/rocket_example/migration/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub use sea_schema::migration::*;

mod m20220120_000001_create_post_table;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220120_000001_create_post_table::Migration)]
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use entity::post::*;
use sea_schema::migration::{
sea_query::{self, *},
*,
};

pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m20220120_000001_create_post_table"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Entity)
.if_not_exists()
.col(
ColumnDef::new(Column::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Column::Title).string().not_null())
.col(ColumnDef::new(Column::Text).string().not_null())
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Entity).to_owned())
.await
}
}
18 changes: 18 additions & 0 deletions examples/rocket_example/migration/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use migration::Migrator;
use sea_schema::migration::*;

#[async_std::main]
async fn main() {
// Setting `DATABASE_URL` environment variable
let key = "DATABASE_URL";
if std::env::var(key).is_err() {
// Getting the database URL from Rocket.toml if it's not set
let figment = rocket::Config::figment();
let database_url: String = figment
.extract_inner("databases.sea_orm.url")
.expect("Cannot find Database URL in Rocket.toml");
std::env::set_var(key, database_url);
}

cli::run_cli(Migrator).await;
}
16 changes: 8 additions & 8 deletions examples/rocket_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ use rocket::{Build, Request, Rocket};
use rocket_dyn_templates::Template;
use serde_json::json;

use entity::sea_orm;
use migration::MigratorTrait;
use sea_orm::{entity::*, query::*};
use sea_orm_rocket::{Connection, Database};

mod pool;
use pool::Db;

mod setup;

mod post;
pub use post::Entity as Post;
pub use entity::post;
pub use entity::post::Entity as Post;

const DEFAULT_POSTS_PER_PAGE: usize = 5;

Expand Down Expand Up @@ -114,7 +114,7 @@ async fn list(
"num_pages": num_pages,
"posts": posts,
"flash": flash.map(FlashMessage::into_inner),
})
}),
)
}

Expand All @@ -131,7 +131,7 @@ async fn edit(conn: Connection<'_, Db>, id: i32) -> Template {
"edit",
json! ({
"post": post,
})
}),
)
}

Expand Down Expand Up @@ -160,13 +160,13 @@ pub fn not_found(req: &Request<'_>) -> Template {
"error/404",
json! ({
"uri": req.uri()
})
}),
)
}

async fn run_migrations(rocket: Rocket<Build>) -> fairing::Result {
let conn = &Db::fetch(&rocket).unwrap().conn;
let _ = setup::create_post_table(conn).await;
let _ = migration::Migrator::up(conn, None).await;
Ok(rocket)
}

Expand Down
1 change: 1 addition & 0 deletions examples/rocket_example/src/pool.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use async_trait::async_trait;
use entity::sea_orm;
use sea_orm::ConnectOptions;
use sea_orm_rocket::{rocket::figment::Figment, Config, Database};
use std::time::Duration;
Expand Down
33 changes: 0 additions & 33 deletions examples/rocket_example/src/setup.rs

This file was deleted.

2 changes: 1 addition & 1 deletion issues/262/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ publish = false

[dependencies]
sea-orm = { path = "../../", features = [ "sqlx-all", "runtime-async-std-native-tls", "debug-print" ] }
async-std = { version = "^1", features = ["attributes"] }
async-std = { version = "^1", features = ["attributes", "tokio1"] }
2 changes: 1 addition & 1 deletion issues/319/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
publish = false

[dependencies]
async-std = { version = "^1", features = ["attributes"] }
async-std = { version = "^1", features = ["attributes", "tokio1"] }
serde = { version = "^1", features = ["derive"] }
sea-orm = { path = "../../", features = [
"sqlx-mysql",
Expand Down
5 changes: 3 additions & 2 deletions sea-orm-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ path = "src/main.rs"
[dependencies]
clap = { version = "^2.33.3" }
dotenv = { version = "^0.15" }
async-std = { version = "^1.9", features = [ "attributes" ] }
async-std = { version = "^1.9", features = [ "attributes", "tokio1" ] }
sea-orm-codegen = { version = "^0.5.0", path = "../sea-orm-codegen" }
sea-schema = { version = "0.4.0", default-features = false, features = [
sea-schema = { git = "https://github.com/SeaQL/sea-schema.git", branch = "restructure-sea-query-dep", default-features = false, features = [
"debug-print",
"sqlx-mysql",
"sqlx-sqlite",
"sqlx-postgres",
"discovery",
"writer",
"migration",
] }
sqlx = { version = "^0.5", default-features = false, features = [ "mysql", "postgres" ] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
Expand Down
Loading
点击 这是indexloc提供的php浏览器服务,不要输入任何密码和下载