rename cargo-sqlx -> sqlx-cli

edit README
This commit is contained in:
Austin Bonander 2020-05-15 20:07:57 -07:00 committed by Ryan Leckey
parent a44e29c84c
commit 7dae3dbf57
17 changed files with 106 additions and 51 deletions

View File

@ -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/

42
Cargo.lock generated
View File

@ -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"

View File

@ -4,7 +4,7 @@ members = [
"sqlx-core",
"sqlx-macros",
"sqlx-test",
"cargo-sqlx",
"sqlx-cli",
"examples/mysql/todos",
"examples/postgres/listen",
"examples/postgres/todos",

View File

@ -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 <name>` - add new migration to your migrations folder named `<timestamp>_<name>.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.

View File

@ -1,8 +1,11 @@
[package]
name = "cargo-sqlx"
version = "0.1.0"
description = "Simple postgres migrator without support for down migration"
authors = ["Jesper Axelsson <jesperaxe@gmail.com>"]
name = "sqlx-cli"
version = "0.0.1"
description = "Command-line utility for SQLx, the Rust SQL toolkit."
authors = [
"Jesper Axelsson <jesperaxe@gmail.com>",
"Austin Bonander <austin.bonander@gmail.com>" # 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" ]

63
sqlx-cli/README.md Normal file
View File

@ -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 <name>
```
Creates a new file in `migrations/<timestamp>-<name>.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.

View File

@ -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
}

View File

@ -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
}

View File

@ -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<Box<dyn DatabaseMigrator>> {
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",

View File

@ -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,

View File

@ -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('"', "\"\"")