首页 > 解决方案 > 移动框架时的数据库迁移

问题描述

在我的工作地点,我们决定尝试对我们的一些网站进行现代化改造,这些网站是用原生 PHP 编写的。鉴于此,我们转向 Laravel,因为经过一些研究,这似乎是一个明智的选择。

最近我一直在研究数据迁移,因为我们有一个为大约 500 个需要迁移数据的客户提供服务的应用程序;该数据使用定制的 PHP 类进行加密,以处理加密和解密。

因此,我采取了以下步骤来处理迁移

  1. 使用应用程序中的解密功能对数据进行解密
  2. 将此数据移动到 CSV 文件中
  3. 更新了所有列并删除了不再使用的列
  4. 使用 phpMyAdmin 导入 CSV 文件
  5. 在我的 Laravel 应用程序中安装了一个包,可以将数据库数据转换为可以在 Laravel 中使用的数据库播种器

我现在有一个名为 UsersTableSeeder 的播种器,其中包含所有旧用户数据。

在我的 User 模型中,我定义了 Accessors 和 Mutators 来通过 Laravel 的本机加密和解密函数来处理加密和解密。

例如,使用 first_name 我做了以下事情:

/**
 * Encrypt first_name
 *
 * @param [type] $value
 * @return void
 */
public function setFirstNameAttribute($value)
{
    $this->attributes['first_name'] = encrypt(ucfirst($value));
}

/**
 * Decrypt first_name if the field is NOT empty.
 *
 * @param [type] $value
 * @return void
 */
public function getFirstNameAttribute($value)
{
    return empty($value) ? '' : decrypt($value);
}

这种方法在从应用程序中写入和检索数据时有效。

我的UsersTableSeeder样子是这样的:

    \DB::table('users')->insert(array (
        0 => 
        array (
            'title' => 'Mr',
            'first_name' => 'first',
            'last_name' => 'last',
            'email' => 'me@gmail.com',
            'mobile_number' => NULL,
            'member_type' => 'Active',
            'contact_by_email' => '0',
            'contact_by_phone' => '0',
            'contact_by_post' => '0',
            'remember_token' => 'pOxr1PgwQo',
            'created_at' => '2018-01-29 00:00:00',
            'updated_at' => '2018-01-29 00:00:00',
        ),

这是通过反向播种我导入的数据库创建的。

问题是它\DB::table('users')->insert()不通过User模型,因此在运行种子时数据没有加密。

我可以更改播种器中的每个语句以使用User::create([]),但编写控制台命令是否更可行?

大家有没有遇到过类似的情况?

数据库迁移通常仅由数据库本身处理,而不是通过应用程序抽取数据吗?

我用于反向播种的包是:https ://github.com/orangehill/iseed

标签: phpmysqllaravelencryptiondata-migration

解决方案


推荐阅读