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 migration:refresh, migration:reset, migration:fresh and db:wipe commands #764

Merged

Conversation

Julien-R44
Copy link
Member

@Julien-R44 Julien-R44 commented Jan 19, 2022

Proposed changes

Hey ! 👋
This PR add the following commands :

node ace migration:refresh
node ace migration:reset

node ace migration:reset is basically an alias for node ace migration:rollback --batch 0.
node ace migration:refresh calls migration:reset, migration:run, and db:seed ( if asked ).

node ace migration:refresh is a command that is absolutely missing in Adonis. I often find myself going into TablePlus, deleting my DB, and restarting my migrations + seeders. node ace migration:refresh --seed is a real time-saver in my opinion.
Also these two commands are very often used by Laravel developers, and they make up a big part of our community !

In a near future I will propose two new commands db:wipe and migration:fresh which will also have the same behavior as on Laravel : completely remove the DB (truncate, no rollback of migrations). That said it's a bit more complicated because I had a quick look and Knex doesn't seem to offer an abstraction for truncating, so we'll have to write our own abstraction or find a small plugin. ( If you have any recommendations please let me know )

Types of changes

  • Bugfix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)

Checklist

  • I have read the CONTRIBUTING doc
  • Lint and unit tests pass locally with my changes
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added necessary documentation (if appropriate)

Further comments

I don't expect this PR to be merged right away, I'm willing to work on it again because one point seems quite problematic to me:

I would have liked to use kernel.exec to execute the commands, it would have been much cleaner, but the problem is that the MigrationBase class closes the connection to the database at the end of each command.

So I had to add a shouldCloseConnectionAfterMigrations property, which I set to false when I need to run several commands in the same process.
I'm really not sure if this is the best solution. If you have something better to suggest, don't hesitate and I'll make the necessary changes!

@Julien-R44 Julien-R44 changed the title Feat/reset wipe refresh commands Add migration:refresh and migration:reset Jan 20, 2022
@Julien-R44 Julien-R44 force-pushed the feat/reset-wipe-refresh-commands branch from 78942fa to 8f1d902 Compare January 20, 2022 00:14
@thetutlage
Copy link
Member

Thanks for creating this PR. I know many have asked for it several times :)

That said it's a bit more complicated because I had a quick look and Knex doesn't seem to offer an abstraction for truncating

We have methods for dropping all the tables here https://github.com/adonisjs/lucid/blob/develop/src/Dialects/Pg.ts#L58-L61. Will this help?

@Julien-R44
Copy link
Member Author

Julien-R44 commented Jan 20, 2022

Exactly what I needed, I hadn't seen these functions! Thank you !

Edit : I guess it's just an oversight, but any reason for dropAllTables not being defined here? https://github.com/adonisjs/lucid/blob/develop/src/QueryClient/index.ts

@Julien-R44
Copy link
Member Author

I added dropAllTables to the QueryClient, and fixed a minor bug: an exception was thrown when dropAllTables was called with empty database on Postgres/Redshift

Related docs PR : adonisjs/v5-docs#115

@Julien-R44 Julien-R44 changed the title Add migration:refresh and migration:reset Add migration:refresh, migration:reset, migration:fresh and db:wipe commands Jan 20, 2022
@Julien-R44
Copy link
Member Author

Added migration:fresh and db:wipe with basic tests.
I think I'm done now, don't hesitate to give me some reviews I'll make the necessary changes. Thank you very much!

@thetutlage
Copy link
Member

Looks good to me at a first glance. Let's merge it and give it a test run

@thetutlage thetutlage merged commit 441d9b5 into adonisjs:develop Feb 5, 2022
@thetutlage
Copy link
Member

Thanks @Julien-R44 for spending time on this. Also, I was looking at Laravel and found that they also drop all the views and custom types from the database.

https://github.com/laravel/framework/blob/8.x/src/Illuminate/Database/Console/WipeCommand.php#L78-L96

Do you think it is worth dropping them?

@Julien-R44
Copy link
Member Author

Julien-R44 commented Feb 5, 2022

Great, thanks you !
About dropping views and custom types, yes it's definitely something that was on my todo list. Here I'll also check but I think we'll have to create our own abstraction to get all the views and custom types since Knex doesn't seem to offer an API for that.
And since this RP was already pretty big, I figured we'd leave it for now but I'll try to look into it as soon as possible !

@Julien-R44 Julien-R44 deleted the feat/reset-wipe-refresh-commands branch February 5, 2022 04:54
@thetutlage
Copy link
Member

Make sense!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants