Skip to content

Commit 7f08eff

Browse files
committed
Merge branch 'dev'
2 parents 03dee87 + 24f5ec2 commit 7f08eff

File tree

13 files changed

+304
-296
lines changed

13 files changed

+304
-296
lines changed

Cargo.toml

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ members = [
77

88
[package]
99
name = "rorm-cli"
10-
version = "0.5.0"
10+
version = "0.6.0"
1111
edition = "2021"
1212
authors = ["myOmikron <[email protected]>"]
1313
repository = "https://github.com/rorm-orm/rorm-cli"
@@ -18,60 +18,54 @@ documentation = "https://docs.rorm.rs"
1818
license = "MIT"
1919
description = "The cli tool for managing rorm applications."
2020

21+
[lib]
22+
path = "src/lib.rs"
23+
name = "rorm_cli"
24+
25+
[[bin]]
26+
name = "rorm-cli"
27+
path = "src/main.rs"
28+
2129
[dependencies]
2230
# Internal dependencies
2331
rorm-declaration = { version = "~0.3", path = "rorm-declaration" }
24-
rorm-sql = { version = "~0.5", path = "rorm-sql" }
32+
rorm-sql = { version = "~0.5", path = "rorm-sql", features = ["sqlite", "mysql", "postgres"] }
2533

2634
# CLI parsing tool
27-
clap = { version = "~4.0" }
35+
clap = { version = "~4", features = ["derive"] }
2836

2937
# Serialization library
30-
serde = { version = "~1.0" }
31-
serde_json = { version = "~1.0" }
32-
toml = { version = "~0.5" }
38+
serde = { version = "~1", features = ["derive"] }
39+
serde_json = { version = "~1" }
40+
toml = { version = "~0.7" }
3341

3442
# Generic error package
35-
anyhow = { version = "~1.0" }
43+
anyhow = { version = "~1" }
3644

3745
# Regex
38-
regex = { version = "~1.7" }
46+
regex = { version = "~1" }
3947

4048
# Lazy implementation
41-
once_cell = { version = "~1.16" }
49+
once_cell = { version = "~1" }
4250

4351
# SQL database abstraction
44-
sqlx = { version = "~0.6" }
52+
sqlx = { version = "~0.6", features = ["mysql", "postgres", "sqlite", "any", "chrono", "runtime-tokio-rustls"] }
4553

4654
# Runtime
47-
tokio = { version = "~1.23" }
55+
tokio = { version = ">=1.23.1", features = ["macros"] }
4856

4957
# Enum simplification
5058
strum = { version = "~0.24" }
5159

5260
# Read passwords from cli
53-
rpassword = { version = "~7.2" }
61+
rpassword = { version = "~7" }
5462

5563
[dev-dependencies]
5664
# Creating and clearing temporary directories
5765
temp-dir = { version = "~0.1" }
5866

59-
[features]
60-
default = [
61-
"sqlx/runtime-tokio-rustls",
62-
"sqlx/mysql",
63-
"sqlx/sqlite",
64-
"sqlx/postgres",
65-
"sqlx/any",
66-
"sqlx/chrono",
67-
68-
"tokio/macros",
69-
70-
"serde/derive",
71-
72-
"clap/derive",
73-
74-
"rorm-sql/sqlite",
75-
"rorm-sql/postgres",
76-
"rorm-sql/mysql",
77-
]
67+
[profile.fat-lto]
68+
inherits = "release"
69+
opt-level = "s"
70+
lto = "fat"
71+
strip = true

changelog.txt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,2 @@
1-
Since 0.4.1
1+
Since 0.5.0
22
-----------
3-
4-
- Added init subcommand.

src/entry.rs

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
use clap::{ArgAction, Parser, Subcommand};
2+
3+
use crate::init::init;
4+
use crate::make_migrations::{run_make_migrations, MakeMigrationsOptions};
5+
use crate::migrate::{run_migrate, MigrateOptions};
6+
use crate::squash_migrations::squash_migrations;
7+
8+
#[derive(Subcommand)]
9+
pub enum InitDriver {
10+
#[clap(about = "Initialize a sqlite configuration")]
11+
Sqlite {
12+
#[clap(long = "filename")]
13+
#[clap(default_value_t = String::from("db.sqlite3"))]
14+
#[clap(help = "Name of the sqlite file.")]
15+
filename: String,
16+
},
17+
#[clap(about = "Initialize a mysql configuration")]
18+
Mysql {
19+
#[clap(long = "host")]
20+
#[clap(default_value_t = String::from("127.0.0.1"))]
21+
#[clap(help = "The address to use to connect to the database.")]
22+
host: String,
23+
#[clap(long = "port")]
24+
#[clap(default_value_t = 5432)]
25+
#[clap(help = "The port to use to connect to the database.")]
26+
port: u16,
27+
#[clap(long = "user")]
28+
#[clap(default_value_t = String::from("dbuser"))]
29+
#[clap(help = "The user to use to connect to the database.")]
30+
user: String,
31+
#[clap(long = "password")]
32+
#[clap(
33+
help = "Set the password. To minimize the risk of exposing your password, use --ask-password instead."
34+
)]
35+
password: Option<String>,
36+
#[clap(long = "ask-password")]
37+
#[clap(
38+
help = "Ask for the password for the database. If specified with the --password option, this value will be prevalent."
39+
)]
40+
ask_password: bool,
41+
#[clap(long = "name")]
42+
#[clap(default_value_t = String::from("dbname"))]
43+
#[clap(help = "The name of the database to connect to.")]
44+
name: String,
45+
},
46+
#[clap(about = "Initialize a postgres configuration")]
47+
Postgres {
48+
#[clap(long = "host")]
49+
#[clap(default_value_t = String::from("127.0.0.1"))]
50+
#[clap(help = "The address to use to connect to the database.")]
51+
host: String,
52+
#[clap(long = "port")]
53+
#[clap(default_value_t = 5432)]
54+
#[clap(help = "The port to use to connect to the database.")]
55+
port: u16,
56+
#[clap(long = "user")]
57+
#[clap(default_value_t = String::from("dbuser"))]
58+
#[clap(help = "The user to use to connect to the database.")]
59+
user: String,
60+
#[clap(long = "password")]
61+
#[clap(
62+
help = "Set the password. To minimize the risk of exposing your password, use --ask-password instead."
63+
)]
64+
password: Option<String>,
65+
#[clap(long = "ask-password")]
66+
#[clap(
67+
help = "Ask for the password for the database. If specified with the --password option, this value will be prevalent."
68+
)]
69+
ask_password: bool,
70+
#[clap(long = "name")]
71+
#[clap(default_value_t = String::from("dbname"))]
72+
#[clap(help = "The name of the database to connect to.")]
73+
name: String,
74+
},
75+
}
76+
77+
#[derive(Subcommand)]
78+
enum Commands {
79+
#[clap(about = "Create the database configuration file")]
80+
Init {
81+
#[clap(long = "database-config")]
82+
#[clap(default_value_t = String::from("./database.toml"))]
83+
#[clap(help = "Path to the database configuration file that should be created.")]
84+
database_config: String,
85+
86+
#[clap(short = 'f', long = "force")]
87+
#[clap(help = "Overwrite the database configuration if it is existent already")]
88+
force: bool,
89+
90+
#[clap(subcommand)]
91+
driver: InitDriver,
92+
},
93+
94+
#[clap(about = "Tool to create migrations")]
95+
MakeMigrations {
96+
#[clap(long = "models-file")]
97+
#[clap(default_value_t=String::from("./.models.json"))]
98+
#[clap(help = "Location of the intermediate representation of models.")]
99+
models_file: String,
100+
101+
#[clap(short = 'm', long = "migration-dir")]
102+
#[clap(default_value_t=String::from("./migrations/"))]
103+
#[clap(help = "Destination to / from which migrations are written / read.")]
104+
migration_dir: String,
105+
106+
#[clap(help = "Use this name as migration name instead of generating one.")]
107+
name: Option<String>,
108+
109+
#[clap(long = "non-interactive")]
110+
#[clap(action = ArgAction::SetTrue)]
111+
#[clap(help = "If set, no questions will be asked.")]
112+
non_interactive: bool,
113+
114+
#[clap(long = "disable-warnings")]
115+
#[clap(action = ArgAction::SetTrue)]
116+
#[clap(help = "If set, no warnings will be printed.")]
117+
warnings_disabled: bool,
118+
},
119+
120+
#[clap(about = "Apply migrations")]
121+
Migrate {
122+
#[clap(short = 'm', long = "migration-dir")]
123+
#[clap(default_value_t=String::from("./migrations/"))]
124+
#[clap(help = "Destination to / from which migrations are written / read.")]
125+
migration_dir: String,
126+
127+
#[clap(long = "database-config")]
128+
#[clap(default_value_t=String::from("./database.toml"))]
129+
#[clap(help = "Path to the database configuration file.")]
130+
database_config: String,
131+
132+
#[clap(long = "log-sql")]
133+
#[clap(action = ArgAction::SetTrue)]
134+
#[clap(help = "If turned on, all queries to the database will be logged")]
135+
log_queries: bool,
136+
137+
#[clap(long = "apply-until")]
138+
#[clap(id = "MIGRATION_ID")]
139+
#[clap(help = "Only apply the migrations to (inclusive) the given migration.")]
140+
apply_until: Option<u16>,
141+
},
142+
143+
#[clap(about = "Squash migrations")]
144+
SquashMigrations {
145+
#[clap(short = 'm', long = "migration-dir")]
146+
#[clap(default_value_t = String::from("./migrations/"))]
147+
#[clap(help = "Destination to / from which migrations are written / read.")]
148+
migration_dir: String,
149+
150+
#[clap(help = "First migration to start squashing from.")]
151+
first_migration: u16,
152+
153+
#[clap(help = "Last migration to squash.")]
154+
last_migration: u16,
155+
},
156+
157+
#[clap(about = "Merge migrations")]
158+
MergeMigrations {},
159+
}
160+
161+
#[derive(Parser)]
162+
#[clap(version, about = "CLI tool for rorm", long_about = None)]
163+
#[clap(arg_required_else_help = true)]
164+
#[clap(name = "rorm-cli")]
165+
pub struct Cli {
166+
#[clap(subcommand)]
167+
command: Option<Commands>,
168+
}
169+
170+
pub async fn entry(cli: Cli) -> anyhow::Result<()> {
171+
match cli.command {
172+
Some(Commands::Init {
173+
force,
174+
driver,
175+
database_config,
176+
}) => init(database_config, driver, force)?,
177+
Some(Commands::MakeMigrations {
178+
models_file,
179+
migration_dir,
180+
name,
181+
non_interactive,
182+
warnings_disabled,
183+
}) => {
184+
run_make_migrations(MakeMigrationsOptions {
185+
models_file,
186+
migration_dir,
187+
name,
188+
non_interactive,
189+
warnings_disabled,
190+
})?;
191+
}
192+
Some(Commands::Migrate {
193+
migration_dir,
194+
database_config,
195+
log_queries,
196+
apply_until,
197+
}) => {
198+
run_migrate(MigrateOptions {
199+
migration_dir,
200+
database_config,
201+
log_queries,
202+
apply_until,
203+
})
204+
.await?;
205+
}
206+
Some(Commands::SquashMigrations {
207+
migration_dir,
208+
first_migration,
209+
last_migration,
210+
}) => {
211+
squash_migrations(migration_dir, first_migration, last_migration).await?;
212+
}
213+
_ => {}
214+
}
215+
216+
Ok(())
217+
}

src/init/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use std::process::exit;
55

66
use rorm_declaration::config::{DatabaseConfig, DatabaseDriver};
77

8+
use crate::entry::InitDriver;
89
use crate::migrate::config::DatabaseConfigFile;
9-
use crate::InitDriver;
1010

1111
pub fn init(database_configuration: String, driver: InitDriver, force: bool) -> anyhow::Result<()> {
1212
let p = Path::new(&database_configuration);

src/lib.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/// Exports for [rorm-cli-lib].
2+
///
3+
/// Reexports for executing the defined cli parser via another
4+
pub mod entry;
5+
/// This module is used for creating a configuration file that can be used by the
6+
/// binary version.
7+
pub mod init;
8+
/// This module handles the creation of migration files
9+
pub mod make_migrations;
10+
/// This module is used for applying migrations
11+
pub mod migrate;
12+
13+
mod linter;
14+
mod squash_migrations;
15+
mod utils;

0 commit comments

Comments
 (0)