Laravel - large seed SQL file

A memory exception occurs when I run my DB script seed during production.

Below is my seed script.

class MembershipTableSeeder extends Seeder { public function run() { DB::table('members')->delete(); foreach (range(1, 99) as $days){ Members::create(array('membership_code' => 'test'.$days)); } DB::unprepared(file_get_contents(app_path()."/database/seeds/members.sql")); } } 

So what I did was add a restriction to my seed script.

 ini_set('memory_limit', '-1'); 

Now the problem is that when the script starts, it writes the output to the terminal of the contents of the SQL script (which is very, very large).

Is there a good way to run an SQL dump inside my seed database that doesn't consume much memory? Now I executed it manually:

 mysql -uuser -p db < script.sql 
+6
source share
3 answers

The problem arises because when using Db :: unprepared, it also logs the request in the laravel.log file, doing much more in the background than you think, from this side you will run out of memory. If you are not using safe mode, I would execute the console command as follows:

 exec("mysql -u ".\Config::get('database.mysql.user')." -p".\Config::get('database.mysql.password')." ".\Config::get('database.mysql.database')." < script.sql") 
+8
source

For others who prefer a more efficient Laravel-ish solution, I processed this:

 /** * This class is responsible for running the data dump sql. * It is recommended to update this class instead of creating new ones for new database content dumps. */ class DatabaseDumpSeeder extends Seeder { /** * Run the database seeds. * @throws \Exception */ public function run() { // Note: these dump files must be generated with DELETE (or TRUNCATE) + INSERT statements $sql = file_get_contents(__DIR__ . '/dumps/dump-20150709.sql'); if (! str_contains($sql, ['DELETE', 'TRUNCATE'])) { throw new Exception('Invalid sql file. This will not empty the tables first.'); } // split the statements, so DB::statement can execute them. $statements = array_filter(array_map('trim', explode(';', $sql))); foreach ($statements as $stmt) { DB::statement($stmt); } } } 
+11
source

// ================================================= ==================================================== ==============
// Create the Seeder file "PostalCodeTableSeeder.php" in Project_directory / database / seed
// ================================================= =================================================== ============

  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. "& lt postal_codes.sql");
         // postal_codes.sql is inside root folder
     }
 }

// ================================================= ==================================================== ==============
// Also add the class name to Project_directory / database / seed / DatabaseSeeder.php, for example, the 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);
     }
 }
+1
source

Source: https://habr.com/ru/post/975471/