Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion butane/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,10 @@ pub mod prelude {
//! Its use is recommended, but not required. If not used, the use
//! of butane's macros may require some of its re-exports to be
//! used manually.
pub use butane_core::db::BackendConnection;

#[doc(no_inline)]
pub use crate::DataObject;
#[doc(no_inline)]
pub use crate::DataResult;
pub use butane_core::db::BackendConnection;
}
3 changes: 1 addition & 2 deletions butane/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ use butane::db::Connection;
use butane::{butane_type, find, model, query};
use butane::{colname, prelude::*};
use butane::{ForeignKey, ObjectState};
use butane_test_helper::*;
use chrono::{naive::NaiveDateTime, offset::Utc, DateTime};
use serde::Serialize;

use butane_test_helper::*;

#[butane_type]
pub type Whatsit = String;

Expand Down
1 change: 0 additions & 1 deletion butane/tests/common/blog.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use butane::prelude::*;
use butane::{dataresult, model};
use butane::{db::Connection, ForeignKey, Many, ObjectState};
use chrono::{naive::NaiveDateTime, offset::Utc};

#[cfg(feature = "fake")]
use fake::Dummy;

Expand Down
1 change: 0 additions & 1 deletion butane/tests/custom_enum_derived.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use butane::db::Connection;
use butane::prelude::*;
use butane::{model, query};
use butane::{FieldType, FromSql, ObjectState, SqlVal, ToSql};

use butane_test_helper::*;

#[derive(PartialEq, Eq, Debug, Clone, FieldType)]
Expand Down
4 changes: 2 additions & 2 deletions butane/tests/custom_pg.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
// We wrap everything in an inner module just so it's easier to have the feature gate in one place
#[cfg(feature = "pg")]
mod custom_pg {
use std::result::Result;

use butane::custom::{SqlTypeCustom, SqlValRefCustom};
use butane::prelude::*;
use butane::{butane_type, db::Connection, model, ObjectState};
use butane::{FieldType, FromSql, SqlType, SqlVal, SqlValRef, ToSql};
use butane_test_helper::{maketest, maketest_pg};

use std::result::Result;

// newtype so we can implement traits for it.
#[butane_type(Custom(POINT))]
#[derive(Debug, PartialEq, Clone)]
Expand Down
1 change: 0 additions & 1 deletion butane/tests/custom_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use butane::db::Connection;
use butane::prelude::*;
use butane::{butane_type, model, query};
use butane::{FieldType, FromSql, ObjectState, SqlType, SqlVal, SqlValRef, ToSql};

use butane_test_helper::*;

#[butane_type(Text)]
Expand Down
8 changes: 3 additions & 5 deletions butane/tests/fake.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
use butane::db::Connection;
use butane::{find, DataObject, ForeignKey};

use butane_test_helper::*;

mod common;

fn fake_blog_post(conn: Connection) {
use fake::{Fake, Faker};

use common::blog::{Blog, Post, Tag};
use common::blog::{Blog, Post, Tag};
use fake::{Fake, Faker};

fn fake_blog_post(conn: Connection) {
let mut fake_blog: Blog = Faker.fake();
fake_blog.save(&conn).unwrap();

Expand Down
3 changes: 1 addition & 2 deletions butane/tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use std::collections::HashMap;
use butane::model;
use butane::prelude::*;
use butane::{db::Connection, FieldType, ObjectState};
use butane_test_helper::*;
use serde::{Deserialize, Serialize};
use serde_json::Value;

use butane_test_helper::*;

#[model]
#[derive(PartialEq, Eq, Debug, Clone)]
struct FooJJ {
Expand Down
1 change: 0 additions & 1 deletion butane/tests/many.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use butane::db::Connection;
use butane::prelude::*;
use butane::{model, query::OrderDirection, Many, ObjectState};

use butane_test_helper::*;

mod common;
Expand Down
3 changes: 1 addition & 2 deletions butane/tests/migration-tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,12 @@ use butane::migrations::{
};
use butane::{db::Connection, prelude::*, SqlType, SqlVal};
use butane_core::codegen::{butane_type_with_migrations, model_with_migrations};
use butane_test_helper::*;
use proc_macro2::TokenStream;
use quote::quote;
use sqlparser::dialect::GenericDialect;
use sqlparser::parser::Parser as SqlParser;

use butane_test_helper::*;

#[test]
fn current_migration_basic() {
let tokens = quote! {
Expand Down
1 change: 0 additions & 1 deletion butane/tests/nullable.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use butane::db::Connection;
use butane::prelude::*;
use butane::{model, query};

use butane_test_helper::*;

#[model]
Expand Down
3 changes: 1 addition & 2 deletions butane/tests/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ use butane::db::Connection;
use butane::prelude::*;
use butane::query::BoolExpr;
use butane::{colname, filter, find, query, Many};
use chrono::{TimeZone, Utc};

use butane_test_helper::*;
use chrono::{TimeZone, Utc};

mod common;
use common::blog;
Expand Down
3 changes: 1 addition & 2 deletions butane/tests/r2d2.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use butane_test_helper::{pg_connspec, setup_db, sqlite_connspec};

use butane::db;
use butane_test_helper::{pg_connspec, setup_db, sqlite_connspec};
use r2d2_for_test as r2d2;

#[cfg(feature = "sqlite")]
Expand Down
3 changes: 1 addition & 2 deletions butane/tests/uuid.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use butane::model;
use butane::prelude::*;
use butane::{db::Connection, ObjectState};
use uuid_for_test::Uuid;

use butane_test_helper::*;
use uuid_for_test::Uuid;

#[model]
#[derive(PartialEq, Eq, Debug, Clone)]
Expand Down
4 changes: 3 additions & 1 deletion butane_cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,9 @@ pub fn clean(base_dir: &Path) -> Result<()> {
pub fn get_migrations(base_dir: &Path) -> Result<FsMigrations> {
let root = base_dir.join("migrations");
if !root.exists() {
eprintln!("No butane migrations directory found. Add at least one model to your project and build.");
eprintln!(
"No butane migrations directory found. Add at least one model to your project and build."
);
std::process::exit(1);
}
Ok(migrations::from_root(root))
Expand Down
3 changes: 1 addition & 2 deletions butane_cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use std::path::PathBuf;

use clap::{value_parser, Arg, ArgMatches};

use butane_cli::{
base_dir, clean, clear_data, collapse_migrations, delete_table, embed, handle_error,
list_migrations, migrate, Result,
};
use clap::{value_parser, Arg, ArgMatches};

fn main() {
let app = clap::Command::new("butane")
Expand Down
3 changes: 2 additions & 1 deletion butane_codegen/src/filter.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use super::*;
use proc_macro2::Span;
use proc_macro2::TokenStream as TokenStream2;
use quote::{quote, quote_spanned, ToTokens};
use syn::{spanned::Spanned, BinOp, Expr, ExprBinary, ExprMethodCall, ExprPath, Ident, LitStr};

use super::*;

pub fn for_expr(dbres: &Ident, expr: &Expr) -> TokenStream2 {
handle_expr(&quote!(<#dbres as butane::DataResult>::DBO::fields()), expr)
}
Expand Down
3 changes: 2 additions & 1 deletion butane_codegen/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
#![deny(missing_docs)]
extern crate proc_macro;

use std::path::PathBuf;

use butane_core::migrations::adb::{DeferredSqlType, TypeIdentifier};
use butane_core::{codegen, make_compile_error, migrations, SqlType};
use proc_macro::TokenStream;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::TokenTree;
use quote::quote;
use std::path::PathBuf;
use syn::{Expr, Ident};

mod filter;
Expand Down
7 changes: 4 additions & 3 deletions butane_core/src/codegen/dbobj.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
use super::*;
use crate::migrations::adb::{DeferredSqlType, TypeIdentifier};
use crate::SqlType;
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span};
use quote::{quote, quote_spanned};
use syn::{spanned::Spanned, Field, ItemStruct};

use super::*;
use crate::migrations::adb::{DeferredSqlType, TypeIdentifier};
use crate::SqlType;

// Configuration that can be specified with attributes to override default behavior
#[derive(Clone, Debug, Default)]
pub struct Config {
Expand Down
3 changes: 2 additions & 1 deletion butane_core/src/codegen/migration.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use syn::{Field, ItemStruct};

use super::*;
use crate::migrations::adb::{AColumn, ATable};
use crate::migrations::{MigrationMut, MigrationsMut};
use crate::Result;
use syn::{Field, ItemStruct};

pub fn write_table_to_disk<M>(
ms: &mut impl MigrationsMut<M = M>,
Expand Down
7 changes: 4 additions & 3 deletions butane_core/src/codegen/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
//! Code-generation backend

use crate::migrations::adb::{DeferredSqlType, TypeIdentifier, TypeKey};
use crate::migrations::{MigrationMut, MigrationsMut};
use crate::{SqlType, SqlVal};
use proc_macro2::TokenStream as TokenStream2;
use proc_macro2::{Ident, Span, TokenTree};
use quote::{quote, ToTokens};
Expand All @@ -13,6 +10,10 @@ use syn::{
MetaNameValue,
};

use crate::migrations::adb::{DeferredSqlType, TypeIdentifier, TypeKey};
use crate::migrations::{MigrationMut, MigrationsMut};
use crate::{SqlType, SqlVal};

const OPTION_TYNAMES: [&str; 2] = ["Option", "std::option::Option"];
const MANY_TYNAMES: [&str; 2] = ["Many", "butane::Many"];
const FKEY_TYNAMES: [&str; 2] = ["ForeignKey", "butane::ForeignKey"];
Expand Down
3 changes: 2 additions & 1 deletion butane_core/src/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@
//! source repository. Not supported for the Sqlite backend as Sqlite
//! supports a very limited set of types to begin with.

use serde::{Deserialize, Serialize};
use std::fmt;

use serde::{Deserialize, Serialize};

/// For use with [SqlType::Custom](crate::SqlType)
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
pub enum SqlTypeCustom {
Expand Down
5 changes: 3 additions & 2 deletions butane_core/src/db/connmethods.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
//! Not expected to be called directly by most users. Used by code
//! generated by `#[model]`, `query!`, and other macros.

use crate::query::{BoolExpr, Expr, Order};
use crate::{Result, SqlType, SqlVal, SqlValRef};
use std::ops::{Deref, DerefMut};
use std::vec::Vec;

use crate::query::{BoolExpr, Expr, Order};
use crate::{Result, SqlType, SqlVal, SqlValRef};

/// Methods available on a database connection. Most users do not need
/// to call these methods directly and will instead use methods on
/// [DataObject][crate::DataObject] or the `query!` macro. This trait is
Expand Down
11 changes: 6 additions & 5 deletions butane_core/src/db/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@
// may occur if no backends are selected
#![allow(unused)]

use std::borrow::Cow;
use std::fmt::Write;

#[cfg(feature = "datetime")]
use chrono::naive::NaiveDateTime;

use super::Column;
use crate::migrations::adb::{AColumn, TypeIdentifier};
use crate::query::Expr::{Condition, Placeholder, Val};
use crate::query::{BoolExpr::*, Expr, Join, Order, OrderDirection};
use crate::Error;
use crate::{query, Result, SqlType, SqlVal};
use std::borrow::Cow;
use std::fmt::Write;

#[cfg(feature = "datetime")]
use chrono::naive::NaiveDateTime;

pub trait PlaceholderSource {
fn next_placeholder(&mut self) -> Cow<str>;
Expand Down
15 changes: 8 additions & 7 deletions butane_core/src/db/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
//! what a `BackendConnection` can do, but allows using a single concrete type that is not tied to a particular
//! database backend. It is returned by the `connect` method.

use crate::query::BoolExpr;
use crate::{migrations::adb, Error, Result, SqlVal, SqlValRef};
use serde::{Deserialize, Serialize};
use std::borrow::Cow;
use std::fmt::Debug;
use std::fs;
use std::io::Write;
use std::ops::{Deref, DerefMut};
use std::path::Path;

use serde::{Deserialize, Serialize};

use crate::query::BoolExpr;
use crate::{migrations::adb, Error, Result, SqlVal, SqlValRef};

mod connmethods;
mod helper;
mod macros;
Expand All @@ -32,16 +34,15 @@ pub mod sqlite;

#[cfg(feature = "r2d2")]
pub mod r2;
pub use connmethods::{
BackendRow, BackendRows, Column, ConnectionMethods, MapDeref, QueryResult, RawQueryResult,
};
#[cfg(feature = "r2d2")]
pub use r2::ConnectionManager;

// Macros are always exported at the root of the crate
use crate::connection_method_wrapper;

pub use connmethods::{
BackendRow, BackendRows, Column, ConnectionMethods, MapDeref, QueryResult, RawQueryResult,
};

/// Database connection.
pub trait BackendConnection: ConnectionMethods + Debug + Send + 'static {
/// Begin a database transaction. The transaction object must be
Expand Down
16 changes: 9 additions & 7 deletions butane_core/src/db/pg.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
//! Postgresql database backend
use std::cell::RefCell;
use std::fmt::Write;

use bytes::BufMut;
#[cfg(feature = "datetime")]
use chrono::NaiveDateTime;
use postgres::fallible_iterator::FallibleIterator;
use postgres::GenericClient;

use super::connmethods::VecRows;
use super::helper;
use super::*;
use crate::custom::{SqlTypeCustom, SqlValRefCustom};
use crate::migrations::adb::{AColumn, ATable, Operation, TypeIdentifier, ADB};
use crate::{debug, query};
use crate::{Result, SqlType, SqlVal, SqlValRef};
use bytes::BufMut;
#[cfg(feature = "datetime")]
use chrono::NaiveDateTime;
use postgres::fallible_iterator::FallibleIterator;
use postgres::GenericClient;
use std::cell::RefCell;
use std::fmt::Write;

/// The name of the postgres backend.
pub const BACKEND_NAME: &str = "pg";
Expand Down
3 changes: 2 additions & 1 deletion butane_core/src/db/r2.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
pub use r2d2::ManageConnection;

use super::connmethods::ConnectionMethodWrapper;
use super::*;
use crate::Result;
pub use r2d2::ManageConnection;

/// R2D2 support for Butane. Implements [`r2d2::ManageConnection`].
#[derive(Clone, Debug)]
Expand Down
Loading