Postgres migration improvement; prevent MysqlToPostgres from being run

This commit is contained in:
Kelvin Zhang 2016-08-18 14:02:24 +01:00
parent bdbaca7a19
commit deaf14f53c
2 changed files with 33 additions and 33 deletions

View file

@ -33,35 +33,35 @@ class AddDeletedAtColumnToActivities extends Migration
Schema::table('activities', function(Blueprint $table) { Schema::table('activities', function(Blueprint $table) {
$table->softDeletes()->index(); $table->softDeletes()->index();
}); });
// Retroactively fix activities that should be marked as deleted. // Retroactively fix activities that should be marked as deleted.
// Tracks // Tracks
DB::table('activities') DB::table('activities')
->where('resource_type', 2) ->where('resource_type', 2)
->join('tracks', 'activities.resource_id', '=', 'tracks.id') ->join('tracks', 'activities.resource_id', '=', 'tracks.id')
->whereNotNull('tracks.deleted_at') ->whereNotNull('tracks.deleted_at')
->update(['activities.deleted_at' => DB::raw('tracks.deleted_at')]); ->update(['deleted_at' => DB::raw('tracks.deleted_at')]);
// Albums // Albums
DB::table('activities') DB::table('activities')
->where('resource_type', 3) ->where('resource_type', 3)
->join('albums', 'activities.resource_id', '=', 'albums.id') ->join('albums', 'activities.resource_id', '=', 'albums.id')
->whereNotNull('albums.deleted_at') ->whereNotNull('albums.deleted_at')
->update(['activities.deleted_at' => DB::raw('albums.deleted_at')]); ->update(['deleted_at' => DB::raw('albums.deleted_at')]);
// Playlists // Playlists
DB::table('activities') DB::table('activities')
->where('resource_type', 4) ->where('resource_type', 4)
->join('playlists', 'activities.resource_id', '=', 'playlists.id') ->join('playlists', 'activities.resource_id', '=', 'playlists.id')
->whereNotNull('playlists.deleted_at') ->whereNotNull('playlists.deleted_at')
->update(['activities.deleted_at' => DB::raw('playlists.deleted_at')]); ->update(['deleted_at' => DB::raw('playlists.deleted_at')]);
// Comments // Comments
DB::table('activities') DB::table('activities')
->where('resource_type', 5) ->where('resource_type', 5)
->join('comments', 'activities.resource_id', '=', 'comments.id') ->join('comments', 'activities.resource_id', '=', 'comments.id')
->whereNotNull('comments.deleted_at') ->whereNotNull('comments.deleted_at')
->update(['activities.deleted_at' => DB::raw('comments.deleted_at')]); ->update(['deleted_at' => DB::raw('comments.deleted_at')]);
} }
/** /**

View file

@ -15,34 +15,34 @@ class MysqlToPostgres extends Migration
*/ */
public function up() public function up()
{ {
$this->console = new ConsoleOutput(); // $this->console = new ConsoleOutput();
//
// Generate pgloader config // // Generate pgloader config
$mysqlConnection = "from mysql://" . env('DB_USERNAME') . ":" . env('DB_PASSWORD') . "@" . env('DB_HOST') . "/" . env('DB_DATABASE'); // $mysqlConnection = "from mysql://" . env('DB_USERNAME') . ":" . env('DB_PASSWORD') . "@" . env('DB_HOST') . "/" . env('DB_DATABASE');
$postgresConnection = "into postgresql://" . env('POSTGRESQL_DB_USERNAME', 'homestead') . ":" . env('POSTGRESQL_DB_PASSWORD', 'secret') . "@" . env('POSTGRESQL_DB_HOST', 'localhost') . "/" . env('POSTGRESQL_DB_DATABASE', 'homestead'); // $postgresConnection = "into postgresql://" . env('POSTGRESQL_DB_USERNAME', 'homestead') . ":" . env('POSTGRESQL_DB_PASSWORD', 'secret') . "@" . env('POSTGRESQL_DB_HOST', 'localhost') . "/" . env('POSTGRESQL_DB_DATABASE', 'homestead');
//
$header = "LOAD DATABASE"; // $header = "LOAD DATABASE";
$body = <<<'EOD' // $body = <<<'EOD'
with truncate //with truncate
//
CAST type datetime to timestamp using zero-dates-to-null, //CAST type datetime to timestamp using zero-dates-to-null,
type date to timestamp using zero-dates-to-null // type date to timestamp using zero-dates-to-null
//
EXCLUDING TABLE NAMES MATCHING 'migrations'; //EXCLUDING TABLE NAMES MATCHING 'migrations';
EOD; //EOD;
//
$output = implode("\n", array($header, $mysqlConnection, $postgresConnection, $body)); // $output = implode("\n", array($header, $mysqlConnection, $postgresConnection, $body));
$configPath = base_path() . "/pfmimport.load"; // $configPath = base_path() . "/pfmimport.load";
file_put_contents($configPath, $output); // file_put_contents($configPath, $output);
//
// Run pgloader // // Run pgloader
$this->execRunWithCallback("pgloader " . $configPath); // $this->execRunWithCallback("pgloader " . $configPath);
//
// Run after-import.sql // // Run after-import.sql
DB::unprepared(file_get_contents(base_path() . "/database/after-import.sql")); // DB::unprepared(file_get_contents(base_path() . "/database/after-import.sql"));
//
// Remove pgloader config // // Remove pgloader config
unlink($configPath); // unlink($configPath);
} }
private function execRunWithCallback($command) private function execRunWithCallback($command)