Skip to content

Commit

Permalink
fixup! rename schema option, support model list
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Nov 27, 2018
1 parent 4386003 commit 929d80e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 16 deletions.
9 changes: 3 additions & 6 deletions examples/todo/src/migrate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,12 @@
import {TodoListApplication} from './application';

export async function migrate(args: string[]) {
const dropExistingSchema = args.includes('--rebuild');
console.log(
'Migrating schemas (%s)',
dropExistingSchema ? 'rebuild' : 'update',
);
const existingSchema = args.includes('--rebuild') ? 'drop' : 'alter';
console.log('Migrating schemas (%s existing schema)', existingSchema);

const app = new TodoListApplication();
await app.boot();
await app.migrateSchema({dropExistingSchema});
await app.migrateSchema({existingSchema});

// Connectors usually keep a pool of opened connections,
// this keeps the process running even after all works is done.
Expand Down
14 changes: 12 additions & 2 deletions packages/repository/src/datasource.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,18 @@ export interface DataSource {

export interface SchemaMigrationOptions extends Options {
/**
* When set to true, schema migration will drop existing tables and recreate
* When set to 'drop', schema migration will drop existing tables and recreate
* them from scratch, removing any existing data along the way.
*
* When set to 'alter', schema migration will try to preserve current schema
* and data, and perform a non-destructive incremental update.
*/
dropExistingSchema?: boolean;
existingSchema?: 'drop' | 'alter';

/**
* List of model names to migrate.
*
* By default, all models are migrated.
*/
models?: string[];
}
7 changes: 3 additions & 4 deletions packages/repository/src/mixins/repository.mixin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,8 @@ export function RepositoryMixin<T extends Class<any>>(superClass: T) {
* preserving data or rebuild everything from scratch.
*/
async migrateSchema(options: SchemaMigrationOptions = {}): Promise<void> {
const operation = options.dropExistingSchema
? 'automigrate'
: 'autoupdate';
const operation =
options.existingSchema === 'drop' ? 'automigrate' : 'autoupdate';

// Instantiate all repositories to ensure models are registered & attached
// to their datasources
Expand All @@ -201,7 +200,7 @@ export function RepositoryMixin<T extends Class<any>>(superClass: T) {

if (operation in ds && typeof ds[operation] === 'function') {
debug('Migrating dataSource %s', b.key);
await ds[operation]();
await ds[operation](options.models);
} else {
debug('Skipping migration of dataSource %s', b.key);
}
Expand Down
17 changes: 13 additions & 4 deletions packages/repository/test/unit/mixins/repository.mixin.unit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('RepositoryMixin', () => {
it('calls autoupdate on registered datasources', async () => {
app.dataSource(DataSourceStub);

await app.migrateSchema({dropExistingSchema: false});
await app.migrateSchema({existingSchema: 'alter'});

sinon.assert.called(updateStub);
sinon.assert.notCalled(migrateStub);
Expand All @@ -95,7 +95,7 @@ describe('RepositoryMixin', () => {
it('calls automigrate on registered datasources', async () => {
app.dataSource(DataSourceStub);

await app.migrateSchema({dropExistingSchema: true});
await app.migrateSchema({existingSchema: 'drop'});

sinon.assert.called(migrateStub);
sinon.assert.notCalled(updateStub);
Expand All @@ -115,7 +115,7 @@ describe('RepositoryMixin', () => {
.tag('datasource')
.inScope(BindingScope.SINGLETON);

await app.migrateSchema({dropExistingSchema: true});
await app.migrateSchema({existingSchema: 'drop'});
// the test passes when migrateSchema() does not throw any error
});

Expand Down Expand Up @@ -146,11 +146,20 @@ describe('RepositoryMixin', () => {
}
app.repository(ProductRepository);

await app.migrateSchema({dropExistingSchema: true});
await app.migrateSchema({existingSchema: 'drop'});

expect(modelsMigrated).to.eql(['Product']);
});

it('migrates selected models only', async () => {
app.dataSource(DataSourceStub);

await app.migrateSchema({existingSchema: 'drop', models: ['Category']});

sinon.assert.calledWith(migrateStub, ['Category']);
sinon.assert.notCalled(updateStub);
});

function setupTestHelpers() {
app = new AppWithRepoMixin();

Expand Down

0 comments on commit 929d80e

Please sign in to comment.