sqlx-modes is a working progress implementation for a sql migration manangement tool for applications using sqlx.
install the CLI by running the following command:
cargo install sqlx-models-cli
now write in src/main.rs
:
```rust
use sqlx_models::Model;
struct User { #[primarykey] id: i32, #[unique] email: String, password: String, #[default = 0] isadmin: bool, }
struct PostLike { #[foreignkey(User.id)] #[primarykey(postid)] userid: i32, #[foreignkey(Post.id)] postid: i32, }
struct CommentLike { #[foreignkey(User.id)] #[primarykey(comment)] user: i32, #[foreignkey(Comment.id)] comment: i32, #[default = false] isdislike: bool, }
struct Post {
#[primarykey]
id: i32,
#[foreignkey(User.id)]
author_: String,
#[default = "
struct Comment { #[primarykey] id: i32, #[foreignkey(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
-- at
CREATE TABLE commentlike ( user INTEGER NOT NULL, COMMENT INTEGER NOT NULL, isdislike BOOLEAN NOT NULL DEFAULT false, CONSTRAINT commentlikeforeignuserid FOREIGN KEY (user) REFERENCES User(id), CONSTRAINT commentlikeprimaryusercomment PRIMARY KEY (user, COMMENT), CONSTRAINT commentlikeforeigncommentid FOREIGN KEY (COMMENT) REFERENCES COMMENT(id) );
-- at