laravel 數據庫遷移轉 sql 語句


可以使用下面的命令

 php artisan migrate --pretend --no-ansi  

 當然,你需要有可以 migrate 的東西。

 

數據庫遷移導出到文件(使用命令)

<?php

namespace App\Console\Commands;

use Illuminate\Contracts\Bus\SelfHandling;

class MigrateToSql implements SelfHandling
{
    protected $signature = 'migrate_to_sql';

    protected $description = '數據庫遷移轉 sql';

    /**
     * Execute the command.
     *
     * @return void
     */
    public function handle()
    {
        $command = PHP_BINARY . ' ' . base_path('artisan') . ' migrate --pretend --no-ansi';
        exec($command, $output);

        $sql = '';
        if (count($output) > 0) {
            foreach ($output as $line) {
                $sql .= preg_replace('/.*?:/', '', $line) . ";\n";
            }
        }

        $file = database_path('sqls/' . date('Y-m-d H:i:s') . '.sql');
        file_put_contents($file, $sql);
    }
}

  上面的一些處理是把一些無效的信息去掉,如時間戳,這樣最后剩下的就是可以直接執行的 sql 語句了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM