Skip to content

Basic Rust library to run SQL migrations in a directory against a PostgreSQL database

Notifications You must be signed in to change notification settings

reagent/rust-migrator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PostgreSQL Database Migrator

A simple database migration library that will read SQL files from a directory in order and apply them to a PostgreSQL database.

Usage

Make sure you have a database:

createdb migrations_development && \
  echo "DATABASE_URL=postgres:https://username@localhost/migrations_development" > .env

Point the migrator at your migrations directory and Postgres connection:

use std::path::Path;

use dotenv::dotenv;
use migrator::Migrator;
use postgres::{Client, NoTls};

fn main() {
    dotenv().ok();

    let migrations_path = Path::new(file!()).parent().unwrap();
    let migrations_path = migrations_path.join("db").join("migrations");

    let database_url = dotenv::var("DATABASE_URL").unwrap();

    let mut conn = Client::connect(&database_url, NoTls).unwrap(); // This can fail when db conn fails

    let migrator = Migrator::new(migrations_path);
    migrator.migrate(&mut conn);
}

About

Basic Rust library to run SQL migrations in a directory against a PostgreSQL database

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages