Skip to content

Commit

Permalink
Merge pull request #469 from portabilis/portabilis-patch-2018-12-21
Browse files Browse the repository at this point in the history
Portabilis patch 21/12/2018
  • Loading branch information
edersoares committed Dec 21, 2018
2 parents 5559d56 + e84d246 commit 24b2da7
Show file tree
Hide file tree
Showing 218 changed files with 16,206 additions and 19,624 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -1,3 +1,4 @@
/database/backups
/database/data
/database/migrations/extras
/legacy
Expand Down
7 changes: 4 additions & 3 deletions .scrutinizer.yml
Expand Up @@ -27,18 +27,19 @@ tools:

build:
environment:
postgresql: true
variables:
APP_ENV: 'testing'
DB_CONNECTION: 'pgsql'
DB_HOST: 'localhost'
DB_PORT: '5432'
DB_DATABASE: 'ieducar_test'
DB_DATABASE: 'scrutinizer'
DB_USERNAME: 'scrutinizer'
DB_PASSWORD: 'scrutinizer'
dependencies:
before:
- psql -c "CREATE DATABASE ieducar_test WITH OWNER = scrutinizer ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' TEMPLATE template0"
- composer new-install
tests:
override:
command: "true"
services:
postgres: 9.5
222 changes: 222 additions & 0 deletions app/Console/Commands/DatabaseRestoreCommand.php
@@ -0,0 +1,222 @@
<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class DatabaseRestoreCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'database:restore {database} {filename}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Restore a database using a backup file';

/**
* Return the database host.
*
* @return string
*/
private function getHost()
{
return env('DB_HOST');
}

/**
* Return the database port.
*
* @return string
*/
private function getPort()
{
return env('DB_PORT');
}

/**
* Return the database user.
*
* @return string
*/
private function getUser()
{
return env('DB_USERNAME');
}

/**
* Return the database list filename.
*
* @return string
*/
private function getDatabaseList()
{
return storage_path('db.list');
}

/**
* Generate a database list from backup file.
*
* @param string $filename
*
* @return void
*/
private function createDatabaseList($filename)
{
$definition = 'pg_restore -l %s > %s';

$command = sprintf(
$definition,
$filename,
$this->getDatabaseList()
);

passthru($command);
}

/**
* Remove lines that contains table data imports.
*
* @param array $tables
*
* @return void
*/
private function removeTableDataFromDatabaseList(array $tables)
{
foreach ($tables as $table) {
$table = str_replace('.', ' ', $table);

$definition = 'sed -i \'/TABLE DATA %s/d\' %s';

$command = sprintf(
$definition,
$table,
$this->getDatabaseList(),
$this->getDatabaseList()
);

passthru($command);
}
}

/**
* Return audit tables.
*
* @return array
*/
private function getAuditTables()
{
return [
'modules.auditoria',
'modules.auditoria_geral',
'pmieducar.auditoria_falta_componente_dispensa',
'pmieducar.auditoria_nota_dispensa',
];
}

/**
* Drop old database if exists and create it again.
*
* @param string $database
*
* @return void
*/
private function dropAndCreateDatabase($database)
{
$definition = 'echo "drop database if exists %s; create database %s;" | psql -h %s -p %s -U %s';

$command = sprintf(
$definition,
$database,
$database,
$this->getHost(),
$this->getPort(),
$this->getUser()
);

passthru($command);
}

/**
* Restore the database using the backup file.
*
* @param string $database
* @param string $filename
*
* @return void
*/
private function restoreDatabaseUsingBackupFile($database, $filename)
{
$definition = 'pg_restore -L %s --host=%s --port=%s --username=%s --dbname=%s %s';

$command = sprintf(
$definition,
$this->getDatabaseList(),
$this->getHost(),
$this->getPort(),
$this->getUser(),
$database,
$filename
);

passthru($command);
}

/**
* Alter search path for database. Use legacy i-Educar configuration.
*
* @param string $database
*
* @return void
*/
private function alterSearchPathInDatabase($database)
{
$definition = 'echo "ALTER DATABASE %s SET search_path = \"\$user\", public, portal, cadastro, acesso, alimentos, consistenciacao, historico, pmiacoes, pmicontrolesis, pmidrh, pmieducar, pmiotopic, urbano, modules;" | psql -h %s -p %s -U %s';

$command = sprintf(
$definition,
$database,
$this->getHost(),
$this->getPort(),
$this->getUser()
);

passthru($command);
}

/**
* Delete the database list created.
*
* @return void
*/
private function deleteDatabaseList()
{
if (file_exists($this->getDatabaseList())) {
unlink($this->getDatabaseList());
}
}

/**
* Execute the console command.
*
* @return mixed
*/
public function handle()
{
$database = $this->argument('database');
$filename = $this->argument('filename');

$this->createDatabaseList($filename);
$this->removeTableDataFromDatabaseList($this->getAuditTables());
$this->dropAndCreateDatabase($database);
$this->restoreDatabaseUsingBackupFile($database, $filename);
$this->alterSearchPathInDatabase($database);
$this->deleteDatabaseList();
}
}
3 changes: 2 additions & 1 deletion composer.json
Expand Up @@ -3,7 +3,7 @@
"description": "Software livre de gestão escolar",
"type": "project",
"license": "GPL-2.0-or-later",
"version": "2.1.0",
"version": "2.1.1",
"keywords": [
"Portabilis",
"i-Educar"
Expand All @@ -15,6 +15,7 @@
"cocur/slugify": "^3.1",
"composer/semver": "^1.4",
"cossou/jasperphp": "^2.7",
"doctrine/dbal": "^2.9",
"fideloper/proxy": "^4.0",
"google/recaptcha": "^1.2",
"honeybadger-io/honeybadger-laravel": "^1.4",
Expand Down

0 comments on commit 24b2da7

Please sign in to comment.