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,no_run
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,no_run
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(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
72 let input = parse_macro_input!(input as DeriveInput);
73 quote::expand_impl_migration_context(&input)
74 .unwrap_or_else(syn::Error::into_compile_error)
75 .into()
76}
77
78/// `MigrationSource` does the work of collecting all of the migrations, sorting
79/// them, unifying SQL and Rust migrations under a common interface by
80/// implementing [`Migration`], and then exposing methods to return an ordered
81/// subset of them to be used in a given operation. It has one required
82/// attribute and one optional attribute.
83///
84/// * `source` is a required macro attribute. It is the location of the
85/// migration files relative to the project root (i.e., CARGO_MANIFEST_DIR).
86///
87/// ## Usage
88///
89/// ```rust,no_run
90/// use tern::{SqlxPgExecutor, MigrationSource};
91///
92/// #[derive(MigrationSource)]
93/// #[tern(source = "src/migrations")]
94/// pub struct MyContext {
95/// #[tern(executor_via)]
96/// executor: SqlxPgExecutor,
97/// }
98/// ```
99///
100/// [`Migration`]: https://docs.rs/tern/latest/tern/trait.Migration.html
101#[proc_macro_derive(MigrationSource, attributes(tern))]
102pub fn migration_source(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
103 let input = parse_macro_input!(input as DeriveInput);
104 quote::expand_impl_migration_source(&input)
105 .unwrap_or_else(syn::Error::into_compile_error)
106 .into()
107}