-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(repository): migrateSchema APIs
Introduce new APIs to simplify database migrations: - A new interface `MigrateableRepository` describing repositores that know how to migrate their database schema. - A new Application-level method `app.migrateSchema()` provided by `RepositoryMixin` and running schema migration provided by all registered repositories under the hood. Also: - Implement `MigrateableRepository` in `DefaultCrudRepository` using autoupdate/automigrate APIs provided by legacy-juggler's DataSource class. - Simplify the instructions shown in `Database-migrations.db` - Add an example `migrate.ts` script to our Todo example to verify that the code snippet shown in the docs works as intended.
- Loading branch information
Showing
9 changed files
with
266 additions
and
106 deletions.
There are no files selected for viewing
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
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
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,20 @@ | ||
// Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/example-todo | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {TodoListApplication} from './application'; | ||
|
||
export async function migrate(args: string[]) { | ||
const rebuild = args.includes('--rebuild'); | ||
console.log('Migrating schemas (%s)', rebuild ? 'rebuild' : 'update'); | ||
|
||
const app = new TodoListApplication(); | ||
await app.boot(); | ||
await app.migrateSchema({rebuild}); | ||
} | ||
|
||
migrate(process.argv).catch(err => { | ||
console.error('Cannot migrate database schema', err); | ||
process.exit(1); | ||
}); |
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
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
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
49 changes: 49 additions & 0 deletions
49
packages/repository/src/repositories/migrateable.repository.ts
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,49 @@ | ||
// Copyright IBM Corp. 2017,2018. All Rights Reserved. | ||
// Node module: @loopback/repository | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
import {Options} from '../common-types'; | ||
import {Model} from '../model'; | ||
import {Repository} from './repository'; | ||
|
||
export interface SchemaMigrationOptions extends Options { | ||
/** | ||
* When set to true, schema migration will drop existing tables and recreate | ||
* them from scratch, removing any existing data along the way. | ||
*/ | ||
rebuild?: boolean; | ||
} | ||
|
||
/** | ||
* A repository capable of database schema migration (auto-update/auto-migrate). | ||
*/ | ||
export interface MigrateableRepository<T extends Model> extends Repository<T> { | ||
/** | ||
* Update or recreate the database schema. | ||
* | ||
* **WARNING**: By default, `migrateSchema()` will attempt to preserve data | ||
* while updating the schema in your target database, but this is not | ||
* guaranteed to be safe. | ||
* | ||
* Please check the documentation for your specific connector(s) for | ||
* a detailed breakdown of behaviors for automigrate! | ||
* | ||
* @param options Migration options, e.g. whether to update tables | ||
* preserving data or rebuild everything from scratch. | ||
*/ | ||
migrateSchema(options?: SchemaMigrationOptions): Promise<void>; | ||
} | ||
|
||
/** | ||
* A type guard for detecting repositories implementing MigratableRepository | ||
* interface. | ||
* | ||
* @param repo The repository instance to check. | ||
*/ | ||
export function isMigrateableRepository<T extends Model = Model>( | ||
// tslint:disable-next-line:no-any | ||
repo: any, | ||
): repo is MigrateableRepository<T> { | ||
return typeof repo.migrateSchema === 'function'; | ||
} |
Oops, something went wrong.