• Stars
    star
    129
  • Rank 279,262 (Top 6 %)
  • Language
    Rust
  • License
    Apache License 2.0
  • Created about 9 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

PostgreSQL/SQLite/MySQL migration tool in rust

dbmigrate

Build Status

A tool to create and manage SQL migrations.

Databases supported

  • Postgres
  • MySQL
  • Sqlite

Usage

Using CLI

Every call to dbmigrate requires 2 arguments: database url and migrations folder. Those can be set through environment variables: DBMIGRATE_URL and DBMIGRATE_PATH, using a .env file, or as args to a call. Argument will override an environment variable.

# create a migration file
dbmigrate --url postgres://.. --path ./migrations create my_name
# apply all non applied migrations
dbmigrate --url postgres://.. --path ./migrations up
# un-apply all migrations
dbmigrate --url postgres://.. --path ./migrations down
# redo the last migration
dbmigrate --url postgres://.. --path ./migrations redo
# revert the last migration
dbmigrate --url postgres://.. --path ./migrations revert
# see list of migrations and which one is currently applied
dbmigrate --url postgres://.. --path ./migrations status

The format of the migration files is the following:

0001.initial_db.up.sql
0001.initial_db.down.sql

You can also pass a string to create and dbmigrate will slugify it for you:

dbmigrate --url postgres://.. --path ./migrations create "change currency table"

# gives the following files
0001.change_currency_table.up.sql
0001.change_currency_table.down.sql

. (dot) is not allowed in a migration name as it is the filename separator character.

Using the library

Migrations can also be done programmatically and is how the CLI tool is built.

You will need to add the dbmigrate-lib dependency to your Cargo.toml file. The best example to see how how to make it work is to look at the dbmigrate directory, which uses it to implement the CLI tool.

Test locally

Build the project first with cargo build. Assuming you use the docker images in the Makefile for pg and mysql:

Postgres:

./target/release/dbmigrate --url=postgres://pg@localhost:5777/migrate --path=/my/full/path/migrations status

MySQL:

./target/release/dbmigrate --url=mysql://mg:pass@localhost:5789/migrate --path=/my/full/path/migrations status

For Sqlite I have a Sqlite db named dbmigrate.db in the repo (gitignored):

./target/release/dbmigrate --path=/home/vincent/Code/dbmigrate/examples/migrations --url=sqlite:///dbmigrate.db status

Using a different schema

dbmigrate will use the default schema. You can override that from your database URL, for example for Postgres:

--url="postgres://postgres:@127.0.0.1:5432/migrate?application_name=my_app&options=-c search_path%3Dmy_app"

Changelog

Lib

  • 0.1.5: update postgres dependencies
  • 0.1.4: Use postgres-native-tls
  • 0.1.1: Add features so you can only pick the database you need

CLI

  • 0.3.4: Don't crash if the terminal doesn't support colours
  • 0.3.3: Don't crash if there is no terminal
  • 0.3.2: Use features
  • 0.3.1: Refactor code to create a dbmigrate-lib and use it for the dbmigrate tool
  • 0.3.0: Add dotenv support + refactor error handling + update dependencies
  • 0.2.7: Update docs to mention sqlite support
  • 0.2.6: Support Sqlite
  • 0.2.5: Update dependencies
  • 0.2.4: Do not require DB connection to create migration file and update dependencies
  • 0.2.3: don't panic on invalid files in migration folder & ssl support for postgres
  • 0.2.2: slugify migration names and check if they are ok

Acknowledgments

This is heavily inspired by https://github.com/mattes/migrate.

License

Licensed under either of

at your option.

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

More Repositories

1

tera

A template engine for Rust based on Jinja2/Django
Rust
3,512
star
2

validator

Simple validation for Rust structs
Rust
2,024
star
3

jsonwebtoken

JWT lib in rust
Rust
1,681
star
4

kickstart

A scaffolding tool to get new projects up and running quickly
Rust
363
star
5

rust-bcrypt

Easily hash and verify passwords using Bcrypt
Rust
335
star
6

rodent

Turn your laptop into a CCTV!
Python
71
star
7

scl

A simple configuration language
Rust
63
star
8

ng-boilerplate

AngularJS boilerplate app
CSS
60
star
9

webrust

Rust
51
star
10

rust-cli-template

A template to get started with writing cross-platforms CLI applications
Shell
51
star
11

tera2

Rust
39
star
12

react-ts-boilerplate

A React + Typescript + Sass boilerplate
TypeScript
33
star
13

django-drf-template

A django template to start a DRF project
Python
28
star
14

graphql-parser

Rust
20
star
15

vincentprouillet

My website
HTML
15
star
16

flow-typescript

JavaScript
9
star
17

invoicer

TypeScript
8
star
18

vat

VAT number validation and VAT rates for the EU
Rust
7
star
19

playbook-mailinabox

Shell
6
star
20

gulp-example

Example setup for blogpost
CoffeeScript
6
star
21

hn-react

TypeScript
5
star
22

pelican-hyde

Port of https://github.com/poole/hyde to Pelican with slight changes
CSS
4
star
23

react-example

Basic example using react, redux and react-router.
JavaScript
3
star
24

Artemis

Entity-Components-Systems framework in coffeescript
JavaScript
3
star
25

react-redux-boilerplate

JavaScript
2
star
26

papyrus

Start of a contenteditable editor (not worked on)
JavaScript
1
star
27

kcalculator-react

TypeScript
1
star
28

markdown-benches-rs

Rust
1
star
29

dotfiles

Shell
1
star
30

serde-tera

Experiment
Rust
1
star
31

Punchstone

Game engine written in coffeescript
JavaScript
1
star
32

hackertyper

Python
1
star
33

restacular-old

REST Server
Go
1
star
34

webpack-react-typescript

JavaScript
1
star