Models is an implementation for a sql migration management tool

install the CLI by running the following command:

Models

.

Basic Tutorial

$ cargo install 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

Alternatively it can be set as a environment variable with the following command:

$ export DATABASE_URL=sqlite://database.db

We now can create the database running the following command:

$ models database create

This command will have created an sqlite file called database.db. You can now derive the Model trait on your structures, and models will manage the migrations for you. For example, write at src/main.rs:

#![allow(dead_code)]
use 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, on_delete="cascade")]
    #[primary_key(post_id)]
    user_id: i32,
    #[foreign_key(Post.id, on_delete="cascade")]
    post_id: i32,
}

#[derive(Model)]
struct CommentLike {
    #[foreign_key(User.id)]
    #[primary_key(comment_id)]
    user_id: i32,
    #[foreign_key(Comment.id)]
    comment_id: i32,
    #[default(0)]
    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.

$ models 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 execute these migrations you can execute the following command:

models migrate run

The output should look like this:

Applied 1631716729974/migrate user (342.208&micro;s)
Applied 1631716729980/migrate post (255.958&micro;s)
Applied 1631716729986/migrate comment (287.792&micro;s)
Applied 1631716729993/migrate postlike (349.834&micro;s)
Applied 1631716729998/migrate commentlike (374.625&micro;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.

    #[primary_key]
    id: i32, 

for tables with multicolumn primary keys, the following syntax is used:

    #[primary_key(second_id)]
    first_id: i32, 
    second_id: i32, 

This is equivalent to

    PRIMARY KEY (first_id, second_id),

foreign_key

It is used to mark a foreign key constraint.

    #[foreign_key(User.id)]
    user: i32, 

It can also specify on_delete and on_update constraints:

    #[foreign_key(User.id, on_delete="cascade")]
    user_id: i32, 

This is equivalent to

    FOREIGN KEY (user_id) REFERENCES user (id) ON DELETE CASCADE,

default

It can be used to set a default value for a column.

    #[default(false)] // when 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.

    #[unique]
    email: String, 

For multicolumn unique constraints the following syntax is used:

    #[unique(post_id)]
    user_id: String,
    post_id: i32,

This is equivalent to

    UNIQUE (user_id, post_id),
Issues

Collection of the latest Issues

mainrs

mainrs

2

I already had a code base with handwritten (up and down) migrations. I changed my Rust struct and annotated it properly. For some reason I get the following error:

I tried to reproduce it inside a clean environment but that failed. In a new, clean project everything works fine. Sadly I can't link you the code.

Information - Updated May 27, 2022

Stars: 40
Forks: 1
Issues: 7

Repositories & Extras

influxdb provides an asynchronous Rust interface to an InfluxDB database

influxdb provides an asynchronous Rust interface to an Integer 32, sponsored by Stephan Buys of

influxdb provides an asynchronous Rust interface to an InfluxDB database

Rust DataBase Connectivity (RDBC)

Love them or hate them, the JDBC standards have made it easy to use a wide range of desktop and server products with many different...

Rust DataBase Connectivity (RDBC)

Rdb is a relational database implemented in Rust

Unlike databases like PostgreSQL and SQLite, Rdb does not operate on a client-server model

Rdb is a relational database implemented in Rust

Simple Database Implementation written in Rust

This repository uses cargo 1

Simple Database Implementation written in Rust

A graph database written in rust

IndraDB's original design is heavily inspired by homepage

A graph database written in rust

Jin is a small relational database engine written in Rust with the standard library and...

Jin is a small relational database engine written in here or run:

Jin is a small relational database engine written in Rust with the standard library and...

Shappy key-value database created by Rust

Copyright (©) 2021 Sh-Zh-7

Shappy key-value database created by Rust
Facebook Instagram Twitter GitHub Dribbble
Privacy