tern_derive/
lib.rs

1use syn::{parse_macro_input, DeriveInput};
2
3mod internal;
4mod quote;
5
6/// A Rust migration requires a struct called `TernMigration` which derives
7/// `Migration`.  This is used in concert with [`MigrationSource`] to finish the
8/// implementation of [`Migration`] for it.
9///
10/// With the macro attribute `no_transaction`, the `Migration` implementation
11/// is constructed to not run the migration in a database transaction.
12///
13/// ## Usage
14///
15/// ```rust,ignore
16/// use tern::Migration;
17///
18/// /// Then implement `tern::QueryBuilder` for this type.
19/// #[derive(Migration)]
20/// #[tern(no_transaction)]
21/// pub struct TernMigration;
22/// ```
23///
24/// [`MigrationSource`]: crate::MigrationSource
25/// [`Migration`]: https://docs.rs/tern/latest/tern/trait.Migration.html
26#[proc_macro_derive(Migration, attributes(tern))]
27pub fn migration(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
28    let input = parse_macro_input!(input as DeriveInput);
29    quote::expand_impl_migration(&input)
30        .unwrap_or_else(syn::Error::into_compile_error)
31        .into()
32}
33
34/// `MigrationContext` implements the trait [`MigrationContext`], which is
35/// required of a type to be suitable for use in a migration [`Runner`].  A
36/// bound of [`MigrationSource`][source-core] exists, which can be satisfied by
37/// hand or by using the derive macro provided here, [`MigrationSource`].
38/// Custom, dynamic behavior for a migration can be defined for the context,
39/// which is available to [`QueryBuilder`].
40///
41/// The macro exposes one one optional field attribute:
42///
43/// * `executor_via` decorates the field holding an [`Executor`], which is
44///   required of the type to be a context.  If not specified then it is
45///   expected that the type itself implements `Executor`.
46///
47/// ## Usage
48///
49/// ```rust,ignore
50/// use tern::{SqlxPgExecutor, MigrationContext};
51///
52/// #[derive(MigrationContext)]
53/// pub struct MyContext {
54///     #[tern(executor_via)]
55///     executor: SqlxPgExecutor,
56/// }
57/// ```
58///
59/// [`Runner`]: https://docs.rs/tern/latest/tern/struct.Runner.html
60/// [`QueryBuilder`]: https://docs.rs/tern/latest/tern/trait.QueryBuilder.html
61/// [`MigrationContext`]: https://docs.rs/tern/latest/tern/trait.MigrationContext.html
62/// [source-core]: https://docs.rs/tern/latest/tern/trait.MigrationSource.html
63/// [`MigrationSource`]: crate::MigrationSource
64/// [`Executor`]: https://docs.rs/tern/latest/tern/trait.Executor.html
65#[proc_macro_derive(MigrationContext, attributes(tern))]
66pub fn migration_context(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
67    let input = parse_macro_input!(input as DeriveInput);
68    quote::expand_impl_migration_context(&input)
69        .unwrap_or_else(syn::Error::into_compile_error)
70        .into()
71}
72
73/// `MigrationSource` does the work of collecting all of the migrations, sorting
74/// them, unifying SQL and Rust migrations under a common interface by
75/// implementing [`Migration`], and then exposing methods to return an ordered
76/// subset of them to be used in a given operation.  It has one required
77/// attribute and one optional attribute.
78///
79/// * `source` is a required macro attribute.  It is the location of the
80///   migration files relative to the project root.
81/// * `table` is an optional macro attribute.  With it enabled, the migration
82///   history will be stored in this table, located in the default schema for
83///   the database driver, instead of the default table, `_tern_migrations`.
84///
85/// ## Usage
86///
87/// ```rust,ignore
88/// use tern::{SqlxPgExecutor, MigrationSource};
89///
90/// #[derive(MigrationSource)]
91/// #[tern(source = "src/migrations", table = "_my_migrations_history")]
92/// pub struct MyContext {
93///     #[tern(executor_via)]
94///     executor: SqlxPgExecutor,
95/// }
96/// ```
97///
98/// [`Migration`]: https://docs.rs/tern/latest/tern/trait.Migration.html
99#[proc_macro_derive(MigrationSource, attributes(tern))]
100pub fn migration_source(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
101    let input = parse_macro_input!(input as DeriveInput);
102    quote::expand_impl_migration_source(&input)
103        .unwrap_or_else(syn::Error::into_compile_error)
104        .into()
105}