1.0.12 • Published 4 years ago

pg-migration-manager v1.0.12

Weekly downloads
23
License
Apache License 2....
Repository
github
Last release
4 years ago

Image alt

The super simple migration manager for loved by everyone pg driver.

Installation

npm i pg-migration-manager

Importing

import MigrationManager from 'pg-migration-manager';

Usage

import * as http from 'http';
import { Pool } from 'pg';
import MigrationManager from 'pg-migration-manager';


class Server { 
    server;
    database;    

    initServer(){
        this.server = http.createServer((req, res)=>{/** request handling **/})
    } 

    async initDatabase(){
        this.database = new Pool(/** connection config **/);

        const manager = new MigrationManager(this.database, {
               pathToMigrations: 'src/migrations', // required
               migrationTableName: 'migration', // optional
           });
        
        await manager.runMigrations();
    }
    
    async start(){
        this.initServer();
        await this.initDatabase();
        this.server.listen(3000, 'hostname');
    }   
}

new Server().start().then(()=>'server has started')

The most reasonable way to implement migrations is to run sql scripts before server has started.

First you create instance of MigrationManager class passing to it database client and configuration object.

The last one contains:

  • pathToMigrations - a path to a folder where your sql scripts store.
  • migrationTableName - a name of a table that will be created for writing fulfilled migrations.

Then you execute runMigrations method that's returning promise.

During the runMigrations method runtime there will create migration table in database (if it does not exist yet), then your migrations will be worked out and written in this table.

Next time when you start server saved migrations will be ignored, only new ones will be run.

Note: thus don't change migration file names.

Another note: the migration manager implements migrations one by one according to creation dates of files.

In conclusion

I'll be happy to hear your ideas and suggestions to possible improving this tiny but helpful tool.

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.2

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago