0

I have the script below (following the advice from here):

public function up()
{
  set_time_limit(1000);
  $sqlFileDirectory =
    __DIR__.DIRECTORY_SEPARATOR.
    '..'.DIRECTORY_SEPARATOR.
    '..'.DIRECTORY_SEPARATOR.
    '..'.DIRECTORY_SEPARATOR.
    'db'.DIRECTORY_SEPARATOR.
    'imports'.DIRECTORY_SEPARATOR
  ;
  $dir = new \DirectoryIterator($sqlFileDirectory);
  foreach ($dir as $fileinfo)
  {
    if (!$fileinfo->isDot())
    {
      $sqlFile = $sqlFileDirectory.$fileinfo->getFilename();
      DB::unprepared(file_get_contents($sqlFile));

    }
  }
}

All it does is import sql files in the /db/imports folder. It gets the file contents and then does this:

DB::unprepared(file_get_contents($sqlFile));

Once completed, the terminal even says:

$ php artisan migrate
Migrating: 2020_08_13_111729_import_various_sql_files_from_existing_site
Migrated:  2020_08_13_111729_import_various_sql_files_from_existing_site (0.02 seconds)

But when I look in the migrations table, nothing!

If I run:

php artisan migrate

then again, it says:

$ php artisan migrate
Migrating: 2020_08_13_111729_import_various_sql_files_from_existing_site
Migrated:  2020_08_13_111729_import_various_sql_files_from_existing_site (0.02 seconds)

The moment I remove this:

DB::unprepared(file_get_contents($sqlFile));

Then the entry in the migration table is created.

Thoughts?

I even tried putting it in a try and catch. Still no luck.

My suspiciouns is perhaps one of the sql files is throwing an error that is causing the migration script not to create the entry.

Thoughts?

rockstardev
  • 13,479
  • 39
  • 164
  • 296

1 Answers1

1

Found it. MySqlDump adds this to the top of my exports:

SET AUTOCOMMIT = 0;

So I added this at the bottom of up():

DB::unprepared(' SET AUTOCOMMIT = 1; ');
rockstardev
  • 13,479
  • 39
  • 164
  • 296