Laravel - seeding large SQL file

后端 未结 3 1913
后悔当初
后悔当初 2021-02-08 01:41

A memory exhaustion happens when I run my DB seed script in production.

Below is my seed script.

class MembershipTableSeeder extends Seeder 
{
    public         


        
3条回答
  •  攒了一身酷
    2021-02-08 02:33

    Create Seeder File "PostalCodeTableSeeder.php" in Project_directory/database/seeds

    use Illuminate\Database\Seeder;
    
    class PostalCodeTableSeeder extends Seeder {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            // =============================================================
            // file Path -> Project/app/configs/database.php
            // get the database name, database username, database password
            // =============================================================
            $db     = \Config::get('database.connections.mysql.database');
            $user   = \Config::get('database.connections.mysql.username');
            $pass   = \Config::get('database.connections.mysql.password');
    
            // $this->command->info($db);
            // $this->command->info($user);
            // $this->command->info($pass);
    
            // running command line import in php code
            exec("mysql -u " . $user . " -p" . $pass . " " . $db . " < postal_codes.sql");
            // postal_codes.sql is inside root folder
        }
    }
    

    Also add the class name into Project_directory/database/seed/DatabaseSeeder.php like code below

    use Illuminate\Database\Seeder;
    
    class DatabaseSeeder extends Seeder
    {
        /**
         * Run the database seeds.
         *
         * @return void
         */
        public function run()
        {
            $this->call(PostalCodeTableSeeder::class);
            // $this->call(UsersTableSeeder::class);
        }
    }
    

提交回复
热议问题