tern_derive/lib.rs
1use syn::{DeriveInput, parse_macro_input};
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 optional macro attribute and one optional field
42/// attribute:
43///
44/// * `table` is the optional macro attribute. With it enabled, the migration
45/// history will be stored in this table, located in the default schema for
46/// the database driver, instead of the default table, `_tern_migrations`.
47/// * `executor_via` decorates the field holding an [`Executor`], which is
48/// required of the type to be a context. If not specified then it is
49/// expected that the type itself implements `Executor`.
50///
51/// ## Usage
52///
53/// ```rust,ignore
54/// use tern::{SqlxPgExecutor, MigrationContext};
55///
56/// #[derive(MigrationContext)]
57/// #[tern(table = "_my_migration_history")]
58/// pub struct MyContext {
59/// #[tern(executor_via)]
60/// executor: SqlxPgExecutor,
61/// }
62/// ```
63///
64/// [`Runner`]: https://docs.rs/tern/latest/tern/struct.Runner.html
65/// [`QueryBuilder`]: https://docs.rs/tern/latest/tern/trait.QueryBuilder.html
66/// [`MigrationContext`]: https://docs.rs/tern/latest/tern/trait.MigrationContext.html
67/// [source-core]: https://docs.rs/tern/latest/tern/trait.MigrationSource.html
68/// [`MigrationSource`]: crate::MigrationSource
69/// [`Executor`]: https://docs.rs/tern/latest/tern/trait.Executor.html
70#[proc_macro_derive(MigrationContext, attributes(tern))]
71pub fn migration_context(
72 input: proc_macro::TokenStream,
73) -> proc_macro::TokenStream {
74 let input = parse_macro_input!(input as DeriveInput);
75 quote::expand_impl_migration_context(&input)
76 .unwrap_or_else(syn::Error::into_compile_error)
77 .into()
78}
79
80/// `MigrationSource` does the work of collecting all of the migrations, sorting
81/// them, unifying SQL and Rust migrations under a common interface by
82/// implementing [`Migration`], and then exposing methods to return an ordered
83/// subset of them to be used in a given operation. It has one required
84/// attribute and one optional attribute.
85///
86/// * `source` is a required macro attribute. It is the location of the
87/// migration files relative to the project root (i.e., CARGO_MANIFEST_DIR).
88///
89/// ## Usage
90///
91/// ```rust,ignore
92/// use tern::{SqlxPgExecutor, MigrationSource};
93///
94/// #[derive(MigrationSource)]
95/// #[tern(source = "src/migrations")]
96/// pub struct MyContext {
97/// #[tern(executor_via)]
98/// executor: SqlxPgExecutor,
99/// }
100/// ```
101///
102/// [`Migration`]: https://docs.rs/tern/latest/tern/trait.Migration.html
103#[proc_macro_derive(MigrationSource, attributes(tern))]
104pub fn migration_source(
105 input: proc_macro::TokenStream,
106) -> proc_macro::TokenStream {
107 let input = parse_macro_input!(input as DeriveInput);
108 quote::expand_impl_migration_source(&input)
109 .unwrap_or_else(syn::Error::into_compile_error)
110 .into()
111}