#![forbid(unsafe_code)]
#![warn(clippy::pedantic)]
use std::collections::BTreeMap;
use indexmap::IndexMap;
use std::borrow::Cow;
use std::ffi::OsString;
use std::fs;
use std::path::{Path, PathBuf};
use std::process::ExitCode;
use std::time::{Duration, SystemTime, UNIX_EPOCH};
use clap::{Parser, Subcommand};
use notify::{RecursiveMode, Watcher};
use ruprizzle::{Executor, Pool, Tx};
use ruprizzle_codegen::generate_all;
use ruprizzle_core::SchemaError;
use ruprizzle_core::ir::{DatasourceUrl, Provider, Schema};
use ruprizzle_dialect::{check_schema_capabilities, dialect_for};
use ruprizzle_migrate::rename::suggest_renames;
use ruprizzle_migrate::runner::{compute_checksum, split_statements};
use ruprizzle_migrate::{Change, MigrationMeta, Migrator, diff, down_sql, up_sql};
mod introspect;
mod seed;
#[cfg(feature = "studio")]
mod studio;
#[derive(Parser, Debug)]
#[command(
name = "ruprizzle",
version,
about = "A schema-first ORM for Rust",
long_about = "Write a schema, get typed entities, a query builder that shows you \
its SQL, and migrations generated by diffing your schema."
)]
struct Cli {
#[arg(
long,
global = true,
default_value = "schema.ruprizzle",
env = "RUPRIZZLE_SCHEMA"
)]
schema: String,
#[arg(long, global = true)]
verbose: bool,
#[command(subcommand)]
command: Command,
}
#[derive(Subcommand, Debug)]
enum Command {
Init {
#[arg(long, default_value = "postgres")]
provider: String,
},
Generate {
#[arg(long)]
watch: bool,
},
Validate,
Format,
#[command(subcommand)]
Migrate(MigrateCommand),
#[command(subcommand)]
Db(DbCommand),
Lsp {
#[arg(long, default_value_t = true)]
stdio: bool,
},
Check {
#[arg(long)]
manifest: String,
#[arg(long, default_value = "pretty")]
format: String,
},
#[cfg(feature = "studio")]
Studio {
#[arg(long, default_value_t = 5555)]
port: u16,
#[arg(long, default_value = "127.0.0.1")]
host: String,
#[arg(long)]
allow_writes: bool,
#[arg(long)]
yes_i_know: bool,
#[arg(long)]
no_browser: bool,
#[arg(long, env = "RUPRIZZLE_STUDIO_TOKEN", hide_env_values = true)]
token: Option<String>,
},
}
#[derive(Subcommand, Debug)]
enum MigrateCommand {
Dev {
#[arg(long)]
name: Option<String>,
#[arg(long)]
accept_data_loss: bool,
#[arg(long)]
create_only: bool,
},
Deploy,
Status,
Resolve {
id: String,
},
Reset {
#[arg(long)]
force: bool,
},
Squash {
#[arg(long)]
name: Option<String>,
#[arg(long)]
force: bool,
},
}
#[derive(Subcommand, Debug)]
enum DbCommand {
Push {
#[arg(long)]
accept_data_loss: bool,
},
Pull,
Seed,
}
#[tokio::main]
async fn main() -> ExitCode {
let cli = Cli::parse();
if cli.verbose {
eprintln!("schema path: {}", cli.schema);
}
match run(cli).await {
Ok(()) => ExitCode::SUCCESS,
Err(e) => {
eprintln!("ruprizzle: {e}");
ExitCode::FAILURE
}
}
}
async fn run(cli: Cli) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let _ = dotenvy::dotenv();
match &cli.command {
Command::Init { provider } => init(&cli.schema, provider),
Command::Generate { watch: true } => generate_watch(&cli.schema).await,
Command::Generate { watch: false } => generate(&cli.schema),
Command::Validate => validate(&cli.schema),
Command::Format => format_schema(&cli.schema),
Command::Migrate(MigrateCommand::Dev {
name,
accept_data_loss,
create_only,
}) => {
migrate_dev(
&cli.schema,
"migrations",
name.as_ref(),
*accept_data_loss,
*create_only,
cli.verbose,
)
.await
}
Command::Migrate(MigrateCommand::Deploy) => {
migrate_deploy(&cli.schema, "migrations", false, cli.verbose).await
}
Command::Migrate(MigrateCommand::Status) => {
migrate_status(&cli.schema, "migrations", cli.verbose).await
}
Command::Migrate(MigrateCommand::Resolve { id }) => {
migrate_resolve(&cli.schema, "migrations", id, cli.verbose).await
}
Command::Migrate(MigrateCommand::Reset { force }) => {
if !force {
return Err("migrate reset is destructive; pass --force to proceed".into());
}
migrate_reset(&cli.schema, "migrations", cli.verbose).await
}
Command::Migrate(MigrateCommand::Squash { name, force }) => {
if !force {
return Err("migrate squash rewrites history; pass --force to proceed".into());
}
migrate_squash(&cli.schema, "migrations", name.as_ref(), cli.verbose).await
}
Command::Db(DbCommand::Push { accept_data_loss }) => {
db_push(&cli.schema, "migrations", *accept_data_loss, cli.verbose).await
}
Command::Db(DbCommand::Pull) => db_pull(&cli.schema, cli.verbose).await,
Command::Db(DbCommand::Seed) => db_seed(&cli.schema, cli.verbose).await,
Command::Lsp { stdio } => run_lsp(*stdio).await,
Command::Check { manifest, format } => run_check(&cli.schema, manifest, format),
#[cfg(feature = "studio")]
Command::Studio {
port,
host,
allow_writes,
yes_i_know,
no_browser,
token,
} => {
let db_url = resolve_database_url(&cli.schema, cli.verbose).ok();
let config = studio::StudioConfig {
port: *port,
host: host.clone(),
schema_path: PathBuf::from(&cli.schema),
database_url: db_url,
allow_writes: *allow_writes,
yes_i_know: *yes_i_know,
no_browser: *no_browser,
auth_token: token.clone(),
};
studio::run_studio(config).await
}
}
}
#[derive(Debug)]
struct DiagnosticReport(miette::Report);
impl std::fmt::Display for DiagnosticReport {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:?}", self.0)
}
}
impl std::error::Error for DiagnosticReport {}
async fn run_lsp(stdio: bool) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if !stdio {
return Err("only --stdio transport is currently supported".into());
}
ruprizzle_lsp::run_stdio().await;
Ok(())
}
fn run_check(
schema_path: &str,
manifest_path: &str,
format_str: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let source = std::fs::read_to_string(manifest_path)?;
let manifest: ruprizzle_check::QueryManifest = serde_json::from_str(&source)?;
let report_format: ruprizzle_check::ReportFormat = format_str
.parse()
.map_err(std::convert::Into::<Box<dyn std::error::Error + Send + Sync>>::into)?;
let (schema, _, _) = parse_schema(schema_path)?;
let errors = ruprizzle_check::validate_manifest(&schema, &manifest);
let output = ruprizzle_check::format_report(&errors, report_format, manifest_path);
if errors.is_empty() {
if !output.is_empty() {
println!("{output}");
}
return Ok(());
}
eprintln!("{output}");
Err(format!("{} query(s) failed validation", errors.len()).into())
}
fn parse_schema(
path: &str,
) -> Result<(Schema, String, Vec<SchemaError>), Box<dyn std::error::Error + Send + Sync>> {
let source = fs::read_to_string(path)?;
match ruprizzle_parser::parse_with_warnings(path, &source) {
Ok((schema, warnings)) => Ok((schema, source, warnings)),
Err(errors) => {
let report = miette::Report::new(*errors);
Err(Box::new(DiagnosticReport(report)))
}
}
}
fn print_warnings(source: &str, warnings: Vec<SchemaError>) {
for warning in warnings {
let report = miette::Report::new(warning).with_source_code(source.to_owned());
eprintln!("{report:?}");
}
}
fn init(schema_path: &str, provider: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let Some(provider) = Provider::parse(provider) else {
return Err(format!(
"unknown provider `{provider}`; supported: {}",
Provider::ALL
.iter()
.map(Provider::as_str)
.collect::<Vec<_>>()
.join(", ")
)
.into());
};
let provider_str = provider.as_str();
let schema_file = Path::new(schema_path);
let base = schema_file.parent().unwrap_or(Path::new("."));
if schema_file.exists() {
return Err(format!("schema file already exists: {schema_path}").into());
}
let default_url = match provider {
Provider::Postgres => "postgres://localhost:5432/postgres?sslmode=disable",
Provider::Sqlite => "sqlite:test.db?mode=rwc",
Provider::Mysql => "mysql://localhost:3306/app",
};
fs::create_dir_all(base)?;
let schema_content = format!(
r#"datasource db {{
provider = "{provider_str}"
url = env("DATABASE_URL")
}}
generator client {{
output = "src/db"
module_name = "db"
}}
// model User {{
// id Uuid @id @default(uuid7())
// email String @unique
// name String?
// }}
"#
);
fs::write(schema_file, schema_content)?;
let env_path = base.join(".env");
if !env_path.exists() {
fs::write(&env_path, format!("DATABASE_URL={default_url}\n"))?;
}
let gitignore = base.join(".gitignore");
let mut gitignore_content = if gitignore.exists() {
fs::read_to_string(&gitignore)?
} else {
String::new()
};
if !gitignore_content.lines().any(|l| l.trim() == "/src/db") {
if !gitignore_content.is_empty() && !gitignore_content.ends_with('\n') {
gitignore_content.push('\n');
}
gitignore_content.push_str("/src/db\n");
fs::write(&gitignore, gitignore_content)?;
}
let migrations = base.join("migrations");
fs::create_dir_all(&migrations)?;
fs::write(
migrations.join("README.md"),
"# Migrations\n\nEach subdirectory is one migration. It must contain:\n\n- `up.sql` — changes to apply\n- `down.sql` — changes to roll back\n- `meta.json` — migration metadata\n\nDo not edit migrations that have already been applied.\n",
)?;
println!("✓ Initialised ruprizzle (provider: {provider_str})");
println!();
println!(" Next:");
println!(" 1. edit schema.ruprizzle");
println!(" 2. ruprizzle migrate dev --name init");
println!(" 3. cargo add ruprizzle");
Ok(())
}
fn output_dir(schema_path: &str, schema: &Schema) -> PathBuf {
Path::new(schema_path)
.parent()
.unwrap_or(Path::new("."))
.join(&schema.generator.output)
}
fn write_generated_files(
out_dir: &Path,
files: &BTreeMap<String, String>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let file_name = out_dir
.file_name()
.and_then(|s| s.to_str())
.unwrap_or("out");
let tmp = out_dir.with_file_name(format!("{file_name}.tmp"));
let old = out_dir.with_file_name(format!("{file_name}.old"));
if tmp.exists() {
fs::remove_dir_all(&tmp)?;
}
if old.exists() {
fs::remove_dir_all(&old)?;
}
fs::create_dir_all(&tmp)?;
for (rel, content) in files {
let path = tmp.join(rel);
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(&path, content)?;
}
if out_dir.exists() {
fs::rename(out_dir, &old)?;
fs::rename(&tmp, out_dir)?;
fs::remove_dir_all(&old)?;
} else {
fs::rename(&tmp, out_dir)?;
}
Ok(())
}
fn generate_from(
schema: &Schema,
source: &str,
out_dir: &Path,
warnings: Vec<SchemaError>,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
print_warnings(source, warnings);
let mod_existed = out_dir.join("mod.rs").exists();
let files = generate_all(schema);
write_generated_files(out_dir, &files)?;
let hash = schema.fingerprint();
println!(
"Generated {} ({} files, hash {hash:.8})",
out_dir.display(),
files.len()
);
if !mod_existed {
println!("add mod {}; to src/lib.rs", schema.generator.module_name);
}
Ok(hash)
}
fn generate(schema_path: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (schema, source, warnings) = parse_schema(schema_path)?;
let out_dir = output_dir(schema_path, &schema);
let _ = generate_from(&schema, &source, &out_dir, warnings)?;
Ok(())
}
async fn generate_watch(schema_path: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (tx, mut rx) = tokio::sync::mpsc::unbounded_channel::<()>();
let path = Path::new(schema_path).to_path_buf();
let parent = path
.parent()
.map_or_else(|| PathBuf::from("."), std::convert::Into::into);
let file_name: OsString = path
.file_name()
.map_or_else(OsString::new, std::convert::Into::into);
let mut watcher = notify::recommended_watcher(move |res: notify::Result<notify::Event>| {
if let Ok(event) = res {
if event
.paths
.iter()
.any(|p| p.file_name() == file_name.as_os_str().into())
{
let _ = tx.send(());
}
}
})
.map_err(|e| format!("failed to start file watcher: {e}"))?;
watcher
.watch(&parent, RecursiveMode::NonRecursive)
.map_err(|e| format!("failed to watch {}: {e}", parent.display()))?;
let mut last_hash: Option<String> = None;
let mut last_err = match try_generate(schema_path, &mut last_hash) {
Ok(()) => false,
Err(e) => {
eprintln!("ruprizzle: {e}");
true
}
};
loop {
tokio::select! {
Some(()) = rx.recv() => {
tokio::select! {
() = tokio::time::sleep(Duration::from_millis(150)) => {}
_ = tokio::signal::ctrl_c() => break,
}
while rx.try_recv().is_ok() {}
match try_generate(schema_path, &mut last_hash) {
Ok(()) => {
if last_err {
eprintln!("(error cleared)");
}
last_err = false;
}
Err(e) => {
eprintln!("ruprizzle: {e}");
last_err = true;
}
}
}
_ = tokio::signal::ctrl_c() => break,
}
}
Ok(())
}
fn try_generate(
schema_path: &str,
last_hash: &mut Option<String>,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (schema, source, warnings) = parse_schema(schema_path)?;
let hash = schema.fingerprint();
if last_hash.as_ref() == Some(&hash) {
return Ok(());
}
let out_dir = output_dir(schema_path, &schema);
let _ = generate_from(&schema, &source, &out_dir, warnings)?;
*last_hash = Some(hash);
Ok(())
}
fn validate(schema_path: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (schema, source, mut warnings) = parse_schema(schema_path)?;
let dialect = dialect_for(schema.datasource.provider);
let caps = check_schema_capabilities(dialect, &schema);
warnings.extend(caps);
print_warnings(&source, warnings);
println!("{schema_path} is valid.");
Ok(())
}
fn format_schema(schema_path: &str) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (_, source, warnings) = parse_schema(schema_path)?;
print_warnings(&source, warnings);
println!("schema is already canonical");
Ok(())
}
fn snapshot_path(migrations_dir: &str) -> PathBuf {
Path::new(migrations_dir)
.join(".ruprizzle")
.join("snapshot.ruprizzle")
}
fn empty_schema_like(schema: &Schema) -> Schema {
Schema {
version: ruprizzle_core::ir::IR_VERSION,
datasource: schema.datasource.clone(),
generator: schema.generator.clone(),
enums: IndexMap::default(),
models: IndexMap::default(),
relations: Vec::new(),
}
}
fn load_snapshot(
path: &Path,
schema: &Schema,
) -> Result<Schema, Box<dyn std::error::Error + Send + Sync>> {
if !path.exists() {
return Ok(empty_schema_like(schema));
}
let source = fs::read_to_string(path)?;
Ok(serde_json::from_str(&source)?)
}
fn save_snapshot(
path: &Path,
schema: &Schema,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
if let Some(parent) = path.parent() {
fs::create_dir_all(parent)?;
}
fs::write(path, serde_json::to_string_pretty(schema)?)?;
Ok(())
}
fn destructive_descriptions(changes: &[Change]) -> Vec<String> {
changes
.iter()
.filter(|c| c.is_destructive())
.map(Change::description)
.collect()
}
fn migration_id(name: Option<&String>) -> String {
#[allow(clippy::cast_possible_truncation)]
let now = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap_or_default()
.as_secs();
match name {
Some(n) => format!("{now}_{n}"),
None => now.to_string(),
}
}
fn write_migration(
migrations_dir: &str,
id: &str,
up: &str,
down: &str,
destructive: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let dir = Path::new(migrations_dir).join(id);
if dir.exists() {
return Err(format!("migration `{id}` already exists").into());
}
fs::create_dir_all(&dir)?;
fs::write(dir.join("up.sql"), up)?;
fs::write(dir.join("down.sql"), down)?;
let meta = MigrationMeta {
id: id.to_owned(),
checksum: compute_checksum(up),
destructive,
ruprizzle_version: env!("CARGO_PKG_VERSION").to_owned(),
};
fs::write(dir.join("meta.json"), serde_json::to_string_pretty(&meta)?)?;
Ok(())
}
async fn execute_statements(
pool: &Pool,
sql: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let tx = Tx::begin(pool).await?;
for (idx, stmt) in split_statements(sql).iter().enumerate() {
let sql = stmt.trim();
if sql.is_empty() || sql.starts_with("-- ") || sql.starts_with("/*") {
continue;
}
if let Err(e) = tx.execute_raw(Cow::Owned(sql.to_owned()), Vec::new()).await {
return Err(format!("statement {} failed: {e}\n sql: {sql}", idx + 1).into());
}
}
tx.commit().await?;
Ok(())
}
async fn migrate_dev(
schema_path: &str,
migrations_dir: &str,
name: Option<&String>,
accept_data_loss: bool,
create_only: bool,
verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (schema, source, mut warnings) = parse_schema(schema_path)?;
let dialect = dialect_for(schema.datasource.provider);
warnings.extend(check_schema_capabilities(dialect, &schema));
print_warnings(&source, warnings);
let url = resolve_database_url(schema_path, verbose)?;
let pool = connect(&url).await?;
fs::create_dir_all(migrations_dir)?;
let snapshot = snapshot_path(migrations_dir);
let prev = load_snapshot(&snapshot, &schema)?;
let changes = diff(&prev, &schema);
for suggestion in suggest_renames(&prev, &schema, &changes) {
eprintln!(
"Possible rename in {}: `{}` -> `{}`. Add `@renamedFrom(\"{}\")` to the new field to confirm; no rename was inferred.",
suggestion.model, suggestion.from_column, suggestion.to_column, suggestion.from
);
}
let destructive = destructive_descriptions(&changes);
if !destructive.is_empty() && !accept_data_loss {
return Err(format!(
"destructive changes detected; pass --accept-data-loss to proceed:\n - {}",
destructive.join("\n - ")
)
.into());
}
let up = up_sql(&prev, &schema, dialect);
let down = down_sql(&prev, &schema, dialect);
let id = migration_id(name);
let is_destructive = changes.iter().any(Change::is_destructive);
write_migration(migrations_dir, &id, &up, &down, is_destructive)?;
println!("Created migration {id}");
if create_only {
return Ok(());
}
let migrator = Migrator::new(migrations_dir);
let report = migrator.apply_all(&pool, accept_data_loss).await?;
if report.applied.is_empty() {
println!("No pending migrations.");
} else {
for applied in &report.applied {
println!("Applied {applied}");
}
println!("Done in {:?}", report.duration);
}
let out_dir = output_dir(schema_path, &schema);
let _ = generate_from(&schema, &source, &out_dir, Vec::new())?;
save_snapshot(&snapshot, &schema)?;
Ok(())
}
async fn migrate_resolve(
schema_path: &str,
migrations_dir: &str,
id: &str,
verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (_, source, warnings) = parse_schema(schema_path)?;
print_warnings(&source, warnings);
let url = resolve_database_url(schema_path, verbose)?;
let pool = connect(&url).await?;
let migrator = Migrator::new(migrations_dir);
migrator.resolve(&pool, id).await?;
println!("Resolved {id}");
Ok(())
}
async fn migrate_reset(
schema_path: &str,
migrations_dir: &str,
verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (schema, source, mut warnings) = parse_schema(schema_path)?;
let dialect = dialect_for(schema.datasource.provider);
warnings.extend(check_schema_capabilities(dialect, &schema));
print_warnings(&source, warnings);
let url = resolve_database_url(schema_path, verbose)?;
let pool = connect(&url).await?;
let migrator = Migrator::new(migrations_dir);
migrator.reset(&pool, dialect).await?;
let report = migrator.apply_all(&pool, true).await?;
if report.applied.is_empty() {
println!("No pending migrations.");
} else {
for applied in &report.applied {
println!("Applied {applied}");
}
println!("Done in {:?}", report.duration);
}
let out_dir = output_dir(schema_path, &schema);
let _ = generate_from(&schema, &source, &out_dir, Vec::new())?;
save_snapshot(&snapshot_path(migrations_dir), &schema)?;
println!("Reset complete");
Ok(())
}
async fn migrate_deploy(
schema_path: &str,
migrations_dir: &str,
accept_data_loss: bool,
verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (_, source, warnings) = parse_schema(schema_path)?;
print_warnings(&source, warnings);
let url = resolve_database_url(schema_path, verbose)?;
let pool = connect(&url).await?;
if !Path::new(migrations_dir).exists() {
return Err(format!("migrations directory not found: {migrations_dir}").into());
}
let migrator = Migrator::new(migrations_dir);
let report = migrator.apply_all(&pool, accept_data_loss).await?;
if report.applied.is_empty() {
println!("No pending migrations.");
} else {
for id in &report.applied {
println!("Applied {id}");
}
println!("Done in {:?}", report.duration);
}
Ok(())
}
async fn migrate_status(
schema_path: &str,
migrations_dir: &str,
verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (_, source, warnings) = parse_schema(schema_path)?;
print_warnings(&source, warnings);
let url = resolve_database_url(schema_path, verbose)?;
let pool = connect(&url).await?;
let migrator = Migrator::new(migrations_dir);
let status = migrator.status(&pool).await?;
println!("Applied ({}):", status.applied.len());
for id in &status.applied {
println!(" {id}");
}
if status.pending.is_empty() {
println!("No pending migrations.");
} else {
println!("Pending ({}):", status.pending.len());
for id in &status.pending {
println!(" {id}");
}
}
Ok(())
}
async fn migrate_squash(
schema_path: &str,
migrations_dir: &str,
name: Option<&String>,
verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (schema, source, warnings) = parse_schema(schema_path)?;
print_warnings(&source, warnings);
let url = resolve_database_url(schema_path, verbose)?;
let pool = connect(&url).await?;
let migrator = Migrator::new(migrations_dir);
let status = migrator.status(&pool).await?;
if !status.pending.is_empty() {
return Err(format!(
"cannot squash with pending migrations; deploy first: {}",
status.pending.join(", ")
)
.into());
}
if status.applied.is_empty() {
return Err("cannot squash an empty migration history".into());
}
migrator.verify_checksums(&pool).await?;
let dialect = dialect_for(schema.datasource.provider);
let empty = empty_schema_like(&schema);
let up = up_sql(&empty, &schema, dialect);
let down = down_sql(&empty, &schema, dialect);
let default_name = "baseline".to_owned();
let baseline_id = migration_id(name.or(Some(&default_name)));
let migrations_path = Path::new(migrations_dir);
let archive = migrations_path.join(".archive").join(&baseline_id);
fs::create_dir_all(&archive)?;
for migration in migrator.migrations()? {
fs::rename(
migrations_path.join(&migration.id),
archive.join(&migration.id),
)?;
}
write_migration(
migrations_dir,
&baseline_id,
&up,
&down,
down.to_ascii_lowercase().contains("drop table"),
)?;
pool.execute_raw(
Cow::Owned("DELETE FROM _ruprizzle_migrations".to_owned()),
Vec::new(),
)
.await?;
migrator.resolve(&pool, &baseline_id).await?;
println!(
"Squashed {} migration{} into {baseline_id}; archived old files under {}",
status.applied.len(),
if status.applied.len() == 1 { "" } else { "s" },
archive.display()
);
Ok(())
}
async fn db_push(
schema_path: &str,
migrations_dir: &str,
accept_data_loss: bool,
verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (schema, source, mut warnings) = parse_schema(schema_path)?;
let dialect = dialect_for(schema.datasource.provider);
warnings.extend(check_schema_capabilities(dialect, &schema));
print_warnings(&source, warnings);
eprintln!(
"Warning: db push is for prototyping only; it bypasses migration history and cannot be reproduced on another machine."
);
let url = resolve_database_url(schema_path, verbose)?;
let pool = connect(&url).await?;
let snapshot = snapshot_path(migrations_dir);
let prev = load_snapshot(&snapshot, &schema)?;
let changes = diff(&prev, &schema);
let destructive = destructive_descriptions(&changes);
if !destructive.is_empty() && !accept_data_loss {
return Err(format!(
"destructive changes detected; pass --accept-data-loss to proceed:\n - {}",
destructive.join("\n - ")
)
.into());
}
let up = up_sql(&prev, &schema, dialect);
execute_statements(&pool, &up).await?;
save_snapshot(&snapshot, &schema)?;
println!("Pushed schema to database");
Ok(())
}
async fn db_pull(
schema_path: &str,
verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (schema, source, warnings) = parse_schema(schema_path)?;
print_warnings(&source, warnings);
let url = resolve_database_url(schema_path, verbose)?;
let pool = connect(&url).await?;
let database = ruprizzle_migrate::introspect::pull(&pool).await?;
if database.provider != schema.datasource.provider {
return Err(format!(
"database provider `{}` does not match schema provider `{}`",
database.provider, schema.datasource.provider
)
.into());
}
let pulled = introspect::render_schema(&database, &schema.datasource.url, &schema.generator);
fs::write(schema_path, pulled)?;
println!(
"Pulled {} table{} into {schema_path}",
database.tables.len(),
if database.tables.len() == 1 { "" } else { "s" }
);
Ok(())
}
async fn db_seed(
schema_path: &str,
verbose: bool,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let (schema, source, warnings) = parse_schema(schema_path)?;
print_warnings(&source, warnings);
let base = Path::new(schema_path).parent().unwrap_or(Path::new("."));
let seed = base.join("seeds").join("main.json");
let legacy_sql = base.join("seeds").join("main.sql");
let url = resolve_database_url(schema_path, verbose)?;
let pool = connect(&url).await?;
if seed.exists() {
let document = fs::read_to_string(&seed)?;
let rows = seed::apply(&schema, &document, &pool).await?;
println!(
"Seeded {rows} row{} from {}",
if rows == 1 { "" } else { "s" },
seed.display()
);
} else if legacy_sql.exists() {
let sql = fs::read_to_string(&legacy_sql)?;
execute_statements(&pool, &sql).await?;
println!("Ran legacy SQL seed {}", legacy_sql.display());
} else {
return Err("create seeds/main.json".into());
}
Ok(())
}
fn resolve_database_url(
schema_path: &str,
verbose: bool,
) -> Result<String, Box<dyn std::error::Error + Send + Sync>> {
if let Ok(url) = std::env::var("RUPRIZZLE_DATABASE_URL") {
if verbose {
eprintln!("database url: RUPRIZZLE_DATABASE_URL env var");
}
return Ok(url);
}
let (schema, _, _) = parse_schema(schema_path)?;
match &schema.datasource.url {
DatasourceUrl::Literal(url) => {
if verbose {
eprintln!("database url: datasource literal");
}
Ok(url.clone())
}
DatasourceUrl::Env(var) => {
let url = std::env::var(var).map_err(|_| {
format!("environment variable {var} (from datasource.url) is not set")
})?;
if verbose {
eprintln!("database url: {var} env var (from datasource)");
}
Ok(url)
}
}
}
async fn connect(url: &str) -> Result<Pool, Box<dyn std::error::Error + Send + Sync>> {
Ok(ruprizzle::connect(url).await?)
}
#[cfg(test)]
mod tests {
use super::*;
use clap::CommandFactory;
#[test]
fn cli_definition_is_valid() {
Cli::command().debug_assert();
}
#[test]
fn dev_and_deploy_are_separate_subcommands() {
assert!(Cli::try_parse_from(["ruprizzle", "migrate", "dev", "--accept-data-loss"]).is_ok());
assert!(
Cli::try_parse_from(["ruprizzle", "migrate", "deploy", "--accept-data-loss"]).is_err()
);
}
#[test]
fn schema_path_defaults_and_can_be_overridden() {
let cli = Cli::try_parse_from(["ruprizzle", "validate"]).unwrap();
assert_eq!(cli.schema, "schema.ruprizzle");
let cli =
Cli::try_parse_from(["ruprizzle", "--schema", "db/app.ruprizzle", "validate"]).unwrap();
assert_eq!(cli.schema, "db/app.ruprizzle");
}
#[tokio::test]
async fn migrate_squash_archives_history_and_marks_baseline_applied() {
use ruprizzle::Executor;
use std::borrow::Cow;
let root =
std::env::temp_dir().join(format!("ruprizzle-cli-squash-{}", std::process::id()));
let migrations = root.join("migrations");
std::fs::create_dir_all(&migrations).unwrap();
let schema_path = root.join("schema.ruprizzle");
let database_path = root.join("database.sqlite");
let file = database_path.to_string_lossy().replace('\\', "/");
let url = format!("sqlite:///{file}?mode=rwc");
std::fs::write(
&schema_path,
format!(
r#"datasource db {{ provider = "sqlite" url = "{url}" }}
generator client {{ provider = "rust" }}
model User {{
id Int @id
name String
}}
"#
),
)
.unwrap();
let initial = migrations.join("20240101_init");
std::fs::create_dir_all(&initial).unwrap();
std::fs::write(
initial.join("up.sql"),
"CREATE TABLE users (id INTEGER PRIMARY KEY, name TEXT NOT NULL);",
)
.unwrap();
std::fs::write(initial.join("down.sql"), "DROP TABLE users;").unwrap();
let pool = ruprizzle::connect(&url).await.unwrap();
Migrator::new(&migrations)
.apply_all(&pool, false)
.await
.unwrap();
migrate_squash(
schema_path.to_str().unwrap(),
migrations.to_str().unwrap(),
None,
false,
)
.await
.unwrap();
let status = Migrator::new(&migrations).status(&pool).await.unwrap();
assert_eq!(status.applied.len(), 1);
assert!(status.pending.is_empty());
assert!(migrations.join(".archive").exists());
let baseline_count = std::fs::read_dir(&migrations)
.unwrap()
.filter_map(Result::ok)
.filter(|entry| entry.path().join("up.sql").exists())
.count();
assert_eq!(baseline_count, 1);
pool.execute_raw(Cow::Owned("SELECT 1".to_owned()), Vec::new())
.await
.unwrap();
pool.close().await;
let _ = std::fs::remove_dir_all(root);
}
#[tokio::test]
async fn db_pull_rewrites_schema_from_sqlite() {
use ruprizzle::Executor;
use std::borrow::Cow;
let base = std::env::temp_dir();
let stem = format!("ruprizzle-cli-pull-{}", std::process::id());
let database_path = base.join(format!("{stem}.sqlite"));
let schema_path = base.join(format!("{stem}.ruprizzle"));
let file = database_path.to_string_lossy().replace('\\', "/");
let url = format!("sqlite:///{file}?mode=rwc");
let schema = format!(
r#"datasource db {{ provider = "sqlite" url = "{url}" }}
generator client {{ provider = "rust" output = "src/db" module_name = "db" }}
"#
);
std::fs::write(&schema_path, schema).unwrap();
let pool = ruprizzle::connect(&url).await.unwrap();
pool.execute_raw(
Cow::Owned(
"CREATE TABLE users (id INTEGER PRIMARY KEY, email TEXT NOT NULL UNIQUE)"
.to_owned(),
),
Vec::new(),
)
.await
.unwrap();
pool.close().await;
db_pull(schema_path.to_str().unwrap(), false).await.unwrap();
let pulled = std::fs::read_to_string(&schema_path).unwrap();
let parsed = ruprizzle_parser::parse("pulled", &pulled).unwrap();
assert_eq!(parsed.model("Users").unwrap().table, "users");
assert!(pulled.contains("email String"));
let _ = std::fs::remove_file(database_path);
let _ = std::fs::remove_file(schema_path);
}
}