jhipster / jhipster-dotnetcore

JHipster.NET blueprint
Apache License 2.0
314 stars 93 forks source link

Add posibility to upgrade generated database #98

Open sergrdz opened 4 years ago

sergrdz commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

ivanmonteiro commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

@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 commented 4 years ago

@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 commented 4 years ago

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 commented 3 years ago

There is the Migrations Bundle Feature https://github.com/dotnet/efcore/issues/19693 that will probably be released with EF Core 6 that might be helpful.

xbotuk commented 3 years ago

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.