{"id":13822362,"url":"https://github.com/tvallotton/models","last_synced_at":"2025-12-12T14:12:03.169Z","repository":{"id":46717246,"uuid":"397707023","full_name":"tvallotton/models","owner":"tvallotton","description":"A tool for automated migrations for PostgreSQL, SQLite and MySQL. ","archived":false,"fork":false,"pushed_at":"2022-01-25T22:55:54.000Z","size":1362,"stargazers_count":50,"open_issues_count":10,"forks_count":3,"subscribers_count":3,"default_branch":"main","last_synced_at":"2024-05-21T06:50:57.674Z","etag":null,"topics":["database","database-migrations","migrations","rust","sql","sqlite"],"latest_commit_sha":null,"homepage":"","language":"Rust","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tvallotton.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2021-08-18T19:00:14.000Z","updated_at":"2024-03-10T14:30:29.000Z","dependencies_parsed_at":"2022-08-28T19:31:37.879Z","dependency_job_id":null,"html_url":"https://github.com/tvallotton/models","commit_stats":null,"previous_names":["tvallotton/sqlx-models"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvallotton%2Fmodels","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvallotton%2Fmodels/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvallotton%2Fmodels/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tvallotton%2Fmodels/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tvallotton","download_url":"https://codeload.github.com/tvallotton/models/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":242650895,"owners_count":20163610,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["database","database-migrations","migrations","rust","sql","sqlite"],"created_at":"2024-08-04T08:01:56.805Z","updated_at":"2025-12-12T14:12:03.101Z","avatar_url":"https://github.com/tvallotton.png","language":"Rust","funding_links":[],"categories":["Rust"],"sub_categories":[],"readme":"# Models\nModels is a SQL migration management tool. It supports PostgreSQL, MySQL, and SQLite.\n\n\n# Quick Start\n\ninstall the CLI by running the following command: \n```\n$ cargo install models-cli\n```\n\nNow run the following command to create an environment file with the `DATABASE_URL` variable set: \n```\n$ echo \"DATABASE_URL=sqlite://database.db\" \u003e .env\n```\nAlternatively it can be set as a environment variable with the following command: \n```\n$ export DATABASE_URL=sqlite://database.db\n```\nWe now can create the database running the following command: \n```\n$ models database create\n```\nThis command will have created an SQLite file called `database.db`. \nYou can now derive the `Model` trait on your structures, \nand `models` will manage the migrations for you. For example, write at `src/main.rs`: \n```rust\n#![allow(dead_code)]\nuse models::Model; \n\n#[derive(Model)]\nstruct Profile {\n    #[primary_key]\n    id: i32,\n    #[unique]\n    email: String,\n    password: String,\n    is_admin: bool,\n}\n\n#[derive(Model)]\nstruct Post {\n    #[primary_key]\n    id: i32,\n    #[foreign_key(Profile.id)]\n    author: i32,\n    #[default(\"\u003cUntitled Post\u003e\")]\n    title: String,\n    content: String,\n}\n\n#[derive(Model)]\nstruct PostLike {\n    #[foreign_key(Profile.id, on_delete=\"cascade\")]\n    #[primary_key(post_id)]\n    profile_id: i32,\n    #[foreign_key(Post.id, on_delete=\"cascade\")]\n    post_id: i32,\n}\n\n#[derive(Model)]\nstruct CommentLike {\n    #[foreign_key(Profile.id)]\n    #[primary_key(comment_id)]\n    profile_id: i32,\n    #[foreign_key(Comment.id)]\n    comment_id: i32,\n    is_dislike: bool,\n}\n\n#[derive(Model)]\nstruct Comment {\n    #[primary_key]\n    id: i32,\n    #[foreign_key(Profile.id)]\n    author: i32,\n    #[foreign_key(Post.id)]\n    post: i32,\n}\nfn main() {}\n```\n\nIf you now run the following command, your migrations should be automatically created.\n``` \n$ models generate\n```\nThe output should look like this: \n```\nGenerated: migrations/1632280793452 profile\nGenerated: migrations/1632280793459 post\nGenerated: migrations/1632280793465 postlike\nGenerated: migrations/1632280793471 comment\nGenerated: migrations/1632280793476 commentlike\n```\nYou can check out the generated migrations at the `migrations/` folder. \nTo execute these migrations you can execute the following command: \n```\nmodels migrate run\n```\nThe output should look like this: \n```\nApplied 1631716729974/migrate profile (342.208µs)\nApplied 1631716729980/migrate post (255.958µs)\nApplied 1631716729986/migrate comment (287.792µs)\nApplied 1631716729993/migrate postlike (349.834µs)\nApplied 1631716729998/migrate commentlike (374.625µs)\n```\nIf we later modify those structures in our application, we can generate new migrations to update the tables. \n\n## Reverting migration\nModels can generate down migrations with the `-r` flag. Note that simple and reversible migrations cannot be mixed: \n```\n$ models generate -r\n```\nIn order to revert the last migration executed you can run: \n```\n$ models migrate revert\n```\nIf you later want to see which migrations are yet to be applied you can also excecute: \n```\n$ models migrate info\n```\nApplied migrations need to be reverted before they can be deleted. \n## Avaibale Attributes\n### primary_key\nIt's used to mark the primary key fo the table. \n```rust\n    #[primary_key]\n    id: i32, \n```\nfor tables with multicolumn primary keys, the following syntax is used: \n```rust\n    #[primary_key(second_id)]\n    first_id: i32, \n    second_id: i32, \n```\nThis is equivalent to:\n```sql\n    PRIMARY KEY (first_id, second_id),\n```\n\n### foreign_key\nIt is used to mark a foreign key constraint. \n```rust\n    #[foreign_key(Profile.id)]\n    profile: i32, \n```\nIt can also specify `on_delete` and `on_update` constraints: \n```rust\n    #[foreign_key(Profile.id, on_delete=\"cascade\")]\n    profile_id: i32, \n```\nThis is equivalent to:\n```sql\n    FOREIGN KEY (profile_id) REFERENCES profile (id) ON DELETE CASCADE,\n```\n### default\nIt can be used to set a default value for a column. \n```rust\n    #[default(false)] // when using SQLite use 0 or 1\n    is_admin: bool, \n    #[default(\"\")]\n    text: String, \n    #[default(0)]\n    number: i32, \n```\n\n### unique\nIt is used to mark a unique constraint. \n```rust\n    #[unique]\n    email: String, \n```\nFor multicolumn unique constraints the following syntax is used: \n```rust\n    #[unique(post_id)]\n    profile_id: String,\n    post_id: i32,\n```\nThis is equivalent to:\n```sql\n    UNIQUE (profile_id, post_id),\n```\n## CLI Short cuts\nThe CLI includes the following shortcuts: \n* `models database` -\u003e `models db`\n* `models generate` -\u003e `models gen`\n* `models migrate` -\u003e `models mig`\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftvallotton%2Fmodels","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftvallotton%2Fmodels","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftvallotton%2Fmodels/lists"}