-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from fankaiLiu/db
add sea_orm template
- Loading branch information
Showing
22 changed files
with
548 additions
and
106 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "salvo-cli" | ||
version = "0.1.8" | ||
version = "0.1.9" | ||
edition = "2021" | ||
authors = ["Fankai Liu [email protected]"] | ||
keywords = ["salvo", "cli","template"] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,10 @@ | ||
{{#if is_sqlx}} | ||
{{create_success_mysql_or_pgsql_fist_use}} | ||
{{/if}} | ||
{{#if is_sea_orm}} | ||
{{create_success_sea_orm__mysql_or_pgsql_install_sea_orm}} | ||
{{create_success_sea_orm__mysql_or_pgsql_fist_use}} | ||
{{/if}} | ||
BEGIN; | ||
INSERT INTO "users" ("id", "username", "password") VALUES ('cdd0e080-5bb1-4442-b6f7-2ba60dbd0555', 'zhangsan', '$argon2id$v=19$m=19456,t=2,p=1$rcosL5pOPdA2c7i4ZuLA4Q$s0JGh78UzMmu1qZMpVUA3b8kWYLXcZhw7uBfwhYDJ4A'); | ||
COMMIT; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
[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.11.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 | ||
{{#if is_postgres}} | ||
"sqlx-postgres", # `DATABASE_DRIVER` feature | ||
{{/if}} | ||
{{#if is_sqlite}} | ||
"sqlx-sqlite" # `DATABASE_DRIVER` feature | ||
{{/if}} | ||
{{#if is_mysql}} | ||
"sqlx-mysql", # `DATABASE_DRIVER` feature | ||
{{/if}} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -- migrate 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 | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] | ||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/template/migration/src/m20220101_000001_create_table.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
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(User::Table) | ||
.if_not_exists() | ||
.col( | ||
ColumnDef::new(User::Id) | ||
.string() | ||
.not_null() | ||
.primary_key(), | ||
) | ||
.col( | ||
ColumnDef::new(User::Username) | ||
.string() | ||
.not_null() | ||
.unique_key(), | ||
) | ||
.col(ColumnDef::new(User::Password).string().not_null()) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.drop_table(Table::drop().table(User::Table).to_owned()) | ||
.await | ||
} | ||
} | ||
|
||
#[derive(Iden)] | ||
enum User { | ||
Table, | ||
Id, | ||
Username, | ||
Password, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[async_std::main] | ||
async fn main() { | ||
cli::run_cli(migration::Migrator).await; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
{{#if is_sqlx}} | ||
pub mod user; | ||
{{/if}} | ||
{{#if is_sea_orm}} | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 | ||
|
||
pub mod prelude; | ||
|
||
pub mod user; | ||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 | ||
|
||
pub use super::user::Entity as User; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
{{#if is_sqlx}} | ||
use serde::Serialize; | ||
use sqlx::FromRow; | ||
|
||
#[derive(FromRow, Serialize, Debug)] | ||
pub struct User { | ||
pub id: String, | ||
pub username: String, | ||
pub password: String, | ||
} | ||
{{/if}} | ||
{{#if is_sea_orm}} | ||
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.11.3 | ||
|
||
use sea_orm::entity::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)] | ||
#[sea_orm(table_name = "user")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub id: String, | ||
#[sea_orm(unique)] | ||
pub username: String, | ||
pub password: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} | ||
{{/if}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.