nicklayb /
Laravel 数据库导入包,方便操作
Requires
- illuminate/support: >=5.7
Requires (Dev)
- phpunit/phpunit: ^6.0
This package is auto-updated.
Last update: 2024-09-25 02:56:54 UTC
README
作者:Nicolas Boisvert :: nicklay@me.com
Laravel 数据库导入包,方便操作
用途
此包可以帮助您导入生产数据库。它可以用来将当前生产服务器迁移到另一个服务器,或者将生产数据库检查出来与相同数据进行测试,例如。
安装
通过composer安装
composer require nicklayb/laravel-db-import
在app.php
中注册服务提供者
Nicklayb\LaravelDbImport\ServiceProvider::class,
然后您需要发布它。
php artisan vendor:publish
配置
创建导入
首先您需要创建您的导入类。您可以在任何命名空间中创建一个类,使其扩展Nicklayb\LaravelDbImport\Import
类。这里是一个示例文件,我在我的commands命名空间中创建它,但您可以根据需要放置。有关更多参数,请参阅API部分。
<?php namespace Foo\Console\Commands; use Nicklayb\LaravelDbImport\Import; class ProdImport extends Import { protected $sourceConnection = 'source'; protected $destinationConnection = 'mysql'; }
您必须明确至少这两个参数。它们必须指向在您的database.php
配置文件中注册的数据库。将您的生产数据库连接名称放在$sourceConnection
中,而目标,猜猜看,在$destinationConnection
属性中。强烈建议使用只读用户连接源数据库,以防止错误。因为如果您将源放入目标,您会度过一段糟糕的时光。
操作
如果您希望在导入时操作表,例如设置时间戳为现在,您可以创建一个操作。您只需定义一个以manipulate
开头并以驼峰格式命名的表名的方法。该方法接收一个表行实例,您必须返回操作后的它。
<?php namespace Foo\Console\Commands; use Nicklayb\LaravelDbImport\Import; class ProdImport extends Import { protected $sourceConnection = 'source'; protected $destinationConnection = 'mysql'; public function manipulateUsers($user) { $user->created_at = Carbon::now(); return $user; } }
表筛选器
有时您可能需要筛选表以仅获取某些项目,例如仅获取过去6个月的作业。这可以通过添加表筛选器来实现。
假设我有一个名为orders
的表,我只需要过去6个月的项目。
namespace Foo\Console\Commands;
use Nicklayb\LaravelDbImport\Import;
class ProdImport extends Import
{
protected $sourceConnection = 'source';
protected $destinationConnection = 'mysql';
public function filterUsers($query)
{
return $query->where('created_at', '>', Carbon::now()->subMonths(6));
}
}
您将接收到基础查询参数,您应该返回修改后的查询。
注册导入
由于您已发布了供应商文件,您会注意到您配置文件中有一个全新的dbimport.php
。在这个文件中,您将注册您想要使用的所有导入类。
<?php return [ /** * Register your databases imports class here by specifying a key * and the class to manage this import * * 'production' => Namespace\ProductionImport::class */ 'imports' => [ 'prod' => Foo\Console\Commands\ProdImport::class ] ];
执行导入
在尝试之前,请确保您已注册了正确的源和目标
您可以使用以下命令通过artisan调用它
php artisan db:import prod
命令的prod
参数应与您在dbimport.php
中注册的密钥匹配。
API
以下是您可以覆盖以匹配您需求的属性和方法列表
<?php class MyImport extends Import { /** * The key of the source connection created in the database config file * * @var string */ protected $sourceConnection = 'source'; /** * The key of the destination connection created in the database config file * * @var string */ protected $destinationConnection = 'destination'; /** * Password reset option, yout must specify the table of the users as * key and specify the new password as the value. Default column * is 'password' but override it by adding :column * 'users:column_password' => 'superpassword' * * @var array */ protected $resetPassword = []; /** * Specify tables you don't want to import during the upload by specifying * the table name * * @var array */ protected $ignoreTables = []; /** * Set the tables to import after all the others, this is useful when you * are dealing with foreign key constraints * * @var array */ protected $lastTables = []; /** * Set this property to true to execute a php artisan migrate:refresh * before importing your database * * @var bool */ protected $refresh = false; /** * Specify table by table the select statement of which column to load like * ['users' => ['firstname', 'lastname']] * * @var array */ protected $selects = []; /** * Show table command, it may change depending on your database server * * @var string */ protected $showTablesCommand = 'SHOW TABLES'; /** * Key for default password when using reset passwords * * @var string */ protected $defaultPasswordColumn = 'password'; /** * Method that hashes password * * @param string $password * @return string */ public function hashPassword($password) { return bcrypt($password); } /** * Fill the array with Closures to execute before starting the import * * @return array */ public function preImport() { return []; } /** * Fill the array with Closures to execute after the import is done * * @return array */ public function postImport() { return []; } }
结论
感谢您使用、测试和改进它,如果您有任何问题,请随时联系我。
结尾笑话
我不把女性看作对象,我认为每个女性都是一个独特的类别。
许可
特此授予任何获得此软件及其相关文档文件(“软件”)副本的人(“用户”)免费处理软件的权利,包括但不限于使用、复制、修改、合并、发布、分发、再许可和/或出售软件副本,以及允许软件提供方这样做,但受以下条件约束
上述版权声明和本许可声明应包含在软件的所有副本或主要部分中。
软件按“原样”提供,不提供任何形式的保证,无论是明示的、暗示的,包括但不限于适销性、适用于特定目的和非侵权性保证。在任何情况下,作者或版权所有者不应对任何索赔、损害或其他责任负责,无论基于合同、侵权或其他行为,无论是否与软件或其使用或其他方式有关。