Skip to content

Commit

Permalink
Refreshed migration schema definition (SeaQL/sea-orm#2099)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Apr 10, 2024
1 parent 858ab8d commit 85a29dd
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions SeaORM/docs/03-migration/02-writing-migration.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ Here are some common DDL snippets you may find useful.
Story,
}

// Remember to import `sea_orm_migration::schema::*` schema helpers into scope
use sea_orm_migration::{prelude::*, schema::*};

// Defining the schema with helpers
manager
.create_table(
Table::create()
.table(Post::Table)
.if_not_exists()
.col(pk_auto(Post::Id))
.col(string(Post::Title))
.col(string(Post::Text))
.col(enumeration_null(Post::Category, Alias::new("category"), Category::iter()))
)
.await

// Or, you can define the schema without the helpers
manager
.create_table(
Table::create()
Expand All @@ -136,7 +153,6 @@ Here are some common DDL snippets you may find useful.
ColumnDef::new(Post::Category)
.enumeration(Alias::new("category"), Category::iter()),
)
.to_owned(),
)
.await
```
Expand Down Expand Up @@ -167,7 +183,6 @@ Here are some common DDL snippets you may find useful.
Type::create()
.as_enum(CategoryEnum)
.values(CategoryVariants::iter())
.to_owned(),
)
.await?;
```
Expand Down Expand Up @@ -275,23 +290,19 @@ impl MigrationTrait for Migration {
You can combine multiple changes within both up and down migration functions. Here is a complete example:

```rust
// Remember to import `sea_orm_migration::schema::*` schema helpers into scope
use sea_orm_migration::{prelude::*, schema::*};

async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {

manager
.create_table(
sea_query::Table::create()
.table(Post::Table)
.if_not_exists()
.col(
ColumnDef::new(Post::Id)
.integer()
.not_null()
.auto_increment()
.primary_key()
)
.col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Post::Text).string().not_null())
.to_owned()
.col(pk_auto(Post::Id))
.col(string(Post::Title))
.col(string(Post::Text))
)
.await?;

Expand All @@ -302,7 +313,6 @@ async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
.name("idx-post_title")
.table(Post::Table)
.col(Post::Title)
.to_owned(),
)
.await?;

Expand All @@ -315,10 +325,10 @@ and here we have the matching down function:
```rust
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {

manager.drop_index(Index::drop().name("idx-post-title").to_owned())
manager.drop_index(Index::drop().name("idx-post-title"))
.await?;

manager.drop_table(Table::drop().table(Post::Table).to_owned())
manager.drop_table(Table::drop().table(Post::Table))
.await?;

Ok(()) // All good!
Expand Down

0 comments on commit 85a29dd

Please sign in to comment.