Skip to content
This repository has been archived by the owner on Jan 1, 2021. It is now read-only.

Apply fixes from StyleCI #17

Open
wants to merge 1 commit into
base: release/3.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion app/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ class Collection extends Model
{
//protected $with = [];

public function movies() {
public function movies()
{
return $this->hasMany("App\Movie");
}
}
110 changes: 51 additions & 59 deletions app/Console/Commands/DBDown.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Config;

class DBDown extends Command
{
Expand Down Expand Up @@ -42,34 +42,30 @@ public function handle()
{
//
$info = $this->get_maintenance_info();
if ($info === false)
{
$this->info("Starting maintenance mode!");
if ($info === false) {
$this->info('Starting maintenance mode!');
$config = $this->argument('maintenance_to_use');
if ($config == "")
{
$config = "maintenance";
if ($config == '') {
$config = 'maintenance';
}
$this->start_maintenance_mode($config);
}
else
{
} else {
$this->error("Maintenance mode already on the way. Currently on stage: {$info['stage_name']}");
}
}

protected function get_maintenance_info()
{
if (Storage::disk()->exists("db.down"))
{
$contents = Storage::get("db.down");
if (Storage::disk()->exists('db.down')) {
$contents = Storage::get('db.down');

return json_decode($contents, true);
}

return false;
}

protected function start_maintenance_mode($config = "tmdb_maintenance")
protected function start_maintenance_mode($config = 'tmdb_maintenance')
{
$this->replicate_tables($config);

Expand All @@ -78,47 +74,45 @@ protected function start_maintenance_mode($config = "tmdb_maintenance")

protected function replicate_tables($config)
{
$info = array(
"stage" => "db.replication",
"stage_name" => "Database Replication",
"progress" => 0.0
);
$info = [
'stage' => 'db.replication',
'stage_name' => 'Database Replication',
'progress' => 0.0,
];

$this->set_maintenance_info($info);
//Replicating database
$from_db = Config::get("database.$config.from_database");
$to_db = Config::get("database.$config.to_database");
$connection = Config::get("database.$config.db_connection");
if ($from_db == "" || $to_db == "")
{
$this->error("Please check your configuration!");
if ($from_db == '' || $to_db == '') {
$this->error('Please check your configuration!');
$this->free_maintenance_info();

return;
}

$this->info("Replicating database from $from_db to $to_db");

$conf_tables = Config::get("database.$config.tables");
$tables = $conf_tables;
if ($conf_tables == "all" || $conf_tables == "")
{
$tables = DB::select("SHOW TABLES FROM $from_db");
$real_tables = array();
foreach ($tables as $dict) {
foreach ($dict as $key => $value) {
$real_tables[] = $value;
}
}
$tables = $real_tables;
if ($conf_tables == 'all' || $conf_tables == '') {
$tables = DB::select("SHOW TABLES FROM $from_db");
$real_tables = [];
foreach ($tables as $dict) {
foreach ($dict as $key => $value) {
$real_tables[] = $value;
}
}
$tables = $real_tables;
}

$this->info("Replicating tables ".join(", ", $tables));
$this->info('Replicating tables '.implode(', ', $tables));

$first_tables = Config::get("database.$config.tables_before");

if (is_array($first_tables) && count($first_tables) > 0)
{
$this->info("Starting with table(s) ".join(",", $first_tables));
if (is_array($first_tables) && count($first_tables) > 0) {
$this->info('Starting with table(s) '.implode(',', $first_tables));

foreach ($first_tables as $value) {
if (($key = array_search($value, $tables)) !== false) {
Expand All @@ -130,13 +124,13 @@ protected function replicate_tables($config)
array_unshift($tables, $value);
}

$this->info("Order of replication: ".join(", ", $tables));
$this->info('Order of replication: '.implode(', ', $tables));
}

$current = 0;
$total = count($tables);

DB::connection($connection)->statement("SET foreign_key_checks = 0;");
DB::connection($connection)->statement('SET foreign_key_checks = 0;');
foreach ($tables as $value) {
try {
DB::connection($connection)->statement("DROP TABLE $to_db.$value");
Expand All @@ -145,16 +139,14 @@ protected function replicate_tables($config)
}

$create_syntax = DB::select("SHOW CREATE TABLE $from_db.$value");
$actual_syntax = "";
$actual_syntax = '';
foreach ($create_syntax as $syntax) {
//dd($syntax);
foreach ($syntax as $key => $val) {
if ($key == "Create Table")
{
if ($key == 'Create Table') {
$actual_syntax = $val;
}
}

}

$actual_syntax = str_replace("`$value`", "`$to_db`.`$value`", $actual_syntax);
Expand All @@ -163,38 +155,38 @@ protected function replicate_tables($config)
DB::connection($connection)->statement("INSERT INTO $to_db.$value SELECT * FROM $from_db.$value");
$current += 1;
$this->info("Replicated table $value. ($current/$total)");
$info = array(
"stage" => "db.replication",
"stage_name" => "Database Replication",
"progress" => $current / (float)$total
);
$info = [
'stage' => 'db.replication',
'stage_name' => 'Database Replication',
'progress' => $current / (float) $total,
];

$this->set_maintenance_info($info);
}
DB::connection($connection)->statement("SET foreign_key_checks = 1;");
DB::connection($connection)->statement('SET foreign_key_checks = 1;');

$this->info("Finished Replicating Database.");
$this->info('Finished Replicating Database.');
}

protected function finish_preparations()
{
$info = array(
"stage" => "finished",
"stage_name" => "Database Maintenance!",
"progress" => 1.0
);

$this->set_maintenance_info($info);
$this->info("Finished preparing Database Maintenance. You can now safely manipulate production database!");
$info = [
'stage' => 'finished',
'stage_name' => 'Database Maintenance!',
'progress' => 1.0,
];

$this->set_maintenance_info($info);
$this->info('Finished preparing Database Maintenance. You can now safely manipulate production database!');
}

protected function set_maintenance_info($info)
{
Storage::put("db.down", json_encode($info));
Storage::put('db.down', json_encode($info));
}

protected function free_maintenance_info()
{
Storage::delete("db.down");
Storage::delete('db.down');
}
}
Loading