🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

larvitdbmigration-pg

Package Overview
Dependencies
Maintainers
4
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

larvitdbmigration-pg

Database migration tool

latest
Source
npmnpm
Version
0.2.3
Version published
Weekly downloads
39
333.33%
Maintainers
4
Weekly downloads
 
Created
Source

image:https://travis-ci.org/larvit/larvitdbmigration-pg.svg?branch=master[] image:https://coveralls.io/repos/github/larvit/larvitdbmigration-pg/badge.svg?branch=master[]

= Database migration tool

This is used to keep track of database structure, content etc, and update it when need be via deploys.

A table by default called db_versions will be created, containing a single integer.

Scripts will be placed by default in process.cwd()/dbmigration/.js

Each migration script will be ran, and the db_version increased, until no more migration scripts exists.

== Installation

npm i larvitdbmigration-pg

== Usage

=== Application startup script

In your application startup script, do something like this:

[source,javascript]

const DbMigration = require('larvitdbmigration-pg'); const Db = require('larvitdb-pg'); const dbDriver = new Db({ user: 'foo', password: 'bar', }); const dbMigration = new DbMigration({ dbDriver, tableName: 'db_version', // Optional - defaults to 'db_version' migrationScriptPath: './dbmigration', // Optional, defaults to './dbmigration' log // Optional, will use log.silly(), log.debug(), log.verbose(), log.info(), log.warn() and log.error() if given. });

dbMigration.run().then(() => { console.log('Database migrated to latest version'); }).catch(err => { throw err; });

=== Example migration scripts

Lets say the current database have a table like this:

[source,SQL]

CREATE TABLE bloj (nisse serial);

And in the next deploy we'd like to change the column name "nisse" to "hasse". For this you can do one of two methods:

==== Javascript

Create the file process.cwd()/migrationScriptPath/1.js with this content:

.migrationScriptPath/1.js [source,javascript]

'use strict';

// Always make the function async (or return a promise, they are equal) exports = module.exports = async function (options) { const { db } = options;

await db.query('ALTER TABLE bloj RENAME COLUMN nisse TO hasse;');

};

==== SQL

IMPORTANT! SQL files will be ignored if a .js file exists.

Create the file process.cwd()/migrationScriptPath/1.sql with this content:

.migrationScriptPath/1.sql [source,SQL]

ALTER TABLE bloj RENAME COLUMN nisse TO hasse;

==== Summary

Tadaaa! Now this gets done once and the version will be bumped to 1. If you then create a script named "2.js" or "2.sql" you might guess what happends. :)

== Version history

=== v0.2.0

  • Updated larvitdb-pg and the requirements of the database layer

=== v0.1.3

  • Fixed bug with multiple concurrent migrations hangs

Keywords

db

FAQs

Package last updated on 16 Nov 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts