Refresh Your Laravel Database Without Dropping Every Table

最后更新于 经过

Refresh Your Laravel Database Without Dropping Every Table image

Laravel 内置的 migrate:fresh drops every table before re-running your migrations, which is exactly what you want until it isn't. If a table holds seed data you don't want to regenerate, or test accounts you'd rather not recreate after every change, the all-or-nothing behaviour gets in the way. 定制新鲜 , by 马哈茂德·拉马丹 , adds a fresh:custom command that rebuilds the database while preserving the tables you name.

Choosing Which Tables Survive

Say you're building a subscription billing app and iterating on the schema all day, but you don't want to lose your test accounts or the seeded plans data on every refresh. You can name the tables to keep as a comma-separated argument or use the --keep flag. Both forms do the same thing:

php 工匠 fresh:custom users,plans
php 工匠 fresh:custom --keep=users,plans

Everything else is dropped, and the migrations run again, so the tables you list keep their rows while the rest of the schema is rebuilt from scratch.

Glob Patterns for Grouped Tables

Cashier alone creates subscriptionssubscription_items , and listing each one by name gets tedious. Custom Fresh accepts glob patterns, so a trailing wildcard matches any table sharing a prefix:

php 工匠 fresh:custom "users,plans,subscription_*"

Previewing Before You Commit

Because this command is destructive, you can ask it to report what it would do without touching the database. The --explain flag prints the tables it would keep and drop:

php 工匠 fresh:custom users,plans,subscription_ * --explain

You can also point the command at a specific connection with --database , which is handy when some data lives in a separate database:

php 工匠 fresh:custom monthly_revenue --database=analytics
Screenshot showing fresh:custom command
Screenshot showing the Artisan command fresh:custom dry run with --explain and also running it without a dry run

Configuration and Events

Publishing the config file lets you set defaults instead of passing the same flags on every run. Tables in always_keep survive automatically, patterns applies your glob rules without spelling them out each time, and confirm_in lists the environments where the command pauses for a confirmation prompt before it drops anything:

返回 [
'always_keep' => [ “用户” , 'plans' ],
“模式” => [ 'subscription_*' ],
'confirm_in' => [ “舞台” , '生产' ],
];

The command also dispatches three events during a run, which you can listen for to log activity or trigger follow-up work: RefreshingDatabase before any tables are dropped, TablesDropped after the removal step, and DatabaseRefreshed once the migrations finish.

安装

Install the package with Composer:

作曲家 要求 ramadan/custom-fresh

Custom Fresh requires PHP 8.2 or higher and supports Laravel 10 through 13. You can read the documentation and view the source on GitHub

Yannick Lyn Fatt 的照片

Laravel News 的特约撰稿人和全栈 Web 开发人员。

归档于:
立方体

Laravel 时事通讯

加入超过 4 万名开发者的行列,不错过任何新的技巧、教程等内容。

图像
Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

访问 Laravel Cloud
Tinkerwell 徽标

廷克威尔

Laravel 开发者必备的代码运行器。可在本地和生产环境中体验 AI、自动补全和即时反馈功能。

廷克威尔
Kirschbaum 标志

樱桃树

提供创新和稳定性,确保您的Web应用程序取得成功。

樱桃树
Laravel Cloud 标志

Laravel 云

轻松创建和管理服务器,并在几秒钟内部署 Laravel 应用程序。

Laravel 云
了解 Softtech 的标志

了解软科技

Acquaint Softtech 提供 AI 就绪的 Laravel 开发人员,48 小时内即可上手,每月费用为 3000 美元,没有冗长的销售流程,并提供 100% 退款保证。

了解软科技
Lucky Media 标志

幸运传媒

Get Lucky Now——拥有十余年经验的 Laravel 开发理想之选!

幸运传媒
PhpStorm 标志

PhpStorm

首选的 PHP IDE,对 Laravel 及其生态系统提供广泛的开箱即用支持。

PhpStorm
Shift 标志

转移

还在运行旧版本的 Laravel?立即实现 Laravel 自动升级和代码现代化,让您的应用程序保持最新状态。

转移
鱼叉:新一代时间跟踪和发票标志

Harpoon:新一代时间跟踪和发票系统

新一代时间跟踪和计费软件,帮助您的机构规划和预测盈利的未来。

Harpoon:新一代时间跟踪和发票系统
SaaSykit:Laravel SaaS 入门套件徽标

SaaSykit:Laravel SaaS 入门套件

SaaSykit 是一个多租户 Laravel SaaS 入门套件,包含运行现代 SaaS 所需的所有功能,例如支付、美观的结账界面、管理面板、用户仪表盘、身份验证、现成组件、统计数据、博客、文档等等。

SaaSykit:Laravel SaaS 入门套件
绝不妥协标志

绝不妥协

来自 No Compromises 播客的两位经验丰富的开发者 Joel 和 Aaron 现在可以为您的 Laravel 项目提供服务。⬧ 固定费用 9500 美元/月。⬧ 无冗长的销售流程。⬧ 无需签订合同。⬧ 100% 退款保证。

绝不妥协
Typed Translation Accessors in Laravel 13.15.0 image

Typed Translation Accessors in Laravel 13.15.0

阅读文章
JSON Schema Deserialization in Laravel 13.14 image

JSON Schema Deserialization in Laravel 13.14

阅读文章
Generate Short, URL-Safe IDs From Numbers With Sqids image

Generate Short, URL-Safe IDs From Numbers With Sqids

阅读文章
Scheduler List: A Web Dashboard for Laravel's Scheduled Tasks image

Scheduler List: A Web Dashboard for Laravel's Scheduled Tasks

阅读文章
Community Laravel Extension for Zed image

Community Laravel Extension for Zed

阅读文章
Advanced Eloquent Query Filtering with Filterable image

Advanced Eloquent Query Filtering with Filterable

阅读文章