Permalink
Browse files

feat(Migrator): add dev migrations

  • Loading branch information...
RWOverdijk committed Nov 5, 2016
1 parent 4d752a2 commit 7bd9e8f9544004cd48a259e79bdc512c188d4c97
Showing with 26 additions and 22 deletions.
  1. +26 −22 src/Migrator/Migrator.ts
@@ -3,7 +3,6 @@ import * as Bluebird from 'bluebird';
import * as Knex from 'knex';
import {Wetland} from '../Wetland';
import {Scope} from '../Scope';
import {SchemaBuilder} from '../SchemaBuilder';
import {Run} from './Run';
import {MigratorConfigInterface} from './MigratorConfigInterface';
import {MigrationFile} from './MigrationFile';
@@ -42,11 +41,6 @@ export class Migrator {
*/
private manager: Scope;
/**
* @type {SchemaBuilder}
*/
private schemaBuilder: SchemaBuilder;
/**
* @type {MigratorConfig}
*/
@@ -97,25 +91,35 @@ export class Migrator {
}
/**
* Create your database schema.
* Get all migrations from the directory.
*
* @returns {SchemaBuilder}
* @returns {Promise<Array<string>>}
*/
public createSchema(): SchemaBuilder {
if (!this.schemaBuilder) {
this.schemaBuilder = new SchemaBuilder(this.manager);
}
return this.schemaBuilder.create();
public allMigrations(): Bluebird<Array<string>|null> {
return this.migrationFile.getMigrations();
}
/**
* Get all migrations from the directory.
* Run dev migrations.
*
* @returns {Promise<Array<string>>}
* @param {boolean} revert
*
* @returns {Bluebird<any>}
*/
public allMigrations(): Bluebird<Array<string>|null> {
return this.migrationFile.getMigrations();
public devMigrations(revert: boolean): Bluebird<any> {
let snapshot = this.wetland.getSnapshotManager();
return snapshot
.fetch()
.then(previous => this.wetland.getSchemaManager().apply(previous, revert))
.then(() => snapshot.create())
.catch(error => {
if (revert) {
return Promise.resolve();
}
return this.devMigrations(true).then(() => Bluebird.reject(error));
});
}
/**
@@ -131,11 +135,12 @@ export class Migrator {
* Create a new migration file.
*
* @param {string} name
* @param {{}} [code]
*
* @returns {Promise<any>}
*/
public create(name: string): Bluebird<any> {
return this.migrationFile.create(name);
public create(name: string, code?: {up: string, down: string}): Bluebird<any> {
return this.migrationFile.create(name, code);
}
/**
@@ -159,8 +164,7 @@ export class Migrator {
* @returns {Promise}
*/
public down(action: string): Promise<string> {
return this.migrationTable.getLastMigrationName()
.then(name => this.run(Migrator.DIRECTION_DOWN, action, name));
return this.migrationTable.getLastMigrationName().then(name => this.run(Migrator.DIRECTION_DOWN, action, name));
}
/**

0 comments on commit 7bd9e8f

Please sign in to comment.