Skip to content

Commit

Permalink
Platform-specific migrations, each in their own SQL file
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-north committed Mar 11, 2018
1 parent 1585113 commit a0ab120
Show file tree
Hide file tree
Showing 10 changed files with 110 additions and 58 deletions.
57 changes: 0 additions & 57 deletions migrations/20171203034929-first.js

This file was deleted.

3 changes: 3 additions & 0 deletions migrations/20171203034929-first.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { sqlFileMigration } from '../src/migration-utils';

export = sqlFileMigration('20171203034929-first');
File renamed without changes.
1 change: 1 addition & 0 deletions migrations/sqls/20171203034929-first/pg-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE NewTable;
4 changes: 4 additions & 0 deletions migrations/sqls/20171203034929-first/pg-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE NewTable (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
1 change: 1 addition & 0 deletions migrations/sqls/20171203034929-first/sqlite-down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
DROP TABLE NewTable;
4 changes: 4 additions & 0 deletions migrations/sqls/20171203034929-first/sqlite-up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE NewTable (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL
);
12 changes: 11 additions & 1 deletion scripts/db/migrate/create.sh
Original file line number Diff line number Diff line change
@@ -1,2 +1,12 @@
#!/usr/bin/env sh
./node_modules/.bin/db-migrate create -e sqlite $1 $2 $3 $4
# ./node_modules/.bin/db-migrate create -e sqlite $1 $2 $3 $4
DATE_STAMP=$(date "+%Y%m%d%H%M%S")
MIGRATION_NAME="$DATE_STAMP-$1"
echo "import { sqlFileMigration } from '../src/migration-utils';\n\nexport = sqlFileMigration('$MIGRATION_NAME');" > "./migrations/$MIGRATION_NAME.ts"
mkdir "./migrations/sqls/$MIGRATION_NAME"
echo "-- Put your MySQL \"up\" migration here" > "./migrations/sqls/$MIGRATION_NAME/mysql-up.sql"
echo "-- Put your MySQL \"down\" migration here" > "./migrations/sqls/$MIGRATION_NAME/mysql-down.sql"
echo "-- Put your PostgreSQL \"up\" migration here" > "./migrations/sqls/$MIGRATION_NAME/pg-up.sql"
echo "-- Put your PostgreSQL \"down\" migration here" > "./migrations/sqls/$MIGRATION_NAME/pg-down.sql"
echo "-- Put your SQLite \"up\" migration here" > "./migrations/sqls/$MIGRATION_NAME/sqlite-up.sql"
echo "-- Put your SQLite \"down\" migration here" > "./migrations/sqls/$MIGRATION_NAME/sqlite-down.sql"
86 changes: 86 additions & 0 deletions src/migration-utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import * as fs from 'fs';
import * as path from 'path';

const MIGRATION_PATH = path.join(__dirname, '..', 'migrations');

export interface Migration {
_meta: {
version: number
};
setup(options: any, seedLink: any): void;
up(db: any): void;
down(db: any): void;
}

function getDbType(db: any) {
if (db.connection.config) {
return 'mysql';
}
if (db.connection.port) {
return 'pg';
}
return 'sqlite';
}

export function sqlFileMigration(name: string): Migration {

let dbm;
let type;
let seed;
// tslint:disable-next-line:variable-name
let Promise: PromiseConstructor;
/**
* We receive the dbmigrate dependency from dbmigrate initially.
* This enables us to not have to rely on NODE_PATH.
*/
function setup(options: any, seedLink: any) {
dbm = options.dbmigrate;
type = dbm.dataType;
seed = seedLink;
Promise = options.Promise;
};

function up(db: any) {
let dbType = getDbType(db);
let filePath = path.join(MIGRATION_PATH, 'sqls', `${name}/${dbType}-up.sql`);
return new Promise((resolve, reject) => {
fs.readFile(filePath, {
encoding: 'utf-8'
}, (err, data) => {
if (err) { return reject(err); }
// tslint:disable-next-line:no-console
console.log('received data: ' + data);

resolve(data);
});
})
.then((data) => {
return db.runSql(data);
});
};

function down(db: any) {
let dbType = getDbType(db);
let filePath = path.join(MIGRATION_PATH, 'sqls', `${name}/${dbType}-down.sql`);
return new Promise((resolve, reject) => {
fs.readFile(filePath, {
encoding: 'utf-8'
}, (err, data) => {
if (err) { return reject(err); }
// tslint:disable-next-line:no-console
console.log('received data: ' + data);

resolve(data);
});
})
.then((data) => {
return db.runSql(data);
});
};

const _meta = {
version: 1
};

return { setup, up, down, _meta };
}

0 comments on commit a0ab120

Please sign in to comment.