diff --git a/butane/src/lib.rs b/butane/src/lib.rs index f01353eb..59a06340 100644 --- a/butane/src/lib.rs +++ b/butane/src/lib.rs @@ -17,8 +17,13 @@ pub use butane_core::{ Result, SqlType, SqlVal, SqlValRef, ToSql, }; +pub mod _filenames { + //! Filenames used in butane. + pub use butane_core::_filenames::*; +} + pub mod db { - //! Database helpers + //! Database helpers. pub use butane_core::db::*; } diff --git a/butane_cli/src/lib.rs b/butane_cli/src/lib.rs index 9b96027f..30c93dfc 100644 --- a/butane_cli/src/lib.rs +++ b/butane_cli/src/lib.rs @@ -12,6 +12,7 @@ use std::{ path::{Path, PathBuf}, }; +use butane::_filenames::{BUTANE_DIRNAME, MIGRATIONS_DIRNAME}; use butane::migrations::{ copy_migration, FsMigrations, MemMigrations, Migration, MigrationMut, Migrations, MigrationsMut, }; @@ -304,7 +305,7 @@ pub fn clean(base_dir: &Path) -> Result<()> { } pub fn get_migrations(base_dir: &Path) -> Result { - let root = base_dir.join("migrations"); + let root = base_dir.join(MIGRATIONS_DIRNAME); if !root.exists() { eprintln!("No butane migrations directory found. Add at least one model to your project and build."); std::process::exit(1); @@ -342,7 +343,7 @@ pub fn find_butane_workspace_member_paths() -> Result> { // Find all workspace member with a .butane for member in workspace_members { let package_dir = extract_package_directory(&metadata.packages, member)?; - let member_butane_dir = package_dir.join(".butane/"); + let member_butane_dir = package_dir.join(BUTANE_DIRNAME); if member_butane_dir.exists() { possible_directories.push(package_dir); diff --git a/butane_cli/src/main.rs b/butane_cli/src/main.rs index f0d9402c..79f49634 100644 --- a/butane_cli/src/main.rs +++ b/butane_cli/src/main.rs @@ -1,5 +1,6 @@ use std::path::PathBuf; +use butane::_filenames::BUTANE_DIRNAME; use butane_cli::{ base_dir, clean, clear_data, collapse_migrations, delete_table, detach_latest_migration, embed, get_migrations, handle_error, list_migrations, migrate, Result, @@ -109,7 +110,7 @@ However if the migration has been manually edited, it will need to be manually r .arg_required_else_help(true); let args = app.get_matches(); let mut base_dir = args.get_one::("path").unwrap().clone(); - base_dir.push(".butane"); + base_dir.push(BUTANE_DIRNAME); // List any detached migrations. if let Ok(ms) = get_migrations(&base_dir) { diff --git a/butane_cli/tests/embed.rs b/butane_cli/tests/embed.rs index b5e28f02..8c1dacda 100644 --- a/butane_cli/tests/embed.rs +++ b/butane_cli/tests/embed.rs @@ -1,3 +1,5 @@ +use butane::_filenames::BUTANE_DIRNAME; + #[test] fn working_dir_path() { let path = butane_cli::working_dir_path(); @@ -8,7 +10,8 @@ fn working_dir_path() { fn embed() { let example_dir = std::env::current_dir() .unwrap() - .join("../examples/getting_started/.butane"); + .join("../examples/getting_started") + .join(BUTANE_DIRNAME); assert!(example_dir.exists()); butane_cli::embed(&example_dir).unwrap(); } diff --git a/butane_codegen/src/lib.rs b/butane_codegen/src/lib.rs index 08468ebf..ab0df68c 100644 --- a/butane_codegen/src/lib.rs +++ b/butane_codegen/src/lib.rs @@ -7,6 +7,7 @@ extern crate proc_macro; use std::path::PathBuf; +use butane_core::_filenames::*; use butane_core::migrations::adb::{DeferredSqlType, TypeIdentifier}; use butane_core::{codegen, make_compile_error, migrations, SqlType}; use proc_macro::TokenStream; @@ -219,8 +220,8 @@ fn migrations_dir() -> PathBuf { let mut dir = PathBuf::from( std::env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR expected to be set"), ); - dir.push(".butane"); - dir.push("migrations"); + dir.push(BUTANE_DIRNAME); + dir.push(MIGRATIONS_DIRNAME); dir } diff --git a/butane_core/src/db/mod.rs b/butane_core/src/db/mod.rs index 76c848ee..41d563de 100644 --- a/butane_core/src/db/mod.rs +++ b/butane_core/src/db/mod.rs @@ -23,6 +23,7 @@ use std::path::Path; use serde::{Deserialize, Serialize}; +use crate::_filenames::CONNECTION_JSON_FILENAME; use crate::query::BoolExpr; use crate::{migrations::adb, Error, Result, SqlVal, SqlValRef}; @@ -112,7 +113,7 @@ impl ConnectionSpec { f.write_all(serde_json::to_string_pretty(self)?.as_bytes()) .map_err(|e| e.into()) } - /// Load a previously saved connection spec + /// Load a previously saved connection spec. pub fn load(path: impl AsRef) -> Result { let path = conn_complete_if_dir(path.as_ref()); serde_json::from_reader(fs::File::open(path)?).map_err(|e| e.into()) @@ -127,7 +128,8 @@ impl ConnectionSpec { fn conn_complete_if_dir(path: &Path) -> Cow { if path.is_dir() { - Cow::from(path.join("connection.json")) + //let path = path.join(CONNECTION_JSON_FILENAME); + Cow::from(path.join(CONNECTION_JSON_FILENAME)) } else { Cow::from(path) } diff --git a/butane_core/src/lib.rs b/butane_core/src/lib.rs index f47c5a07..188a022d 100644 --- a/butane_core/src/lib.rs +++ b/butane_core/src/lib.rs @@ -29,6 +29,15 @@ use db::{BackendRow, Column, ConnectionMethods}; pub use query::Query; pub use sqlval::{AsPrimaryKey, FieldType, FromSql, PrimaryKeyType, SqlVal, SqlValRef, ToSql}; +/// Filenames used in butane. +pub mod _filenames { + /// Directory to hold butane metadata. + pub const BUTANE_DIRNAME: &str = ".butane"; + /// Filename for storing connection metadata. + pub const CONNECTION_JSON_FILENAME: &str = "connection.json"; + /// Directory to hold butane migration data. + pub const MIGRATIONS_DIRNAME: &str = "migrations"; +} /// Result type that uses [`crate::Error`]. pub type Result = std::result::Result; diff --git a/butane_test_helper/src/lib.rs b/butane_test_helper/src/lib.rs index 8cbe6556..8f3f6ba2 100644 --- a/butane_test_helper/src/lib.rs +++ b/butane_test_helper/src/lib.rs @@ -4,6 +4,7 @@ use std::path::PathBuf; use std::process::{ChildStderr, Command, Stdio}; use std::sync::Mutex; +use butane_core::_filenames::{BUTANE_DIRNAME, MIGRATIONS_DIRNAME}; use butane_core::db::{connect, get_backend, pg, sqlite, Backend, Connection, ConnectionSpec}; use butane_core::migrations::{self, MemMigrations, Migration, Migrations, MigrationsMut}; use once_cell::sync::Lazy; @@ -159,7 +160,8 @@ pub fn pg_connstr(data: &PgSetupData) -> String { pub fn setup_db(backend: Box, conn: &mut Connection, migrate: bool) { let mut root = std::env::current_dir().unwrap(); - root.push(".butane/migrations"); + root.push(BUTANE_DIRNAME); + root.push(MIGRATIONS_DIRNAME); let mut disk_migrations = migrations::from_root(&root); let disk_current = disk_migrations.current(); eprintln!("{:?}", disk_current); diff --git a/docs/getting-started.md b/docs/getting-started.md index 251b2424..6b3cd16d 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -51,6 +51,7 @@ connection parameters. At this point, we can add a method (in our use butane::db::{Connection, ConnectionSpec}; pub fn establish_connection() -> Connection { + let connection_spec = format!(".butane/{CONNECTIONS_JSON_FILENAME}"); butane::db::connect(&ConnectionSpec::load(".butane/connection.json").unwrap()).unwrap() } ``` diff --git a/example/src/main.rs b/example/src/main.rs index bc60dec1..c1763671 100644 --- a/example/src/main.rs +++ b/example/src/main.rs @@ -1,4 +1,6 @@ #![allow(dead_code)] + +use butane::_filenames::BUTANE_DIRNAME; use butane::db::{Connection, ConnectionSpec}; use butane::prelude::*; use butane::{find, model, query, AutoPk, Error, ForeignKey, Many}; @@ -86,7 +88,7 @@ fn query() -> Result<()> { fn establish_connection() -> Result { let mut cwd = std::env::current_dir()?; - cwd.push(".butane"); + cwd.push(BUTANE_DIRNAME); let spec = ConnectionSpec::load(cwd)?; let conn = butane::db::connect(&spec)?; Ok(conn) diff --git a/example/tests/test_cli.rs b/example/tests/test_cli.rs index 2de9211c..4425009c 100644 --- a/example/tests/test_cli.rs +++ b/example/tests/test_cli.rs @@ -1,9 +1,10 @@ use assert_cmd::Command; +use butane::_filenames::{BUTANE_DIRNAME, CONNECTION_JSON_FILENAME}; #[test] fn test_migrate_and_query() { let db = "db.sqlite"; - let connspec = ".butane/connection.json"; + let connspec = format!("{BUTANE_DIRNAME}/{CONNECTION_JSON_FILENAME}"); // These files should have been removed by build.rs assert!(!std::path::Path::new(&db).exists()); diff --git a/examples/getting_started/src/lib.rs b/examples/getting_started/src/lib.rs index 137a4427..1f20bd9a 100644 --- a/examples/getting_started/src/lib.rs +++ b/examples/getting_started/src/lib.rs @@ -5,13 +5,15 @@ pub mod butane_migrations; pub mod models; +use butane::_filenames::{BUTANE_DIRNAME, CONNECTION_JSON_FILENAME}; use butane::db::{Connection, ConnectionSpec}; use butane::prelude::*; use models::{Blog, Post}; /// Load a [Connection]. pub fn establish_connection() -> Connection { - butane::db::connect(&ConnectionSpec::load(".butane/connection.json").unwrap()).unwrap() + let connspec = format!("{BUTANE_DIRNAME}/{CONNECTION_JSON_FILENAME}"); + butane::db::connect(&ConnectionSpec::load(connspec).unwrap()).unwrap() } /// Create a [Blog].