sqlx_models_cli/
opt.rs

1use clap::Clap;
2
3#[derive(Clap, Debug)]
4pub struct Opt {
5    #[clap(subcommand)]
6    pub command: Command,
7}
8
9#[derive(Clap, Debug)]
10pub enum Command {
11    #[clap(alias = "db")]
12    Database(DatabaseOpt),
13
14    /// Generate query metadata to support offline compile-time verification.
15    ///
16    /// Saves metadata for all invocations of `query!` and related macros to `sqlx-data.json`
17    /// in the current directory, overwriting if needed.
18    ///
19    /// During project compilation, the absence of the `DATABASE_URL` environment variable or
20    /// the presence of `SQLX_OFFLINE` (with a value of `true` or `1`) will constrain the
21    /// compile-time verification to only read from the cached query metadata.
22    #[clap(alias = "prep")]
23    Prepare {
24        /// Run in 'check' mode. Exits with 0 if the query metadata is up-to-date. Exits with
25        /// 1 if the query metadata needs updating.
26        #[clap(long)]
27        check: bool,
28
29        /// Generate a single top-level `sqlx-data.json` file when using a cargo workspace.
30        #[clap(long)]
31        merged: bool,
32
33        /// Arguments to be passed to `cargo rustc ...`.
34        #[clap(last = true)]
35        args: Vec<String>,
36
37        /// Location of the DB, by default will be read from the DATABASE_URL env var
38        #[clap(long, short = 'D', env)]
39        database_url: String,
40    },
41
42    #[clap(alias = "mig")]
43    Migrate(MigrateOpt),
44}
45
46/// Group of commands for creating and dropping your database.
47#[derive(Clap, Debug)]
48pub struct DatabaseOpt {
49    #[clap(subcommand)]
50    pub command: DatabaseCommand,
51}
52
53#[derive(Clap, Debug)]
54pub enum DatabaseCommand {
55    /// Creates the database specified in your DATABASE_URL.
56    Create {
57        /// Location of the DB, by default will be read from the DATABASE_URL env var
58        #[clap(long, short = 'D', env)]
59        database_url: String,
60    },
61
62    /// Drops the database specified in your DATABASE_URL.
63    Drop {
64        /// Automatic confirmation. Without this option, you will be prompted before dropping
65        /// your database.
66        #[clap(short)]
67        yes: bool,
68
69        /// Location of the DB, by default will be read from the DATABASE_URL env var
70        #[clap(long, short = 'D', env)]
71        database_url: String,
72    },
73
74    /// Drops the database specified in your DATABASE_URL, re-creates it, and runs any pending migrations.
75    Reset {
76        /// Automatic confirmation. Without this option, you will be prompted before dropping
77        /// your database.
78        #[clap(short)]
79        yes: bool,
80
81        /// Path to folder containing migrations.
82        #[clap(long, default_value = "migrations")]
83        source: String,
84
85        /// Location of the DB, by default will be read from the DATABASE_URL env var
86        #[clap(long, short = 'D', env)]
87        database_url: String,
88    },
89
90    /// Creates the database specified in your DATABASE_URL and runs any pending migrations.
91    Setup {
92        /// Path to folder containing migrations.
93        #[clap(long, default_value = "migrations")]
94        source: String,
95
96        /// Location of the DB, by default will be read from the DATABASE_URL env var
97        #[clap(long, short = 'D', env)]
98        database_url: String,
99    },
100}
101
102/// Group of commands for creating and running migrations.
103#[derive(Clap, Debug)]
104pub struct MigrateOpt {
105    /// Path to folder containing migrations.
106    #[clap(long, default_value = "migrations")]
107    pub source: String,
108
109    #[clap(subcommand)]
110    pub command: MigrateCommand,
111}
112
113#[derive(Clap, Debug)]
114pub enum MigrateCommand {
115    /// Create a new migration with the given description,
116    /// and the current time as the version.
117    Add {
118        description: String,
119
120        /// If true, creates a pair of up and down migration files with same version
121        /// else creates a single sql file
122        #[clap(short)]
123        reversible: bool,
124    },
125
126    /// Run all pending migrations.
127    Run {
128        /// List all the migrations to be run without applying
129        #[clap(long)]
130        dry_run: bool,
131
132        /// Ignore applied migrations that missing in the resolved migrations
133        #[clap(long)]
134        ignore_missing: bool,
135
136        /// Location of the DB, by default will be read from the DATABASE_URL env var
137        #[clap(long, short = 'D', env)]
138        database_url: String,
139    },
140
141    /// Generate migrations from Model.
142    #[clap(alias = "gen")]
143    Generate {
144        /// Location of the DB, by default will be read from the DATABASE_URL env var
145        #[clap(long, short = 'D', env)]
146        database_url: String,
147        /// Used to filter through the models to execute.
148        #[clap(long)]
149        table: Option<String>,
150    },
151
152    /// Revert the latest migration with a down file.
153    Revert {
154        /// List the migration to be reverted without applying
155        #[clap(long)]
156        dry_run: bool,
157
158        /// Ignore applied migrations that missing in the resolved migrations
159        #[clap(long)]
160        ignore_missing: bool,
161
162        /// Location of the DB, by default will be read from the DATABASE_URL env var
163        #[clap(long, short = 'D', env)]
164        database_url: String,
165    },
166
167    /// List all available migrations.
168    Info {
169        /// Location of the DB, by default will be read from the DATABASE_URL env var
170        #[clap(long, env)]
171        database_url: String,
172    },
173
174    /// Generate a `build.rs` to trigger recompilation when a new migration is added.
175    ///
176    /// Must be run in a Cargo project root.
177    BuildScript {
178        /// Overwrite the build script if it already exists.
179        #[clap(long)]
180        force: bool,
181    },
182}