در حال بارگذاری ...

Initial Migrations Setup0:00

Alright, migration squashing. So in my Laravel 8 project, I'll start by setting up a database connection. We'll just call it Laravel 8. And real quick, let's create that. Create database Laravel 8. Okay. So now I can run php artisan migrate, and that will run the initial migrations for the project. Okay. So in my database/migrations directory, yeah, these are the three that come with the framework. So no problem here at all.

Migration Bloat Problem0:25

So in my database/migrations directory, yeah, these are the three that come with the framework. So no problem here at all. But here's the thing. When you've been maintaining a project for many years, this migrations directory will grow and grow. Here's an example. The first LairCast migration was written in 2013. So if I were to list the files in that migrations folder, and let's just pass it to wordCount by line, yeah, 191 migrations. And each of those has to run every time I, for example, refresh my database.

Introducing Schema Dump0:52

by line, yeah, 191 migrations. And each of those has to run every time I, for example, refresh my database. It's a waste. So Laravel 8 can help us with this through a new command called schema:dump. So this effectively allows us to squash all of your migrations down to a schema file. So this would allow us to effectively squash all of your migrations down to a single schema file, which is really useful. So again, in this case, you wouldn't reach for it. You're starting out a brand new project, stick with your migrations. But yeah, after time, you may want to reach for this.

Dumping and Pruning Migrations1:27

You're starting out a brand new project, stick with your migrations. But yeah, after time, you may want to reach for this. So let's have a look. I'm going to run php artisan schema:dump. And there we go. So we'll now find a new schema directory here. And in fact, even better, if I ran this command again, but I added the --prune option, this will also delete any of the migration files in the process. Okay, so now here's what I'm left with. We have our schema file and no more migrations.

Migrating from Schema File1:54

Okay, so now here's what I'm left with. We have our schema file and no more migrations. So have a look. If I were to run php artisan migrate:fresh, even though the migrations are gone, you'll see it'll read first from the schema file and then pull from any migrations that you have. So if I were to switch over to something like TablePlus, give it a refresh, yeah, I still get those initial tables. Okay, so now, if I were to make a new migration, like create_posts_table, we effectively have a new starting point. So we start with a schema file, and then we build on top of it, as you see here.

Building on New Baseline2:28

a new starting point. So we start with a schema file, and then we build on top of it, as you see here. All right, so let's give it a run, php artisan migrate. Of course, TablePlus is going to reflect that. And then, of course, if we run a full php artisan migrate:fresh, it'll drop all of our tables, it'll run our schema file, and then it will load the migrations, and we'll get the exact same thing. So if you're brand new to Laravel, you may not reach for this immediately. But for those of us with long-running projects over many years, I'm very happy about this one.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