Skip to content

Feliperojas2601/Migrachu

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Migrachu

Migrachu

Migrachu is a migration tool for Postgres DB's.

Setup

To set up Migrachu, follow these steps:

  1. Clone the repository:
https://github.com/Feliperojas2601/Migrachu
  1. Navigate to the project directory:
cd migrachu
  1. Install the required dependencies:
npm install
  1. Optional, use docker to run a simply db
docker run --name postgres-db -e POSTGRES_USER=user -e POSTGRES_PASSWORD=pass -e POSTGRES_DB=mydb -p 5432:5432 -d postgres:latest
  1. Optional, poblate DB
CREATE TABLE users (
    id SERIAL PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL
);

CREATE TABLE orders (
    id SERIAL PRIMARY KEY,
    user_id INT REFERENCES users(id) ON DELETE CASCADE,
    total_amount DECIMAL(10,2) NOT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

INSERT INTO users (name, email) VALUES 
('Alice', 'alice@example.com'),
('Bob', 'bob@example.com');

INSERT INTO orders (user_id, total_amount) VALUES 
(1, 99.99),
(2, 49.50);
  1. Replace this vars in .env.dev, .env.stg and .env.prod files
DB_HOST=abc
DB_USER=abc
DB_PASSWORD=abc
DB_NAME=abc
DB_PORT=123
  1. Run the setup, he will use env.prod to create the schema models
npm run setup

Use

After set up, to use Migrachu to control migrations, follow these steps and rules:

Sync

  1. Modify the model needed in src/models.
  2. Add the migration sync to sync.ts in src/,
await Model.sync({ alter: true });
  1. Run sync:
npm run sync-${NODE_ENV}
  1. Note: Here we sync the model we want, comment the models you don't need to sync and uncomment the ones you modify

Other type of migrations

All migrations must implement the base migration abstract class, you can use some util functions to get excel, csv or json data into an object (be aware of the file size) and the migration should implement the up and down method with the specific migration logic.

  1. Run up:
npm run migrate-${NODE_ENV} myFirstMigration
  1. Run down:
npm run migrate-${NODE_ENV} myFirstMigration down

About

Migrachu is a migration tool for Postgres DB's.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published