1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
//! # sqlx-models
//! sqlx-modes is a work in progress implementation for a sql migration management tool for applications using sqlx.
//! Beware, this is still under development, and some API's may be broken in the future.
//!
//!
//! # Basic Tutorial
//!
//! install the CLI by running the following command:
//! ```
//! $ cargo install sqlx-models-cli
//! ```
//!
//! Now run the following command to create an environment file with the `DATABASE_URL` variable set:
//! ```
//! $ echo "DATABASE_URL=sqlite://database.db" > .env
//! ```
//! We now can create the database running the command:
//! ```
//! $ sqlx database create
//! ```
//! This command will have created an sqlite file called `database.db`.
//! You can now derive the `Model` trait on your structures,
//! and `sqlx-models` will manage the migrations for you. For example, write at `src/main.rs`:
//! ```rust
//! #![allow(dead_code)]
//! use sqlx_models::Model;
//!
//! #[derive(Model)]
//! struct User {
//! #[primary_key]
//! id: i32,
//! #[unique]
//! email: String,
//! password: String,
//! #[default(0)]
//! is_admin: bool,
//! }
//!
//! #[derive(Model)]
//! struct Post {
//! #[primary_key]
//! id: i32,
//! #[foreign_key(User.id)]
//! author_: String,
//! #[default("<UNTITLED POST>")]
//! title: String,
//! content: String,
//! }
//!
//! #[derive(Model)]
//! struct PostLike {
//! #[foreign_key(User.id)]
//! #[primary_key(post_id)]
//! user_id: i32,
//! #[foreign_key(Post.id)]
//! post_id: i32,
//! }
//!
//! #[derive(Model)]
//! struct CommentLike {
//! #[foreign_key(User.id)]
//! #[primary_key(comment)]
//! user: i32,
//! #[foreign_key(Comment.id)]
//! comment: i32,
//! #[default(false)]
//! is_dislike: bool,
//! }
//!
//! #[derive(Model)]
//! struct Comment {
//! #[primary_key]
//! id: i32,
//! #[foreign_key(User.id)]
//! author: i32,
//! #[foreign_key(Post.id)]
//! post: i32,
//! }
//! fn main() {}
//! ```
//!
//! If you now run the following command, your migrations should be automatically created.
//! ```
//! $ sqlx migrate generate
//! ```
//! The output should look like this:
//! ```
//! Generated: migrations/1632280793452 user
//! Generated: migrations/1632280793459 post
//! Generated: migrations/1632280793465 postlike
//! Generated: migrations/1632280793471 comment
//! Generated: migrations/1632280793476 commentlike
//! ```
//! You can check out the generated migrations at the `migrations/` folder. To commit this migrations you can execute the following command:
//! ```
//! sqlx migrate run
//! ```
//! The output should look like this:
//! ```
//! Applied: migrations/1632280793452 user (569.208µs)
//! Applied: migrations/1632280793459 post (328.75µs)
//! Applied: migrations/1632280793465 postlike (436.542µs)
//! Applied: migrations/1632280793471 comment (288.625µs)
//! Applied: migrations/1632280793476 commentlike (318.208µs)
//! ```
//! If we later modify those structures in our application, we can generate new migrations to update the tables.
//!
//! ## Avaibale Attributes
//! ### primary_key
//! It's used to mark the primary key fo the table.
//! ```rust
//! #[primary_key]
//! id: i32,
//! ```
//! for tables with multicolumn primary keys, the following syntax is used:
//! ```rust
//! #[primary_key(second_id)]
//! first_id: i32,
//! second_id: i32,
//! ```
//! This is equivalent to
//! ```sql
//! PRIMARY KEY (first_id, second_id),
//! ```
//!
//! ### foreign_key
//! It is used to mark a foreign key constraint.
//! ```rust
//! #[foreign_key(User.id)]
//! user: i32,
//! ```
//! It can also specify `on_delete` and `on_update` constraints:
//! ```rust
//! #[foreign_key(User.id, on_delete="cascade"]
//! user_id: i32,
//! ```
//! This is equivalent to
//! ```sql
//! FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE,
//! ```
//! ### default
//! It can be used to set a default value for a column.
//! ```rust
//! #[default(false)] // if using sqlite use 0 or 1
//! is_admin: bool,
//! #[default("")]
//! text: String,
//! #[default(0)]
//! number: i32,
//! ```
//!
//! ### unique
//! It is used to mark a unique constraint.
//! ```rust
//! #[unique]
//! email: String,
//! ```
//! For multicolumn unique constraints the following syntax is used:
//! ```rust
//! #[unique(hash)]
//! user_id: String,
//! post_id: i32,
//! ```
//! This is equivalent to
//! ```sql
//! UNIQUE (user_id, post_id),
//! ```
#[macro_use]
mod error;
mod model;
mod prelude;
mod scheduler;
#[cfg(feature = "binary")]
pub use model::Binary;
// mod scheduler;
pub use sqlx_models_proc_macro::Model;
#[doc(hidden)]
pub mod private {
use once_cell::sync::Lazy;
pub use super::scheduler::Scheduler;
pub use super::scheduler::{
table::{constraint, Column},
Table,
};
pub static MIGRATIONS: Lazy<Scheduler> = Lazy::new(Scheduler::new);
/// Do not use the types defined in this module.
/// They are intended to be used only through the macro API.
/// Changes in this module are not considered to be breaking changes.
pub use super::model::{Dialect, IntoSQL, Model};
}