Skip to content

Instantly share code, notes, and snippets.

@LiYiBin
Last active September 7, 2017 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save LiYiBin/3f9d79031323c6b57fa497d6fde9f3fb to your computer and use it in GitHub Desktop.
Save LiYiBin/3f9d79031323c6b57fa497d6fde9f3fb to your computer and use it in GitHub Desktop.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateExistTables extends Migration
{
private $sqlFolder = 'database/migrations/Dump20170907';
private function getDumpSQLs()
{
//
$files = scandir($this->sqlFolder);
return collect($files)->filter(function($filename) {
$isSQL = strpos($filename, '.sql');
return $isSQL;
});
}
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
//
$filenames = $this->getDumpSQLs();
collect($filenames)->each(function($filename) {
$sql = file_get_contents($this->sqlFolder . '/' . $filename);
DB::unprepared($sql);
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
//
$filenames = $this->getDumpSQLs();
collect($filenames)->each(function($filename) {
$isSQL = strpos($filename, '.sql');
if ($isSQL !== FALSE) {
$names = preg_split('/[_.]+/', $filename);
array_pop($names);
array_shift($names);
$tableName = implode('_', $names);
Schema::dropIfExists($tableName);
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment