Skip to content

Commit

Permalink
fix: codegen generating migrations that apply backwards (#952)
Browse files Browse the repository at this point in the history
* fix: codegen generating migrations that apply backwards

BREAKING CHANGE: requires inject comment in `migration/src/lib.rs` to now be `inject-above`

* Update lib.rs to be nonbreaking


---------

Co-authored-by: Elad Kaplan <[email protected]>
Co-authored-by: Dotan J. Nahum <[email protected]>
  • Loading branch information
3 people authored Nov 9, 2024
1 parent 2b34485 commit 52cf0b6
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/demo/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 1 addition & 3 deletions examples/demo/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@ pub use sea_orm_migration::prelude::*;

mod m20220101_000001_users;
mod m20231103_114510_notes;

mod m20240416_071825_roles;

mod m20240416_082115_users_roles;
pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
// inject-below (do not remove this comment)
Box::new(m20220101_000001_users::Migration),
Box::new(m20231103_114510_notes::Migration),
Box::new(m20240416_071825_roles::Migration),
Box::new(m20240416_082115_users_roles::Migration),
// inject-above (do not remove this comment)
]
}
}
3 changes: 2 additions & 1 deletion examples/demo/src/controllers/cache.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::models::users;
use loco_rs::prelude::*;
use serde::{Deserialize, Serialize};

use crate::models::users;
#[derive(Serialize, Deserialize)]
pub struct CacheResponse {
value: Option<String>,
Expand Down
2 changes: 1 addition & 1 deletion loco-gen/src/templates/migration.t
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ skip_glob: "migration/src/*_{{mig_name}}.rs"
message: "Migration for `{{name}}` added! You can now apply it with `$ cargo loco db migrate`."
injections:
- into: "migration/src/lib.rs"
after: "inject-below"
before: "inject-above"
content: " Box::new({{module_name}}::Migration),"
- into: "migration/src/lib.rs"
before: "pub struct Migrator"
Expand Down
2 changes: 1 addition & 1 deletion loco-gen/src/templates/model.t
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ skip_glob: "migration/src/m????????_??????_{{plural_snake}}.rs"
message: "Migration for `{{name}}` added! You can now apply it with `$ cargo loco db migrate`."
injections:
- into: "migration/src/lib.rs"
after: "inject-below"
before: "inject-above"
content: " Box::new({{module_name}}::Migration),"
- into: "migration/src/lib.rs"
before: "pub struct Migrator"
Expand Down
3 changes: 2 additions & 1 deletion starters/rest-api/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pub struct Migrator;
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
// inject-below
// inject-below (do not remove this comment)
Box::new(m20220101_000001_users::Migration),
// inject-above (do not remove this comment)
]
}
}
3 changes: 2 additions & 1 deletion starters/saas/migration/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ pub struct Migrator;
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
// inject-below
// inject-below (do not remove this comment)
Box::new(m20220101_000001_users::Migration),
// inject-above (do not remove this comment)
]
}
}

0 comments on commit 52cf0b6

Please sign in to comment.