-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Platform-specific migrations, each in their own SQL file
- Loading branch information
1 parent
1585113
commit a0ab120
Showing
10 changed files
with
110 additions
and
58 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE NewTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
DROP TABLE NewTable; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; | ||
} |