-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Easy-to-use database migrations at application level #2059
Conversation
Right now, the pull request contains what I consider as a minimal viable implementation/improvement. Besides the changes proposed, I would like to discuss few more improvements we may or may not want to do, either as part of this pull request or in follow-ups:
@strongloop/loopback-next thoughts? BTW, I have also considered an entirely different approach, where the migration is executed by a new
|
65efb0d
to
223f5bd
Compare
b3f3789
to
61045e7
Compare
Hmm, I just realized that my proposed implementation is not flexible enough to support migration of related models. For example, when a Customer has many Order instances, the migration should create a foreign key constraint between Customer's id and Order's customerId properties/columns. This is difficult to achieve when migrating models one by one. I hope that moving The high-level API at application (RepositoryMixin) level seems fine to me as-is, it can easily support both repository-level and datasource-level migrations, thus I think it won't be affected by the changes in the underlying implementation. |
Let's discuss. I personally prefer a single method with an option to choose between an incremental update or destructive drop + create. I makes it easier to implement wrapper functions delegating the implementation to lower-level layers, e.g. a single When there are two methods, then we also need multiple wrapper functions ( What are your arguments for having two methods? I also find the names "update" and "migrate" not descriptive enough, the term "migrate" does not imply drop+create in my mind. AFAIK, the term "migration" is usually used for non-destructive upgrades. Few references:
I don't understand what's the use case for such filter. Here is the user story I am trying to address: As a developer wanting to run a LB4 app, I want an easy way how to update my database schema to match application's domain model. In this scenario, we always want to update all tables. What scenario do you @raymondfeng have in mind for choosing a subset of tables only? I think this filter is easy to add later as an incremental improvement of my MVP solution, thus I am proposing to leave it out of this initial pull request. |
61045e7
to
d8c52a6
Compare
In d8c52a6, I reworked the underlying implementation to leverage DataSource-level The pull request is ready for another round of review. @strongloop/loopback-maintainers PTAL. I tested the migration with |
d12b495
to
f015845
Compare
I think it's a limitation of LB3 |
Good to know, so I am not going to worry about this for now. I think the most important thing is that the current high-level design does not prevent the connector to setup referential integrity and possibly other constraints in the future. |
Developers want to control what models that need to be migrated. For example, our UI allows people to select a list of models and click In real world, some models are created newly while others are discovered from existing DB schema. We don't want run We can simply add |
I feel it's a bit of a scope creep here, but I am ok to add models to the options in the TS/JS API as you suggested 👍 |
f015845
to
929d80e
Compare
@raymondfeng I added support for LGTY now? I would also like to hear opinions from @strongloop/loopback-maintainers on the following topic I mentioned earlier: Right now, the pull request contains what I consider as a minimal viable implementation/improvement. Besides the changes proposed, I would like to discuss few more improvements we may or may not want to do, either as part of this pull request or in follow-ups:
|
IMHO, it is a good idea to have a more "automatic" way to create the database tables for users, especially for new users. Another question or discussion point is that what we want for the default behaviour for someone creating a LB app for real use. Maybe it's just me, I might just want the |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM overall, just have some (mostly) minor comments.
{% include code-caption.html content="src/application.ts" %} | ||
|
||
```ts | ||
export class TodoListApplication extends BootMixin( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick: maybe add a comment for imports on top?
Introduce a new Application-level method `app.migrateSchema()` provided by `RepositoryMixin`, this method executes schema migration as implemented by datasources registered with the application. 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.
929d80e
to
fcebea9
Compare
I find the current instructions for database migration as too involved for practical use, for example when running our example applications against a real database (SQL, MongoDB). I would like to propose a better user experience as a short-term solution to scratch this itch. Please see #487 for the discussion about a more comprehensive solution for long term.
In this pull request, I am introducing new APIs to simplify database migrations:
A new interfaceMigrateableRepository
describing repositores that know how to migrate their database schema.A new Application-level method
app.migrateSchema()
provided byRepositoryMixin
and running schema migration provided by all registeredrepositoriesdatasources under the hood.Using these two new APIs, I also
ImplementMigrateableRepository
inDefaultCrudRepository
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.Checklist
npm test
passes on your machinepackages/cli
were updatedexamples/*
were updated