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
[8.x] Schema Dump #32275
[8.x] Schema Dump #32275
Conversation
I haven't had a chance to play with this yet, but i've found mysqldump to have issues with MySQL generated columns in the past, i'll try it out at the weekend if i get the chance, but something worth noting if you haven't come across those problems before |
Also, worth noting, how does this interact with migrations from packages? |
This is really cool. One project some developers in the past prior to joining the project had made changes to production databases manually without migration files and I've been wanting to get migration files in sync with the production database and this seems like it will solve that problem. Thanks! |
We have the same issue as @emielmolenaar . As Seeders are not versioned, it's hard to add data with them in a production environment. Let's assume we have predefined roles in a role table. Now i released a new Feature, let's say a public function up()
{
Role::firstWhere('name', 'Admin')
->permission()
->attach(Permission->firstWhere('name', 'flights.list'));
} @taylorotwell maybe it would make sense to provide a |
It's always best to extract data changes from your migrations. Your migrations generally don't run twice in your production environment and in your tests you don't want to migrate data every time your migrations are changed. |
@driesvints How can we accomplish that? I think migrations are generally a fine way to add or modify data; however if there any other ways to do this thus making this new feature even more useful I am open to suggestions. |
@emielmolenaar just remove the data modification lines from your migrations when you've run them in production. |
How will this affect database compatibility? |
@hubertnnn You can choose the database connection |
I don't think that will help either if the driver in that connection will change eg.
and following .env:
As you can see the connection did not change (is still "main") but the driver did. My question is if in this case the database dump will be stored in some platform agnostic format and will it work, or will it cause an error. |
It isn't in an agnostic format, its in the same format as the platform you export in, supporting only mysql and postgres (based on mysqldump or postgresdump being used). |
I have been doing this "by hand" just to have a better control of the migration files, I never thought about how this may help on tests. Thanks for always be one step ahead!! |
I just saw this new feature and it really amused me, very nice! It will help a lot of people and save a lot of time. If you have a lot of migrations you can significantly reduce the time to migrate them all. My question: - It is possible to decrease even more the migration time for projects with a large number of tables? I explain It with more details here: https://stackoverflow.com/questions/63855888/faster-laravel-migration-for-projects-with-a-lot-of-tables |
Another way I use is:
The 1) guarantees the DB is always in the same state after tests. Saved me a lot of time when I too had the need to only run singular tests. |
I think this feature is pretty nice, however, It seems that won't work at all for those who don't run tests using the same database as the one used in production e.g |
This won't work anymore now that sqlite support has been merged. |
Hello, is it recommended to prune migrations during a major release ? can we consider that users should have run all existing migrations before updating to the new version that remove those migrations ? they can always go back to the previous version to run the migrations and then update again. And it will prevent rollback once updated too. |
I personally don't recommend this approach for packages but if you follow semver then yes you'll have to do it in a major release and note about it in a changelog/upgrade guide. |
Hi, i'm running in the same issue, like @milewski explains, if we prune all migrations we can't setup any other database that are different than the one we use to generate the SQL dump ( and it's logical ) . However, it is possible to make an export for an sqlite or any other database, but we need to setup this one before and dump it after. I've tried this and got and sqlite-schema.sql and it's the one that is used when the tests runs migrations ( with a sqlite:memory database ) But what i've got is So, it could be interesting to dig how we can improve the workflow to dump ( and prune ) when we have a different database for testing. |
@Bouhnosaure since sqlite support has been merged my solution from above won't work anymore. I'll send in a note to the docs to try to explain that this cannot be used in combination with an in memory database. |
Alright @driesvints, indeed i was a bit sceptical when i've began my upgrade to 8.x for this reason, thanks for your quick answer ! |
Hi |
Well I've edited my .dump file with drop if exists statement before create table. Idk if it's supposed to do this. Using php artisan schema:dump --prune maybe should have this as default behaviour no ? thanks |
Try php artisan migrate:fresh --seed. This will drop all tables first.
…On Tue, Oct 6, 2020, 17:23 sdjrppl ***@***.***> wrote:
Hi
I'm currently having a problem with migrate:refresh command using previous
dump file, mysql .
I'm testing brand new project install. This project already have a *php
artisan migrate:dump --purge* file.
When I do *php artisan migrate:refresh --seed* , first time is ok. Second
time It throws error that table A already exists on database. Which means.
My schema dump, have migrations previously created, but using *--purge* ,
they not exists on migrations folder, like supposed. What I'm missing here ?
Thanks
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#32275 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAS3HC3SIFHSV3ROSWYXG23SJMY7NANCNFSM4MDH6FKA>
.
|
@emielmolenaar Dam I've mistaken php artisan migrate:refresh --seed with migrate:fresh --seed . You are right of course , wtf . |
nevermind my comment 😕 |
This PR adds support for a new
php artisan schema:dump
command which usesmysqldump
orpgdump
to dump the current state of your schema to adatabase/schema/{connection}-schema.mysql
file.When this file exists and
php artisan migrate
orphp artisan migrate:fresh
is run AND no migrations have run against the database yet (migrations
table is empty), this schema file will be loaded into the database first and then any outstanding migrations will be run. This means that effectively this schema file would typically only ever be used during local development or during CI testing. In production, you would typically already have migrations that have run in the past so this schema file would never be triggered.Once the schema file is created, any or all of your existing migrations my be deleted as long as they have already run against your production environment. They are no longer needed during local development since the schema file exists. For convenience, a
--prune
flag has been added to theschema:dump
command to delete your existing migrations after dumping the schema.This new feature solves two problems. First, it relieves developers from having a huge migrations directory full of files they no longer need. Second, loading a single schema file is quicker than running hundreds of migrations for each test class in your applications, so your tests can run much faster when using a schema.