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 语句了。

  • 相关阅读:
    deleted
    deleted
    deleted
    deleted
    deleted
    deleted
    POJ 1840 Eqs(乱搞)题解
    UVALive 6955 Finding Lines(随机化优化)题解
    CodeForces 828E DNA Evolution(树状数组)题解
    UVA 11019 Matrix Matcher(二维hash + 尺取)题解
  • 原文地址:https://www.cnblogs.com/eleven24/p/9405031.html
Copyright © 2011-2022 走看看