zoukankan      html  css  js  c++  java
  • laravel 数据库迁移转 sql 语句

    可以使用下面的命令

     php artisan migrate --pretend --no-ansi  

     当然,你需要有可以 migrate 的东西。

    数据库迁移导出到文件(使用命令)

    <?php
    
    namespace AppConsoleCommands;
    
    use IlluminateContractsBusSelfHandling;
    
    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) . ";
    ";
                }
            }
    
            $file = database_path('sqls/' . date('Y-m-d H:i:s') . '.sql');
            file_put_contents($file, $sql);
        }
    }
    

      上面的一些处理是把一些无效的信息去掉,如时间戳,这样最后剩下的就是可以直接执行的 sql 语句了。

  • 相关阅读:
    灾后重建
    购物
    [BZOJ3991][SDOI2015]寻宝游戏
    [BZOJ2286][SDOI2011]消耗战
    [Luogu4149][IOI2011]Race
    [BZOJ4003][JLOI2015]城池攻占
    [HDU5765]Bonds
    [HDU5977]Garden of Eden
    [Luogu4331][Baltic2004]数字序列
    [BZOJ4540][HNOI2016]序列
  • 原文地址:https://www.cnblogs.com/eleven24/p/9405031.html
Copyright © 2011-2022 走看看