midas-migrate-cli 0.8.1

Do painless migration 🦀
Documentation
# Midas (cargo-migrate)

[![Crates.io Package](https://img.shields.io/crates/v/midas?style=flat-square)](https://crates.io/crates/midas)
[![Crates.io Downloads](https://img.shields.io/crates/d/midas?style=flat-square)](https://crates.io/crates/midas)
[![License](https://img.shields.io/crates/l/midas?style=flat-square)](https://github.com/ffimnsr/midas-rs/blob/master/LICENSE-APACHE)
[![Github Workflow Status](https://img.shields.io/github/actions/workflow/status/ffimnsr/midas-rs/ci.yaml?style=flat-square)](https://github.com/ffimnsr/midas-rs/blob/master/.github/workflows/ci.yaml)


> So Midas, king of Lydia, swelled at first with pride
> when he found he could transform everything he touched
> to gold; but when he beheld his food grow rigid and his
> drink harden into golden ice then he understood that
> this gift was a bane and in his loathing for gold, cursed
> his prayer.
> - from In Rufinem, Claudian

Do painless migrations.

## Supported Database

- [x] PostgresSQL and Postgres-wire compatible servers
- [x] CockroachDB
- [x] SQLite3

The migration engine targets a narrow,
well-tested set of backends: Postgres (including managed and Postgres-compatible
services) and SQLite.

## Usage

### Using CLI

Here is a sample command line usage of `midas`.

```shell
# PostgresSQL (also used for CockroachDB and other Postgres-wire servers)
midas --database 'postgres://postgres:postgres@localhost:5432/startup' --source migrations up

# SQLite
midas --database './data.db3' --source migrations up
```

or you could also use the `cargo migrate` to integrate it on your cargo workflow.

> **NOTE:** For SQLite use correct URI filenames as stated here: https://www.sqlite.org/c3ref/open.html#urifilenameexamples

### Using on container

Here is a basic setup:

```bash
MIGRATION_DIR=$PWD/migrations
mkdir -p $MIGRATION_DIR
docker run --rm -v $MIGRATION_DIR:/app/migrations ghcr.io/ffimnsr/midas-rs:latest --database 'postgres://postgres:postgres@localhost:5432/startup' --source migrations status
```

You can omit the `--source migrations` as the source flag would default to migrations. In case you plan to change the mounted volume migrations path then append the `--source <path/to/migrations>` argument.

### Command arguments

The command will execute all **special** (up) SQL migrations files to the database. \
Here are the available subcommands:

```shell
add       Create a timestamped migration file
adopt     Verify that an existing SQLx metadata table can be adopted
baseline  Mark migrations up to a version as applied without running SQL
db        Database lifecycle commands
doctor    Diagnose database and migration state
down      Revert every applied migration
export    Export migrations for another migration tool
generate  Generate Rust types and SQLx derives from the schema DSL
import    Import migrations from another migration tool
info      Show applied, pending, and dirty migrations
init      Set up a midas project
list      List migration files without a database
prepare   Generate offline SQLx metadata from the schema DSL
redo      Revert and re-apply the last migration
repair    Recompute checksums for applied migrations
reset     Drop metadata and replay every migration
resolve   Resolve a dirty migration
revert    Revert applied migrations
run       Apply pending migrations
schema    Validate, format, and emit the schema DSL
```

### Schema DSL, codegen, and offline SQLx

The schema compiler, code generator, and offline SQLx metadata live in the
separate `midas-schema` crate. The `midas` crate stays focused on the migration
engine, so schema changes do not trigger migration-engine rebuilds.

```shell
# Validate and emit create-from-empty SQL
midas schema validate schema.toml
midas schema sql schema.toml --dialect postgres

# Generate SQLx `FromRow` models from the resolved schema
midas generate --sqlx --schema schema.toml --out src

# Write `.sqlx` metadata without a live database
midas prepare --schema schema.toml --path .
midas prepare --check --schema schema.toml --path .
```

The published DSL artifacts live in [`schemas/`](schemas):
`schema.schema.json` for editor validation, `todo-app.toml` as a complete
example schema, and `todo-app.midas` / `full.midas` as `schema.midas` mirrors.
The `version = 1` contract and compatibility rules are in
[`docs/stability.md`](docs/stability.md); the optional `.midas` syntax and parity
rules are in [`docs/schema-midas.md`](docs/schema-midas.md). Both formats emit
byte-identical SQL.

### Seeding and setup

```shell
# Apply deterministic development data to an existing database
midas faker --seed-file seed.toml
midas faker --reset --yes --dry-run --json

# Create the database, apply migrations or the schema DSL, and seed it
midas setup --from auto
midas setup --from schema --accept-data-loss
```

Seeds live in `seed.toml` and never contain DDL or runtime code. The same
master seed, reference time, and starting data produce byte-identical rows. The
full grammar, provider catalogue, reset semantics, and JSON summary shape are
documented in [`docs/seeding.md`](docs/seeding.md).

```sh
# Optional Prisma-like authoring source
midas schema validate schema.midas
midas schema sql schema.midas
midas schema convert schema.toml --to midas --output schema.midas
midas schema format --check schema.midas
```

### SQLx interoperability

Midas can read and write SQLx migration files and adopt an existing SQLx
metadata table without re-running applied SQL.

```shell
# Export Midas migrations for sqlx-cli or `sqlx::migrate!`
midas --source migrations migrate export --format sqlx --out sqlx-migrations

# Import SQLx migrations as Midas migrations
midas sqlx import --from migrations --out midas-migrations

# Copy applied versions from _sqlx_migrations into Midas metadata
midas --source migrations migrate baseline --from sqlx

# Keep using the SQLx table directly (it is never altered)
midas migrate adopt --table _sqlx_migrations
```

Settings in `sqlx.toml` (`table-name`, `migrations-dir`, `create-schemas`,
`ignored-chars`, and `[migrate.defaults]`) are honored automatically.

### Transport security

Postgres connections honor `MIDAS_TLS` (`disable`, `prefer`, or `require`) and any
explicit `sslmode` query parameter in the URL. The URL parameter wins so a single
connection can tighten TLS without changing global configuration.

TLS is compiled in through Cargo features:

```shell
# Pure-Rust TLS (preferred)
cargo install midas-migrate-cli --features tls-rustls

# Platform TLS via native-tls / OpenSSL
cargo install midas-migrate-cli --features tls-native-tls
```

The rustls backend trusts the operating system certificate store; native CAs and
private CAs installed system-wide both work. Without a TLS feature the client
still connects to `sslmode=disable` servers, and `require` fails closed.

Shadow databases for `migrate dev` are created on `shadow_database_url` when it
is configured (for example through `shadow_database_url_env`), otherwise next to
the development database.

For more info see `--help`.

## Installation

The binary name for midas are `midas` and `cargo-migrate`.

Binary for different OS distribution can be downloaded [here](https://github.com/ffimnsr/midas-rs/releases). Linux (musl) and macOS (Apple Silicon) are supported.

### Install using script

`midas` runs on most major platforms. If your platform isn't listed below, please [open an issue](https://github.com/ffimnsr/midas-rs/issues/new).

<details>
  <summary>Linux / WSL / MSYS2 / Cygwin / Git Bash</summary>

  > The recommended way to install midas is via the install script:
  >
  >
  > ```sh
  > curl -sSfL https://raw.githubusercontent.com/ffimnsr/midas-rs/main/install.sh | sh
  > ```
</details>

<details>
  <summary>BSD / Android</summary>

  > The recommended way to install midas is via the install script:
  >
  >
  > ```sh
  > curl -sS https://raw.githubusercontent.com/ffimnsr/midas-rs/main/install.sh | bash
  > ```
</details>

### From source

If you're into **Rust**, then midas can be installed with `cargo`. The minimum supported version of Rust is `1.99.0`. The binaries produce may be bigger than expected as it contains debug symbols.

By default SQLite links against the host `libsqlite3`. Release and container
builds should enable the bundled driver so the binary stays self-contained:

```bash
# Uses the system libsqlite3 (requires libsqlite3-dev / sqlite-devel)
cargo install midas-migrate-cli

# Static, self-contained SQLite (recommended for release installs)
cargo install midas-migrate-cli --features sqlite-bundled
```

### For containers

Docker / Podman installation:

```bash
# Docker
docker pull ghcr.io/ffimnsr/midas-rs:latest

# Podman
podman pull ghcr.io/ffimnsr/midas-rs:latest
```

### For package managers

If you're a Debian user (or a user of a Debian derivative like Ubuntu), then midas can be installed using a binary .deb file provided in each midas release.

```bash
curl -LO https://github.com/ffimnsr/midas-rs/releases/download/0.6.6/midas_0.6.6-1_amd64.deb
sudo dpkg -i midas_0.6.6-1_amd64.deb
```

### Manual installation

Follow the instruction below to install and use `midas` on your system.

1. Download the binary for your OS distribution [here]https://github.com/ffimnsr/midas/releases.
2. Copy it to your system binary directory (`/usr/local/bin`) or to your userspace binary directory (`$HOME/.local/bin`).

## What's in the Roadmap

- [x] TOML DSL for creating database objects
- [x] SQLx codegen and offline metadata (`generate`, `prepare`)
- [x] `setup` and `faker` commands backed by deterministic `seed.toml` data

## License

Licensed under either of

- Apache License, Version 2.0 ([LICENSE-APACHE]LICENSE-APACHE or
  http://www.apache.org/licenses/LICENSE-2.0)
- MIT license ([LICENSE-MIT]LICENSE-MIT or http://opensource.org/licenses/MIT)

at your option.

### Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.