Skip to content

Commit ed4cbd4

Browse files
committed
test(cli): add github ci for rollback migration
1 parent 5acfb2b commit ed4cbd4

File tree

2 files changed

+77
-5
lines changed

2 files changed

+77
-5
lines changed

.github/workflows/ci.yml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,60 @@ jobs:
120120
cd my-app
121121
cargo test --all
122122
123+
rollback-generated-full:
124+
name: "Test rollback on generated full example app"
125+
runs-on: ubuntu-latest
126+
needs: generate-full
127+
128+
services:
129+
postgres:
130+
image: postgres
131+
env:
132+
POSTGRES_DB: my_app
133+
POSTGRES_USER: my_app
134+
POSTGRES_PASSWORD: my_app
135+
options: >-
136+
--health-cmd pg_isready
137+
--health-interval 10s
138+
--health-timeout 5s
139+
--health-retries 5
140+
ports:
141+
- 5432:5432
142+
143+
steps:
144+
- uses: actions/checkout@v5
145+
- uses: actions-rust-lang/setup-rust-toolchain@v1
146+
147+
- uses: actions/download-artifact@3e5f45b2cfb9172054b4087a40e8e0b5a5461e7c # v8
148+
with:
149+
name: my-app-full
150+
path: my-app
151+
152+
- name: migrate
153+
run: |
154+
cd my-app
155+
cargo db reset
156+
157+
- name: test rollback without args
158+
run: |
159+
cd my-app
160+
cargo db rollback
161+
! cargo test --all
162+
- name: test rollback last 1
163+
run: |
164+
cd my-app
165+
cargo db migrate
166+
cargo test --all
167+
cargo db rollback --steps 2
168+
! cargo test --all
169+
- name: test rollback to tasks
170+
run: |
171+
cd my-app
172+
cargo db migrate
173+
cargo test --all
174+
cargo db rollback --to create_tasks_table
175+
! cargo test --all
176+
123177
fmt-generated-full:
124178
name: "Validate format of generated full example app"
125179
runs-on: ubuntu-latest
@@ -468,6 +522,7 @@ jobs:
468522
run: |
469523
cd my-app
470524
cargo generate migration CreatePeopleTable
525+
cargo generate migration AddIndexToPeople --simple
471526
472527
- name: generate-entity
473528
run: |

blueprint/cli/src/bin/generate.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ enum Commands {
8181
Migration {
8282
#[arg(help = "The name of the migration.")]
8383
name: String,
84+
85+
#[arg(long, help = "Generate a simple (non-reversible) migration as a single .sql file.")]
86+
simple: bool,
8487
},
8588
#[command(about = "Generate an entity")]
8689
Entity {
@@ -139,12 +142,18 @@ fn cli(ui: &mut UI<'_>, cli: Cli) -> Result<(), anyhow::Error> {
139142
Ok(())
140143
}
141144
{% if template_type != "minimal" -%}
142-
Commands::Migration { name } => {
145+
Commands::Migration { name, simple } => {
143146
ui.info("Generating migration…");
144-
let (up_file_name, down_file_name) = generate_migration(&name, cli.r#override)
145-
.context("Could not generate migration!")?;
146-
ui.success(&format!("Generated migration {}.", up_file_name.display()));
147-
ui.success(&format!("Generated migration {}.", down_file_name.display()));
147+
if simple {
148+
let file_name = generate_simple_migration(&name, cli.r#override)
149+
.context("Could not generate migration!")?;
150+
ui.success(&format!("Generated migration {}.", file_name.display()));
151+
} else {
152+
let (up_file_name, down_file_name) = generate_migration(&name, cli.r#override)
153+
.context("Could not generate migration!")?;
154+
ui.success(&format!("Generated migration {}.", up_file_name.display()));
155+
ui.success(&format!("Generated migration {}.", down_file_name.display()));
156+
}
148157
Ok(())
149158
}
150159
Commands::Entity { name, fields } => {
@@ -263,6 +272,14 @@ fn generate_controller_test(name: &str, r#override: bool) -> Result<String, anyh
263272
}
264273

265274
{% if template_type != "minimal" -%}
275+
fn generate_simple_migration(name: &str, r#override: bool) -> Result<PathBuf, anyhow::Error> {
276+
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
277+
let file_path = PathBuf::from(format!("./db/migrations/{}_{name}.sql", timestamp.as_secs()));
278+
create_project_file(file_path.to_str().expect("Invalid file path for migration!"), "".as_bytes(), r#override)?;
279+
280+
Ok(file_path)
281+
}
282+
266283
fn generate_migration(name: &str, r#override: bool) -> Result<(PathBuf, PathBuf), anyhow::Error> {
267284
let timestamp = SystemTime::now().duration_since(SystemTime::UNIX_EPOCH)?;
268285
let dir_path = PathBuf::from(&format!("./db/migrations/{}__{name}", timestamp.as_secs()));

0 commit comments

Comments
 (0)