diff --git a/.github/workflows/cargo-sqlx.yml b/.github/workflows/sqlx-cli.yml similarity index 96% rename from .github/workflows/cargo-sqlx.yml rename to .github/workflows/sqlx-cli.yml index 792fe8f5..2c3c4bb7 100644 --- a/.github/workflows/cargo-sqlx.yml +++ b/.github/workflows/sqlx-cli.yml @@ -1,4 +1,4 @@ -name: cargo-sqlx +name: sqlx-cli on: pull_request: @@ -52,8 +52,8 @@ jobs: docker cp schema.sql $CONTAINER_ID:/schema.sql docker exec $CONTAINER_ID bash -c "psql -d $DATABASE_URL -f ./schema.sql" - - name: install cargo-sqlx - run: cargo install -f --path cargo-sqlx/ + - name: install sqlx-cli + run: cargo install -f --path sqlx-cli/ - name: test `cargo sqlx prepare [--check]` working-directory: examples/postgres/todos/ diff --git a/Cargo.lock b/Cargo.lock index 4cb7bd4e..5752a3b0 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -298,27 +298,6 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "130aac562c0dd69c56b3b1cc8ffd2e17be31d0b6c25b61c96b76231aa23e39e1" -[[package]] -name = "cargo-sqlx" -version = "0.1.0" -dependencies = [ - "anyhow", - "async-trait", - "cargo_metadata", - "chrono", - "console", - "dialoguer", - "dotenv", - "futures 0.3.4", - "glob", - "serde", - "serde_json", - "sqlx", - "structopt", - "tokio 0.2.13", - "url 2.1.1", -] - [[package]] name = "cargo_metadata" version = "0.10.0" @@ -1826,6 +1805,27 @@ dependencies = [ "trybuild", ] +[[package]] +name = "sqlx-cli" +version = "0.0.1" +dependencies = [ + "anyhow", + "async-trait", + "cargo_metadata", + "chrono", + "console", + "dialoguer", + "dotenv", + "futures 0.3.4", + "glob", + "serde", + "serde_json", + "sqlx", + "structopt", + "tokio 0.2.13", + "url 2.1.1", +] + [[package]] name = "sqlx-core" version = "0.3.5" diff --git a/Cargo.toml b/Cargo.toml index 3502d5ad..ec492b23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,7 +4,7 @@ members = [ "sqlx-core", "sqlx-macros", "sqlx-test", - "cargo-sqlx", + "sqlx-cli", "examples/mysql/todos", "examples/postgres/listen", "examples/postgres/todos", diff --git a/cargo-sqlx/README.md b/cargo-sqlx/README.md deleted file mode 100644 index 9a0f34b0..00000000 --- a/cargo-sqlx/README.md +++ /dev/null @@ -1,14 +0,0 @@ -# cargo-sqlx - -Sqlx migrator runs all `*.sql` files under `migrations` folder and remembers which ones has been run. - -Database url is supplied through either env variable or `.env` file containing `DATABASE_URL="postgres://postgres:postgres@localhost/realworld"`. - -##### Commands -- `add ` - add new migration to your migrations folder named `_.sql` -- `database` - create or drop database based on connection string -- `run` - Runs all migrations in your migrations folder - - -##### Limitations -- No down migrations! If you need down migrations, there are other more feature complete migrators to use. diff --git a/cargo-sqlx/.gitignore b/sqlx-cli/.gitignore similarity index 100% rename from cargo-sqlx/.gitignore rename to sqlx-cli/.gitignore diff --git a/cargo-sqlx/Cargo.toml b/sqlx-cli/Cargo.toml similarity index 82% rename from cargo-sqlx/Cargo.toml rename to sqlx-cli/Cargo.toml index bd94acf4..9568fe20 100644 --- a/cargo-sqlx/Cargo.toml +++ b/sqlx-cli/Cargo.toml @@ -1,8 +1,11 @@ [package] -name = "cargo-sqlx" -version = "0.1.0" -description = "Simple postgres migrator without support for down migration" -authors = ["Jesper Axelsson "] +name = "sqlx-cli" +version = "0.0.1" +description = "Command-line utility for SQLx, the Rust SQL toolkit." +authors = [ + "Jesper Axelsson ", + "Austin Bonander " # austin@launchbadge.com +] edition = "2018" readme = "README.md" homepage = "https://github.com/launchbadge/sqlx" @@ -40,7 +43,6 @@ cargo_metadata = "0.10.0" [features] default = [ "postgres", "sqlite", "mysql" ] - # database mysql = [ "sqlx/mysql" ] postgres = [ "sqlx/postgres" ] diff --git a/sqlx-cli/README.md b/sqlx-cli/README.md new file mode 100644 index 00000000..e1c84a34 --- /dev/null +++ b/sqlx-cli/README.md @@ -0,0 +1,63 @@ +# SQLx CLI + +SQLx's associated command-line utility for managing databases, migrations, and enabling "offline" +mode with `sqlx::query!()` and friends. + +### Installation + +```bash +$ cargo install sqlx-cli +``` + +### Commands + +All commands require `DATABASE_URL` to be set, either in the environment or in a `.env` file +in the current working directory. + +`database` and `migrate` subcommands support only Postgres; MySQL and SQLite are TODO. + +```dotenv +# Postgres +DATABASE_URL=postgres://postgres@localhost/my_database +``` + +#### Create/drop the database at `DATABASE_URL` + +```bash +sqlx database create +sqlx database drop +``` + +#### Create and run migrations + +```bash +$ sqlx migrate add +``` +Creates a new file in `migrations/-.sql`. Add your database schema changes to +this new file. + +--- +```bash +$ sqlx migration run +``` +Compares the migration history of the running database against the `migrations/` folder and runs +any scripts that are still pending. + +##### Note: Down-Migrations +Down-migrations are currently a non-planned feature as their utility seems dubious but we welcome +any contributions (discussions/code) regarding this matter. + +#### Enable building in "offline" mode with `query!()` +Note: must be run as `cargo sqlx`. + +```bash +cargo sqlx prepare +``` +Saves query data to `sqlx-data.json` in the current directory; check this file into version control +and an active database connection will no longer be needed to build your project. +---- +```bash +cargo sqlx prepare --check +``` +Exits with a nonzero exit status if the data in `sqlx-data.json` is out of date with the current +database schema and queries in the project. Intended for use in Continuous Integration. diff --git a/cargo-sqlx/src/bin/cargo-sqlx.rs b/sqlx-cli/src/bin/cargo-sqlx.rs similarity index 78% rename from cargo-sqlx/src/bin/cargo-sqlx.rs rename to sqlx-cli/src/bin/cargo-sqlx.rs index 89d2709f..2215c6f7 100644 --- a/cargo-sqlx/src/bin/cargo-sqlx.rs +++ b/sqlx-cli/src/bin/cargo-sqlx.rs @@ -1,11 +1,11 @@ -use cargo_sqlx::Command; +use sqlx_cli::Command; use structopt::{clap, StructOpt}; use std::env; #[tokio::main] async fn main() -> anyhow::Result<()> { - // when invoked as `cargo sqlx [...]` the args we see are `[...]/cargo-sqlx sqlx prepare` + // when invoked as `cargo sqlx [...]` the args we see are `[...]/sqlx-cli sqlx prepare` // so we want to notch out that superfluous "sqlx" let args = env::args_os().skip(2); @@ -14,5 +14,5 @@ async fn main() -> anyhow::Result<()> { .setting(clap::AppSettings::NoBinaryName) .get_matches_from(args); - cargo_sqlx::run(Command::from_clap(&matches)).await + sqlx_cli::run(Command::from_clap(&matches)).await } diff --git a/cargo-sqlx/src/bin/sqlx.rs b/sqlx-cli/src/bin/sqlx.rs similarity index 61% rename from cargo-sqlx/src/bin/sqlx.rs rename to sqlx-cli/src/bin/sqlx.rs index 4d9b31a6..2c6997ee 100644 --- a/cargo-sqlx/src/bin/sqlx.rs +++ b/sqlx-cli/src/bin/sqlx.rs @@ -1,8 +1,8 @@ -use cargo_sqlx::Command; +use sqlx_cli::Command; use structopt::StructOpt; #[tokio::main] async fn main() -> anyhow::Result<()> { // no special handling here - cargo_sqlx::run(Command::from_args()).await + sqlx_cli::run(Command::from_args()).await } diff --git a/cargo-sqlx/src/db.rs b/sqlx-cli/src/db.rs similarity index 100% rename from cargo-sqlx/src/db.rs rename to sqlx-cli/src/db.rs diff --git a/cargo-sqlx/src/lib.rs b/sqlx-cli/src/lib.rs similarity index 100% rename from cargo-sqlx/src/lib.rs rename to sqlx-cli/src/lib.rs diff --git a/cargo-sqlx/src/migration.rs b/sqlx-cli/src/migration.rs similarity index 100% rename from cargo-sqlx/src/migration.rs rename to sqlx-cli/src/migration.rs diff --git a/cargo-sqlx/src/migrator/mod.rs b/sqlx-cli/src/migrator/mod.rs similarity index 95% rename from cargo-sqlx/src/migrator/mod.rs rename to sqlx-cli/src/migrator/mod.rs index 23fab844..111ed633 100644 --- a/cargo-sqlx/src/migrator/mod.rs +++ b/sqlx-cli/src/migrator/mod.rs @@ -3,6 +3,9 @@ use async_trait::async_trait; use std::env; use url::Url; +#[cfg(feature = "mysql")] +mod mysql; + #[cfg(feature = "postgres")] mod postgres; @@ -60,7 +63,7 @@ pub fn get() -> Result> { db_url), #[cfg(feature = "mysql")] - "mysql" | "mariadb" => bail!("Not implemented"), + "mysql" | "mariadb" => Ok(Box::new(self::mysql::MySql::new(db_url_raw))), #[cfg(not(feature = "mysql"))] "mysql" | "mariadb" => bail!( "DATABASE_URL {} has the scheme of a MySQL/MariaDB database but the `mysql` feature of sqlx was not enabled", diff --git a/cargo-sqlx/src/mysql.rs b/sqlx-cli/src/migrator/mysql.rs similarity index 98% rename from cargo-sqlx/src/mysql.rs rename to sqlx-cli/src/migrator/mysql.rs index 2cb87bd8..7cb38d5c 100644 --- a/cargo-sqlx/src/mysql.rs +++ b/sqlx-cli/src/migrator/mysql.rs @@ -9,7 +9,7 @@ use sqlx::Row; use anyhow::{anyhow, Context, Result}; use async_trait::async_trait; -use crate::database_migrator::{DatabaseMigrator, MigrationTransaction}; +use super::{DatabaseMigrator, MigrationTransaction}; pub struct MySql { pub db_url: String, diff --git a/cargo-sqlx/src/migrator/postgres.rs b/sqlx-cli/src/migrator/postgres.rs similarity index 98% rename from cargo-sqlx/src/migrator/postgres.rs rename to sqlx-cli/src/migrator/postgres.rs index 9144e039..a6278fc6 100644 --- a/cargo-sqlx/src/migrator/postgres.rs +++ b/sqlx-cli/src/migrator/postgres.rs @@ -89,6 +89,7 @@ impl DatabaseMigrator for Postgres { let mut conn = PgConnection::connect(base_url).await?; + // quote database name (quotes in the name are escaped with additional quotes) sqlx::query(&format!( "CREATE DATABASE \"{}\"", db_name.replace('"', "\"\"") diff --git a/cargo-sqlx/src/migrator/sqlite.rs b/sqlx-cli/src/migrator/sqlite.rs similarity index 100% rename from cargo-sqlx/src/migrator/sqlite.rs rename to sqlx-cli/src/migrator/sqlite.rs diff --git a/cargo-sqlx/src/prepare.rs b/sqlx-cli/src/prepare.rs similarity index 100% rename from cargo-sqlx/src/prepare.rs rename to sqlx-cli/src/prepare.rs