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
//! # SQLX-MODELS
//! sqlx-modes is a working progress implementation for a sql migration manangement tool for applications using sqlx.
//! # Basic Tutorial
//! install the CLI by running the following command: 
//! ```
//! cargo install sqlx-models-cli
//! ```
//!
//! now write in `src/main.rs`: 
//! ```rust
//! 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 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 Post {
//!     #[primary_key]
//!     id: i32,
//!     #[foreign_key(User.id)]
//!     author_: String,
//!     #[default = "<UNTITLED POST>"]
//!     title: String,
//!     content: String,
//! }
//!
//! #[derive(Model)]
//! struct Comment {
//!     #[primary_key]
//!     id: i32,
//!     #[foreign_key(User.id)]
//!     author: i32,
//!     #[foreign_key(Post.id)]
//!     post: i32,
//! }
//! ```
//!
//! If you now run the following command, your migrations should be automatically created (make sure your code compiles).
//! ``` 
//! sqlx generate
//! ```
//! the output generated should look something like this 
//! ```sql
//! -- at <TIMESTAMP>_user.sql. 
//! CREATE TABLE user (
//!     id INTEGER NOT NULL,
//!     email TEXT NOT NULL,
//!     PASSWORD TEXT NOT NULL,
//!     is_admin BOOLEAN NOT NULL DEFAULT 0,
//!     CONSTRAINT user_primary_id PRIMARY KEY (id),
//!     CONSTRAINT user_unique_email UNIQUE (email)
//! );
//! -- at <TIMESTAMP>_post.sql. 
//! CREATE TABLE post (
//!     id INTEGER NOT NULL,
//!     author_ TEXT NOT NULL,
//!     title TEXT NOT NULL DEFAULT '<UNTITLED POST>',
//!     content TEXT NOT NULL,
//!     CONSTRAINT post_primary_id PRIMARY KEY (id),
//!     CONSTRAINT post_foreign_author__id FOREIGN KEY (author_) REFERENCES User(id)
//! );
//!
//! -- at <TIMESTAMP>_comment.sql. 
//! CREATE TABLE COMMENT (
//!     id INTEGER NOT NULL,
//!     author INTEGER NOT NULL,
//!     post INTEGER NOT NULL,
//!     CONSTRAINT comment_primary_id PRIMARY KEY (id),
//!     CONSTRAINT comment_foreign_author_id FOREIGN KEY (author) REFERENCES User(id),
//!     CONSTRAINT comment_foreign_post_id FOREIGN KEY (post) REFERENCES Post(id)
//! );
//! -- at <TIMESTAMP>_commentlike.sql. 
//!
//! CREATE TABLE commentlike (
//!     user INTEGER NOT NULL,
//!     COMMENT INTEGER NOT NULL,
//!     is_dislike BOOLEAN NOT NULL DEFAULT false,
//!     CONSTRAINT commentlike_foreign_user_id FOREIGN KEY (user) REFERENCES User(id),
//!     CONSTRAINT commentlike_primary_user_comment PRIMARY KEY (user, COMMENT),
//!     CONSTRAINT commentlike_foreign_comment_id FOREIGN KEY (COMMENT) REFERENCES COMMENT(id)
//! );
//!
//! -- at <TIMESTAMP>_postlike.sql. 
//! CREATE TABLE postlike (
//!     user_id INTEGER NOT NULL,
//!     post_id INTEGER NOT NULL,
//!     CONSTRAINT postlike_foreign_user_id_id FOREIGN KEY (user_id) REFERENCES User(id),
//!     CONSTRAINT postlike_primary_user_id_post_id PRIMARY KEY (user_id, post_id),
//!     CONSTRAINT postlike_foreign_post_id_id FOREIGN KEY (post_id) REFERENCES Post(id)
//! );
//! ```
//! If we later modify those structures in our application, we can generate new migrations to update the tables. 




mod migration;
mod model;
mod prelude;
mod sorter;
mod error;
pub use sqlx_models_proc_macro::Model;


#[doc(hidden)]
pub mod private {
    use once_cell::sync::Lazy;

    pub use super::migration::{
        table::{constraint, Column},
        Migration, Table,
    };
    pub use super::sorter::Sorter;
    pub static MIGRATIONS: Lazy<Sorter> = Lazy::new(Sorter::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, Model, SqlType};
}