Code Monkey home page Code Monkey logo

dbmigrations's Introduction

Database migration tool

Generic db migration tool to generate the Upgrade sql statements or downgrade sql statements based on the given timestamp. This tool is not specific to any Database or doesn't maintain the version information.

It is inspired by https://github.com/amberframework/micrate, but this project only focused on generating single SQL file for upgrade or downgrade. This doesn't modify or execute the SQL code.

Install

Clone this repo and run dub build. Copy the generated binary to /usr/local/bin.

git clone https://github.com/aravindavk/dbmigrations.git
cd dbmigrations
dub build --compiler=ldc2 -b release
sudo cp dbmigrations /usr/local/bin/

Usage

Create the migrations directory in your project directory. For example,

cd myproject
mkdir -p db/migrations

Scaffold

Now create the migration file by running the below command.

$ dbmigrations -d ./db/migrations scaffold -n users
Created ./db/migrations/20240524213223-users.sql

Open the file and add the SQL for upgrade under -- +UP and downgrade SQL under -- +DOWN section. Example,

-- +UP
-- SQL in section '+UP' is included when this migration is applied
CREATE TABLE users(
  id                         BIGSERIAL PRIMARY KEY,
  name                       VARCHAR NOT NULL,
  email                      VARCHAR NOT NULL,
  password_hash              VARCHAR NOT NULL,
  created_at                 TIMESTAMP,
  updated_at                 TIMESTAMP
);

-- +DOWN
-- SQL section '+DOWN' is included when this migration is rolled back
DROP TABLE users;

Upgrade

Run dbmigrations up to generate the migration file based on given timestamp.

$ dbmigrations -d ./db/migrations up
Included files:
20240524135055-users.sql

Migration file generated. Path: /tmp/dbmigration.sql
Execute the SQL file to run migration (upgrade/downgrade). For example, Postgres

  sudo -u postgres psql mydb -f /tmp/dbmigration.sql

To generate the migration file based on the given timestamp.

$ dbmigrations -d ./db/migrations up -t 20240524135847

Status

$ dbmigrations -d ./db/migrations status
   pending  20240524135055-users.sql
$ dbmigrations -d ./db/migrations status -t 20240524135055
      done  20240524135055-users.sql

Downgrade(one by one)

Generates the migration file based on the given timestamp. Rollback only the last migration after the given timestamp.

$ dbmigrations -d ./db/migrations down -t 20240524135055
Included files:
20240524135055-users.sql

Migration file generated. Path: /tmp/dbmigration.sql
Execute the SQL file to run migration (upgrade/downgrade). For example, Postgres

  sudo -u postgres psql mydb -f /tmp/dbmigration.sql

Contributing

  • Fork it (https://github.com/aravindavk/dbmigrations/fork)
  • Create your feature branch (git checkout -b my-new-feature)
  • Commit your changes (git commit -asm 'Add some feature')
  • Push to the branch (git push origin my-new-feature)
  • Create a new Pull Request

Contributors

  • Aravinda VK - Creator and Maintainer

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.