Skip to content

Commit

Permalink
Implement embed_migration! using macros 1.1
Browse files Browse the repository at this point in the history
* port embed_migration to macros 1.1
* remove codegen_old
* some minor fixes
  • Loading branch information
weiznich committed Oct 9, 2016
1 parent ce31e82 commit b647781
Show file tree
Hide file tree
Showing 24 changed files with 75 additions and 353 deletions.
29 changes: 29 additions & 0 deletions diesel/src/macros/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,35 @@ macro_rules! infer_table_from_schema {
}
}

#[macro_export]
/// This macro will read your migrations at compile time, and embed a module you can
/// use to execute them at runtime without the migration files being present on the
/// file system. This is useful if you would like to use Diesel's migration
/// infrastructure, but want to ship a single executable file (such as for embedded
/// applications). It can also be used to apply migrations to an in memory database
/// (Diesel does this for its own test suite).
///
/// You can optionally pass the path to the migrations directory to this macro. When
/// left unspecified, Diesel Codegen will search for the migrations directory in the
/// same way that Diesel CLI does. If specified, the path should be relative to the
/// directory containing your Cargo.toml (CARGO_MANIFEST_HOME)
///
/// This macro can only be used in combination with the `diesel_codegen` or
/// `diesel_codegen_syntex` crates. It will not work on its own.
///
/// FIXME: Oh look we have a place to actually document this now.
macro_rules! embed_migrations {
() => {
#[derive(Embed_Migrations)]
struct Migrations;
};
($migration_path: expr) => {
#[derive(EmbeddedMigrations)]
#[options(migration_path=$migration_path)]
struct Migrations;
}
}

// The order of these modules is important (at least for those which have tests).
// Utililty macros which don't call any others need to come first.
#[macro_use] mod parse;
Expand Down
2 changes: 2 additions & 0 deletions diesel/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ pub mod connection;
mod migration_error;
#[doc(hidden)]
pub mod schema;
mod embedded_migration;
pub use self::embedded_migration::EmbeddedMigration;

#[doc(inline)]
pub use self::connection::MigrationConnection;
Expand Down
2 changes: 2 additions & 0 deletions diesel_codegen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ authors = ["Sean Griffin <sean@seantheprogrammer.com>"]
syn = "0.8.5"
quote = "0.2.1"
diesel_codegen_shared = { path = "../diesel_codegen_shared", default-features = false }
diesel = {version = "0.7", default-features = false }


