php - 移动框架时的数据库迁移
问题描述
在我的工作地点,我们决定尝试对我们的一些网站进行现代化改造,这些网站是用原生 PHP 编写的。鉴于此,我们转向 Laravel,因为经过一些研究,这似乎是一个明智的选择。
最近我一直在研究数据迁移,因为我们有一个为大约 500 个需要迁移数据的客户提供服务的应用程序;该数据使用定制的 PHP 类进行加密,以处理加密和解密。
因此,我采取了以下步骤来处理迁移
- 使用应用程序中的解密功能对数据进行解密
- 将此数据移动到 CSV 文件中
- 更新了所有列并删除了不再使用的列
- 使用 phpMyAdmin 导入 CSV 文件
- 在我的 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
解决方案
推荐阅读
- javascript - 安装包含本机代码的软件包后,我的 react-native 应用程序无法在 android 上启动
- r - How to calculate a partial area under the curve (AUC)?
- jmeter - 使用 Jmeter 进行节流测试
- php - 使用 axios 上传到 AWS S3 时图像文件不正确
- javascript - 如何在 NodeJS 的任何猫鼬模型中使用数组?
- amazon-web-services - psycopg2 不适用于 AWS Lambda 上的无服务器框架部署
- gradle - AndroidX KTX 导航片段和导航 ui 的版本更新从版本 2.2.2 到 2.3.0 失败
- c# - Identity Server 4 Demo 返回无效的 ClientId
- pytorch - 使用 conv2d 的问题 - 错误的张量输入形状
- excel - 为什么我的用户表单输入字段不能退回小数位?