migration-files
v0.4.3
Published
Utilities for SQL-based migration files
Downloads
142
Maintainers
Readme
migration-files
Utilities for SQL-based migration files.
Install
npm install --save migration-files
# or
yarn add migration-files
Usage
import { map } from 'extra-promise'
import { findMigrationFilenames, readMigrationFile } from 'migration-files'
const filenames = await findMigrationFilenames('./migrations')
const migrations = await map(filenames, readMigrationFile)
Migration format
001-initial.sql
--------------------------------------------------------------------------------
-- Up
--------------------------------------------------------------------------------
CREATE TABLE test (
id INTEGER PRIMARY KEY
);
--------------------------------------------------------------------------------
-- Down
--------------------------------------------------------------------------------
DROP TABLE test;
002-add name column.sql
--------------------------------------------------------------------------------
-- Up
--------------------------------------------------------------------------------
ALTER TABLE test
ADD COLUMN name TEXT;
--------------------------------------------------------------------------------
-- Down
--------------------------------------------------------------------------------
-- https://www.sqlite.org/faq.html#q11
BEGIN TRANSACTION;
CREATE TEMPORARY TABLE test_backup (
id INTEGER PRIMARY KEY
, name TEXT
);
INSERT INTO test_backup
SELECT id, name FROM test;
DROP TABLE test;
CREATE TABLE test (
id
);
INSERT INTO test
SELECT id FROM test_backup;
DROP TABLE test_backup;
COMMIT;
API
interface IMigration {
filename: string
version: number
name: string
up: string
down: string
}
readMigrationFile
function readMigrationFile(filename: string): Promise<IMigration>
findMigrationFilenames
function findMigrationFilenames(dirname: string): Promise<string[]>
parseMigrationFile
function parseMigrationFile(filename: string, content: string): IMigration