[lib]
rustc-macro = true
Expand Down
4 changes: 2 additions & 2 deletions diesel_codegen/src/associations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,6 @@ fn to_foreign_key(model_name: &str) -> syn::Ident {

#[test]
fn to_foreign_key_properly_handles_underscores() {
assert_eq!(str_to_ident("foo_bar_id"), to_foreign_key("FooBar"));
assert_eq!(str_to_ident("foo_bar_baz_id"), to_foreign_key("FooBarBaz"));
assert_eq!(syn::Ident::new("foo_bar_id"), to_foreign_key("FooBar"));
assert_eq!(syn::Ident::new("foo_bar_baz_id"), to_foreign_key("FooBarBaz"));
}
24 changes: 16 additions & 8 deletions diesel_codegen/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#![feature(rustc_macro, rustc_macro_lib)]
#![deny(warnings)]
#![recursion_limit = "512"]

macro_rules! t {
($expr:expr) => {
Expand All @@ -10,7 +11,6 @@ macro_rules! t {
};
}

#[cfg(any(feature = "postgres", feature = "sqlite"))]
extern crate diesel_codegen_shared;
#[macro_use]
extern crate quote;
Expand All @@ -28,6 +28,7 @@ mod queryable;
#[cfg(any(feature = "postgres", feature = "sqlite"))]
mod schema_inference;
mod util;
mod migrations;

use rustc_macro::TokenStream;
use syn::parse_macro_input;
Expand All @@ -53,47 +54,54 @@ const KNOWN_FIELD_ATTRIBUTES: &'static [&'static str] = &[
"column_name",
];

#[rustc_macro_derive(Queryable)]
#[cfg_attr(not(test), rustc_macro_derive(Queryable))]
pub fn derive_queryable(input: TokenStream) -> TokenStream {
expand_derive(input, queryable::derive_queryable)
}

#[rustc_macro_derive(Identifiable)]
#[cfg_attr(not(test), rustc_macro_derive(Identifiable))]
pub fn derive_identifiable(input: TokenStream) -> TokenStream {
expand_derive(input, identifiable::derive_identifiable)
}

#[rustc_macro_derive(Insertable)]
#[cfg_attr(not(test), rustc_macro_derive(Insertable))]
pub fn derive_insertable(input: TokenStream) -> TokenStream {
expand_derive(input, insertable::derive_insertable)
}

#[rustc_macro_derive(AsChangeset)]
#[cfg_attr(not(test), rustc_macro_derive(AsChangeset))]
pub fn derive_as_changeset(input: TokenStream) -> TokenStream {
expand_derive(input, as_changeset::derive_as_changeset)
}

#[rustc_macro_derive(Associations)]
#[cfg_attr(not(test), rustc_macro_derive(Associations))]
pub fn derive_associations(input: TokenStream) -> TokenStream {
expand_derive(input, associations::derive_associations)
}

#[rustc_macro_derive(InferSchema)]
#[cfg_attr(not(test), rustc_macro_derive(InferSchema))]
#[cfg(any(feature = "sqlite", feature = "postgres"))]
pub fn derive_infer_schema(input: TokenStream) -> TokenStream {
let item = parse_macro_input(&input.to_string()).unwrap();
schema_inference::derive_infer_schema(item)
.to_string().parse().unwrap()
}

#[rustc_macro_derive(InferTableFromSchema)]
#[cfg_attr(not(test), rustc_macro_derive(InferTableFromSchema))]
#[cfg(any(feature = "sqlite", feature = "postgres"))]
pub fn derive_infer_table_from_schema(input: TokenStream) -> TokenStream {
let item = parse_macro_input(&input.to_string()).unwrap();
schema_inference::derive_infer_table_from_schema(item)
.to_string().parse().unwrap()
}

#[cfg_attr(not(test), rustc_macro_derive(EmbeddedMigrations))]
pub fn derive_embedded_migrations(input: TokenStream) -> TokenStream {
let item = parse_macro_input(&input.to_string()).unwrap();
migrations::derive_embedded_migrations(item)
.to_string().parse().unwrap()
}

fn expand_derive(input: TokenStream, f: fn(syn::MacroInput) -> quote::Tokens) -> TokenStream {
let mut item = parse_macro_input(&input.to_string()).unwrap();
let output = f(item.clone());
Expand Down
22 changes: 0 additions & 22 deletions diesel_codegen_old/Cargo.toml

This file was deleted.

182 changes: 0 additions & 182 deletions diesel_codegen_old/README.md

This file was deleted.

12 changes: 0 additions & 12 deletions diesel_codegen_old/src/lib.rs

This file was deleted.

5 changes: 5 additions & 0 deletions diesel_codegen_shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ repository = "https://github.com/diesel-rs/diesel"
diesel = { version = "0.7.0", default-features = false }
dotenv = { version = "0.8.0", optional = true }

[dev-dependencies]
tempdir = "0.3.4"


[features]
default = ["dotenv"]
postgres = ["diesel/postgres"]
sqlite = ["diesel/sqlite"]

1 change: 1 addition & 0 deletions diesel_codegen_shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use diesel::sqlite::SqliteConnection;
mod database_url;
#[cfg(any(feature = "sqlite", feature = "postgres"))]
mod schema_inference;
pub mod migrations;

pub use self::database_url::extract_database_url;
#[cfg(any(feature = "sqlite", feature = "postgres"))]
Expand Down
Loading

0 comments on commit b647781

Please sign in to comment.