Laravel将数据从一个表移到另一个表并删除数据来自的列?

时间:2019-05-25 15:09:37

标签: php mysql laravel eloquent laravel-migrations

我正在尝试在Laravel迁移中执行以下过程:

创建一个名为client_log_partitions的新表。 (完成)

$table->create();

$table->bigIncrements('id')->unique();
$table->bigInteger('client_log_id');
$table->mediumText('partition_data');

$table->timestamps();

  • 我已经有一个名为client_logs的表,数据存储在名为bigText()的{​​{1}}列中。
  • log_data表中已经存在的每一行都需要每250 KB(或25万个字符,因为它应该是单字节编码)进行分割,然后作为分区插入到新的client_logs表中,并引用它所属的client_log_partitions条目。

我知道我可以使用client_logs来做到这一点,我只是不知道自己会用什么来做。

将数据移动到新表后,我需要从$table->postExecute()中删除log_data列。

通常,我会使用PHP脚本或类似的工具来执行此操作。但不幸的是,我在无法操作的情况下进行操作。


我的问题是,使用Laravel Migrations可以做到吗?如果可以,怎么办?

编辑

虽然我不确定,因为我只是即时进行测试,还没有测试,但这是我想实现此目的的SQL的样子:

client_logs

2 个答案:

答案 0 :(得分:0)

您需要做的就是运行代码以移动信息并在放置列调用之前重新关联ID。这是因为up函数中的所有内容都是从上到下顺序运行的,而不是异步运行的。 https://laravel.com/docs/5.8/migrations

您可以使用Eloquent和PHP来移动和更改信息。这样做可能比编写查询要容易。这就是我工作的公司移动数据然后更改表的方式。有时,由于SoX合规性的愚蠢,我们不得不使用SQL脚本来移动数据,但是您不必担心听起来像是。

class DropClientLogsLogDataColumn extends Migration
{

    public function up()
    {
        // move data
        $this->moveData()

        Schema::table('client_logs', function (Blueprint $table) {
          // drop the log_data column
          $table->dropColumn('log_data');
        });
    }

    private function moveData()
    {
      // do whatever you need to do here 
      // more than likely you'll use eloquent or query builder
      // this is where you'd associate the id's
    }
}

答案 1 :(得分:0)

将新表迁移方法向上更改为:

c#
<c#>
floating-point
<floating-point>
type-conversion
<type-conversion>
double
<double>
decimal
<decimal>

我认为以这种方式 migrate:rollback 命令也应该有效(撤消更改)!