Last active
September 7, 2017 03:02
-
-
Save LiYiBin/3f9d79031323c6b57fa497d6fde9f3fb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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