Skip to content
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

Add posibility to upgrade generated database #98

Open
sergrdz opened this issue Apr 7, 2020 · 13 comments
Open

Add posibility to upgrade generated database #98

sergrdz opened this issue Apr 7, 2020 · 13 comments
Labels
enhancement 💎 New feature or request

Comments

@sergrdz
Copy link

sergrdz commented Apr 7, 2020

Like the jhipster version for java, it uses liquibase to generate changes in the database. It would be nice to add this feature.

EntityFramework does it using "dotnet ef migrations add Update_1"

@nicolas63
Copy link
Member

@sergrdz As you said before EF already provide the migration tool. Do you want the generator to do it automatically when an entity was generated ?

@sergrdz
Copy link
Author

sergrdz commented Apr 10, 2020

That's right, in the first generation of the application run the tool, and in subsequent changes to the model. In the current version, if changes are made to the model, they are not automatically added to the database. So the changes have to be added in another way.

@nicolas63
Copy link
Member

We can do that, but i think the user must be able to choose when the migration is created so i propose you to ask if the generator add migration after entity creation with this the user can create multiple entity and create migration on the last entity.What do you think about this ? I work currently on issues for milestone 1.0.0. Feel free to you submit a PR if you are interested by this fonctionality

@sergrdz
Copy link
Author

sergrdz commented Apr 13, 2020

I think it's a good idea. But when entities are generated with the command "import-jdl", I can't find a way to know which is the end of generation to invoke generation of migration.

The java version generates the changes in different ways, we could do it that way, always create the migration when using the entity generator.
https://www.jhipster.tech/development/#using-a-database

@nicolas63
Copy link
Member

Yes, it's good remarks. So, yes we could generate migration when using entity generator.

@ivanmonteiro
Copy link
Contributor

The java generator actually generates the liquibase migrations itself using templates, but since in dotnet we have the dotnet ef tool that automatically generates them I think using it is the way to go because it is just a simple command with one line.

I have added some documentation to the readme.md on how to use entity framework database migrations but it is still a manual job to run the dotnet ef command.

Is there any way to know when entity generation is finished, so we can automatically run the dotnet ef commands to generate the migrations?
If not, which workarounds can be used?

@nicolas63
Copy link
Member

For the jdl the better place is in https://github.com/jhipster/generator-jhipster/blob/master/cli/import-jdl.js
At the end of generateEntities(env, forkProcess)
But actually we not override this part.
Maybe and hook can be add in the main generator. Maybe @mshima can help us.

For generation after entity generation with cli we can add command here (https://github.com/jhipster/jhipster-dotnetcore/blob/master/generators/entity/index.js) and add a boolean in prompt.js for ask user about migration

@mshima
Copy link
Member

mshima commented Jun 27, 2020

Not sure but looks like you want something like this at the blueprint main generator:

get end() {
  return {
    checkEfCommand() {
      if (efCommandIsInstalled) {
        return;
      }
      if (this.spawnCommandSync('dotnet tool install --global dotnet-ef').result.status !== 0) {
        throw new Error('Could not install ef command');
      }
    },
    migrate() {
      if (initialMigrationExists) {
        if (this.spawnCommandSync('dotnet ef database update --project YourProject.csproj').result.status !== 0) {
          throw new Error('Could not update existing database migration');
        }
      } else {
        if (this.spawnCommandSync('dotnet ef migrations add InitialCreate --project YourProject.csproj').result.status !== 0) {
          throw new Error('Could not create database migration');
        }
      }
    }
  }
}

If it's optional then you can add a prompt or option

@nicolas63
Copy link
Member

@mshima thanks for your answer. I think your code work for one entity (like with entity generation with cli). The idea is to call this code only once after jdl generation rather than one per entity and i don't know if it is possible in the blueprint to add one callback after jdl generation.

@mshima
Copy link
Member

mshima commented Jun 27, 2020

@mshima thanks for your answer. I think your code work for one entity (like with entity generation with cli). The idea is to call this code only once after jdl generation rather than one per entity and i don't know if it is possible in the blueprint to add one callback after jdl generation.

Not sure why you think so.
If you put at main(app) generator it will run once per application, if you put at entity generator it will run once per entity.

There is no easy way to add a custom callback to jdl generation, only if you override jhipster import-jdl/jdl command. The only callback is the jhipster main generator, which is called like jhipster --with-entities --force (can be missing something).

@nicolas63
Copy link
Member

Ah yes I hadn't thought about the app script. I think it's a good solution.

Thank you very much for your advice @mshima

@ivanmonteiro
Copy link
Contributor

There is the Migrations Bundle Feature dotnet/efcore#19693 that will probably be released with EF Core 6 that might be helpful.

@xbotuk
Copy link

xbotuk commented Aug 21, 2021

It would be good if Liquibase was used for deploying DB schema and also for unit-testing.
Even if not at runtime, but at deployment time will be useful.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement 💎 New feature or request
Projects
None yet
Development

No branches or pull requests

5 participants