Skip to content

Commit a6f5f4d

Browse files
committed
update
1 parent 835fdf3 commit a6f5f4d

7 files changed

Lines changed: 676 additions & 34 deletions

File tree

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ toml = "0.9.2"
4040
sqlx = { version = "0.8.6", features = ["runtime-tokio", "postgres", "chrono"]}
4141
comfy-table = "7.1.1"
4242
path-clean = "1.0.1"
43+
sqlparser = "0.49"
4344

4445
[dev-dependencies]
4546
hoox = "0.3.0"

example/id=1753311846662/up.sql

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,13 @@
1-
-- SQL goes here
1+
ALTER TABLE "ASD" ADD COLUMN "ASD" TEXT FOREIGN KEY REFERENCES "X"("Y");
2+
DROP TABLE "boosker";-- This is a header comment
3+
ALTER TABLE "users" ADD COLUMN "email" TEXT; -- inline comment
4+
5+
/*
6+
Multi-line comment
7+
*/
8+
DROP TABLE "old_table";
9+
10+
CREATE TABLE "new_table" (
11+
id SERIAL PRIMARY KEY, -- column comment
12+
name VARCHAR(255) /* another inline comment */
13+
);

src/args.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ impl CallArgs {
3030
}
3131

3232
match &self.command {
33+
| Command::Migration(Migration { command: MigrationCommand::Diff, .. }) => anyhow::bail!("diff is experimental"),
34+
| Command::Migration(Migration { command: MigrationCommand::Up { diff: true, .. }, .. }) => anyhow::bail!("diff is experimental"),
35+
| Command::Migration(Migration { command: MigrationCommand::Down { diff: true, .. }, .. }) => anyhow::bail!("diff is experimental"),
3336
| _ => (),
3437
}
3538

@@ -57,16 +60,19 @@ pub(crate) enum MigrationCommand {
5760
Up {
5861
timeout: Option<u64>,
5962
count: Option<usize>,
63+
diff: bool,
6064
},
6165
Down {
6266
timeout: Option<u64>,
6367
count: Option<usize>,
6468
remote: bool,
69+
diff: bool,
6570
},
6671
Apply(MigrationApply),
6772
List,
6873
Sync,
6974
Fix,
75+
Diff,
7076
}
7177

7278
#[derive(Debug)]
@@ -161,14 +167,16 @@ impl ClapArgumentLoader {
161167
clap::Command::new("up")
162168
.about("Runs the migrations.")
163169
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
164-
.arg(clap::Arg::new("count").short('c').long("count").required(false)),
170+
.arg(clap::Arg::new("count").short('c').long("count").required(false))
171+
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying")),
165172
)
166173
.subcommand(
167174
clap::Command::new("down")
168175
.about("Rolls back the migrations.")
169176
.arg(clap::Arg::new("timeout").short('t').long("timeout").required(false))
170177
.arg(clap::Arg::new("remote").short('r').long("remote").required(false).num_args(0))
171-
.arg(clap::Arg::new("count").short('c').long("count").required(false)),
178+
.arg(clap::Arg::new("count").short('c').long("count").required(false))
179+
.arg(clap::Arg::new("diff").short('d').long("diff").required(false).num_args(0).help("Show migration diff before applying")),
172180
)
173181
.subcommand(
174182
clap::Command::new("list")
@@ -182,6 +190,10 @@ impl ClapArgumentLoader {
182190
clap::Command::new("fix")
183191
.about("Shuffles all non-run local migrations to the end of the chain."),
184192
)
193+
.subcommand(
194+
clap::Command::new("diff")
195+
.about("Shows pending migration operations without applying them."),
196+
)
185197
.subcommand(
186198
clap::Command::new("apply")
187199
.about("Applies or reverts a specific migration by ID.")
@@ -240,19 +252,23 @@ impl ClapArgumentLoader {
240252
MigrationCommand::Up {
241253
timeout: up_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
242254
count: up_subc.get_one::<String>("count").map(|s| s.parse::<usize>().unwrap()),
255+
diff: up_subc.get_flag("diff"),
243256
}
244257
} else if let Some(down_subc) = subc.subcommand_matches("down") {
245258
MigrationCommand::Down {
246259
timeout: down_subc.get_one::<String>("timeout").map(|s| s.parse::<u64>().unwrap()),
247260
count: down_subc.get_one::<String>("count").map(|s| s.parse::<usize>().unwrap()),
248261
remote: down_subc.get_flag("remote"),
262+
diff: down_subc.get_flag("diff"),
249263
}
250264
} else if let Some(_) = subc.subcommand_matches("list") {
251265
MigrationCommand::List
252266
} else if let Some(_) = subc.subcommand_matches("sync") {
253267
MigrationCommand::Sync
254268
} else if let Some(_) = subc.subcommand_matches("fix") {
255269
MigrationCommand::Fix
270+
} else if let Some(_) = subc.subcommand_matches("diff") {
271+
MigrationCommand::Diff
256272
} else if let Some(apply_subc) = subc.subcommand_matches("apply") {
257273
if let Some(up_subc) = apply_subc.subcommand_matches("up") {
258274
MigrationCommand::Apply(MigrationApply::Up {

src/config.rs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,26 @@ pub struct Config {
55
pub backend: Backend,
66
}
77

8+
#[derive(Debug, Serialize, Deserialize)]
9+
pub struct PostgresMigrations {
10+
pub timeout: Option<u64>,
11+
}
12+
13+
#[derive(Debug, Serialize, Deserialize)]
14+
pub struct PostgresConnection {
15+
pub host: String,
16+
pub port: u16,
17+
pub username: Option<String>,
18+
pub password: Option<String>,
19+
pub database: String,
20+
}
21+
822
#[derive(Debug, Serialize, Deserialize)]
923
#[serde(rename_all = "snake_case")]
1024
pub enum Backend {
1125
Postgres {
12-
host: String,
13-
port: u16,
14-
username: Option<String>,
15-
password: Option<String>,
16-
database: String,
26+
connection: PostgresConnection,
27+
migrations: PostgresMigrations,
1728
schema: String,
1829
table: String,
1930
},

0 commit comments

Comments
 (0)