Skip to content

Commit a455ce5

Browse files
committed
update
1 parent a6f5f4d commit a455ce5

6 files changed

Lines changed: 257 additions & 193 deletions

File tree

docs/README.md

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,25 @@ cargo install --path .
3939
`qop` is configured using a `qop.toml` file. Here is an example for PostgreSQL:
4040

4141
```toml
42-
[backend]
43-
type = "postgres"
44-
host = "localhost"
45-
port = 5432
46-
username = "postgres"
47-
password = "password"
48-
database = "postgres"
42+
[backend.postgres]
43+
connection = { static = "postgresql://postgres:password@localhost:5432/postgres" }
4944
schema = "public"
5045
table = "migrations"
46+
47+
[backend.postgres.migrations]
48+
timeout = 30
49+
```
50+
51+
You can also use environment variables for the connection string:
52+
53+
```toml
54+
[backend.postgres]
55+
connection = { from_env = "DATABASE_URL" }
56+
schema = "public"
57+
table = "migrations"
58+
59+
[backend.postgres.migrations]
60+
timeout = 30
5161
```
5262
5363
The migration files are expected to be in a directory relative to the `qop.toml` file.
@@ -139,23 +149,27 @@ qop migration list --path path/to/your/qop.toml
139149
**Arguments:**
140150
* `-p, --path <PATH>`: Path to the `qop.toml` configuration file. (default: `qop.toml`)
141151
142-
#### `qop migration sync`
152+
#### `qop migration history`
153+
154+
Manages migration history with commands for syncing and fixing migration order.
155+
156+
##### `qop migration history sync`
143157
144158
Upserts all remote migrations locally. This is useful for syncing migrations across multiple developers.
145159
146160
```bash
147-
qop migration sync --path path/to/your/qop.toml
161+
qop migration history sync --path path/to/your/qop.toml
148162
```
149163
150164
**Arguments:**
151165
* `-p, --path <PATH>`: Path to the `qop.toml` configuration file. (default: `qop.toml`)
152166
153-
#### `qop migration fix`
167+
##### `qop migration history fix`
154168
155169
Shuffles all non-run local migrations to the end of the chain. This is useful when you have created migrations out of order.
156170
157171
```bash
158-
qop migration fix --path path/to/your/qop.toml
172+
qop migration history fix --path path/to/your/qop.toml
159173
```
160174
161175
**Arguments:**

example/id=1753311846662/up.sql

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1 @@
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-
);
1+
-- SQL goes here

example/qop.toml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
[backend.postgres]
22
table = "__qop"
33
schema = "public"
4-
host = "localhost"
5-
port = 5432
6-
database = "postgres"
7-
username = "postgres"
8-
password = "postgres"
4+
[backend.postgres.connection]
5+
from_env = "QOP_BACKEND_POSTGRES_CONNECTION"
6+
[backend.postgres.migrations]
7+
timeout = 60

src/args.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,13 @@ pub(crate) enum MigrationApply {
5353
},
5454
}
5555

56+
#[derive(Debug)]
57+
pub(crate) enum HistoryCommand {
58+
Sync,
59+
Fix,
60+
}
61+
62+
5663
#[derive(Debug)]
5764
pub(crate) enum MigrationCommand {
5865
Init,
@@ -70,8 +77,7 @@ pub(crate) enum MigrationCommand {
7077
},
7178
Apply(MigrationApply),
7279
List,
73-
Sync,
74-
Fix,
80+
History(HistoryCommand),
7581
Diff,
7682
}
7783

@@ -183,12 +189,17 @@ impl ClapArgumentLoader {
183189
.about("Lists all applied migrations."),
184190
)
185191
.subcommand(
186-
clap::Command::new("sync")
187-
.about("Upserts all remote migrations locally."),
188-
)
189-
.subcommand(
190-
clap::Command::new("fix")
191-
.about("Shuffles all non-run local migrations to the end of the chain."),
192+
clap::Command::new("history")
193+
.about("Manages migration history.")
194+
.subcommand_required(true)
195+
.subcommand(
196+
clap::Command::new("sync")
197+
.about("Upserts all remote migrations locally."),
198+
)
199+
.subcommand(
200+
clap::Command::new("fix")
201+
.about("Shuffles all non-run local migrations to the end of the chain."),
202+
),
192203
)
193204
.subcommand(
194205
clap::Command::new("diff")
@@ -263,10 +274,15 @@ impl ClapArgumentLoader {
263274
}
264275
} else if let Some(_) = subc.subcommand_matches("list") {
265276
MigrationCommand::List
266-
} else if let Some(_) = subc.subcommand_matches("sync") {
267-
MigrationCommand::Sync
268-
} else if let Some(_) = subc.subcommand_matches("fix") {
269-
MigrationCommand::Fix
277+
} else if let Some(history_subc) = subc.subcommand_matches("history") {
278+
let history_cmd = if let Some(_) = history_subc.subcommand_matches("sync") {
279+
HistoryCommand::Sync
280+
} else if let Some(_) = history_subc.subcommand_matches("fix") {
281+
HistoryCommand::Fix
282+
} else {
283+
unreachable!();
284+
};
285+
MigrationCommand::History(history_cmd)
270286
} else if let Some(_) = subc.subcommand_matches("diff") {
271287
MigrationCommand::Diff
272288
} else if let Some(apply_subc) = subc.subcommand_matches("apply") {

src/config.rs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,32 @@
11
use serde::{Deserialize, Serialize};
2+
use serde::de::DeserializeOwned;
23

34
#[derive(Debug, Serialize, Deserialize)]
5+
#[serde(rename_all = "snake_case")]
46
pub struct Config {
57
pub backend: Backend,
68
}
79

810
#[derive(Debug, Serialize, Deserialize)]
11+
#[serde(rename_all = "snake_case")]
912
pub struct PostgresMigrations {
1013
pub timeout: Option<u64>,
1114
}
1215

16+
1317
#[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,
18+
#[serde(rename_all = "snake_case")]
19+
#[serde(bound(serialize = "T: Serialize", deserialize = "T: DeserializeOwned"))]
20+
pub enum DataSource<T: Serialize + DeserializeOwned> {
21+
Static(T),
22+
FromEnv(String),
2023
}
2124

2225
#[derive(Debug, Serialize, Deserialize)]
2326
#[serde(rename_all = "snake_case")]
2427
pub enum Backend {
2528
Postgres {
26-
connection: PostgresConnection,
29+
connection: DataSource<String>,
2730
migrations: PostgresMigrations,
2831
schema: String,
2932
table: String,

0 commit comments

Comments
 (0)