Skip to content

Commit 24b2da7

Browse files
authored
Merge pull request #469 from portabilis/portabilis-patch-2018-12-21
Portabilis patch 21/12/2018
2 parents 5559d56 + e84d246 commit 24b2da7

File tree

218 files changed

+16206
-19624
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

218 files changed

+16206
-19624
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/database/backups
12
/database/data
23
/database/migrations/extras
34
/legacy

.scrutinizer.yml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,19 @@ tools:
2727

2828
build:
2929
environment:
30-
postgresql: true
3130
variables:
3231
APP_ENV: 'testing'
3332
DB_CONNECTION: 'pgsql'
3433
DB_HOST: 'localhost'
3534
DB_PORT: '5432'
36-
DB_DATABASE: 'ieducar_test'
35+
DB_DATABASE: 'scrutinizer'
3736
DB_USERNAME: 'scrutinizer'
3837
DB_PASSWORD: 'scrutinizer'
3938
dependencies:
4039
before:
41-
- psql -c "CREATE DATABASE ieducar_test WITH OWNER = scrutinizer ENCODING = 'UTF8' LC_COLLATE = 'en_US.UTF-8' TEMPLATE template0"
40+
- composer new-install
4241
tests:
4342
override:
4443
command: "true"
44+
services:
45+
postgres: 9.5
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
<?php
2+
3+
namespace App\Console\Commands;
4+
5+
use Illuminate\Console\Command;
6+
7+
class DatabaseRestoreCommand extends Command
8+
{
9+
/**
10+
* The name and signature of the console command.
11+
*
12+
* @var string
13+
*/
14+
protected $signature = 'database:restore {database} {filename}';
15+
16+
/**
17+
* The console command description.
18+
*
19+
* @var string
20+
*/
21+
protected $description = 'Restore a database using a backup file';
22+
23+
/**
24+
* Return the database host.
25+
*
26+
* @return string
27+
*/
28+
private function getHost()
29+
{
30+
return env('DB_HOST');
31+
}
32+
33+
/**
34+
* Return the database port.
35+
*
36+
* @return string
37+
*/
38+
private function getPort()
39+
{
40+
return env('DB_PORT');
41+
}
42+
43+
/**
44+
* Return the database user.
45+
*
46+
* @return string
47+
*/
48+
private function getUser()
49+
{
50+
return env('DB_USERNAME');
51+
}
52+
53+
/**
54+
* Return the database list filename.
55+
*
56+
* @return string
57+
*/
58+
private function getDatabaseList()
59+
{
60+
return storage_path('db.list');
61+
}
62+
63+
/**
64+
* Generate a database list from backup file.
65+
*
66+
* @param string $filename
67+
*
68+
* @return void
69+
*/
70+
private function createDatabaseList($filename)
71+
{
72+
$definition = 'pg_restore -l %s > %s';
73+
74+
$command = sprintf(
75+
$definition,
76+
$filename,
77+
$this->getDatabaseList()
78+
);
79+
80+
passthru($command);
81+
}
82+
83+
/**
84+
* Remove lines that contains table data imports.
85+
*
86+
* @param array $tables
87+
*
88+
* @return void
89+
*/
90+
private function removeTableDataFromDatabaseList(array $tables)
91+
{
92+
foreach ($tables as $table) {
93+
$table = str_replace('.', ' ', $table);
94+
95+
$definition = 'sed -i \'/TABLE DATA %s/d\' %s';
96+
97+
$command = sprintf(
98+
$definition,
99+
$table,
100+
$this->getDatabaseList(),
101+
$this->getDatabaseList()
102+
);
103+
104+
passthru($command);
105+
}
106+
}
107+
108+
/**
109+
* Return audit tables.
110+
*
111+
* @return array
112+
*/
113+
private function getAuditTables()
114+
{
115+
return [
116+
'modules.auditoria',
117+
'modules.auditoria_geral',
118+
'pmieducar.auditoria_falta_componente_dispensa',
119+
'pmieducar.auditoria_nota_dispensa',
120+
];
121+
}
122+
123+
/**
124+
* Drop old database if exists and create it again.
125+
*
126+
* @param string $database
127+
*
128+
* @return void
129+
*/
130+
private function dropAndCreateDatabase($database)
131+
{
132+
$definition = 'echo "drop database if exists %s; create database %s;" | psql -h %s -p %s -U %s';
133+
134+
$command = sprintf(
135+
$definition,
136+
$database,
137+
$database,
138+
$this->getHost(),
139+
$this->getPort(),
140+
$this->getUser()
141+
);
142+
143+
passthru($command);
144+
}
145+
146+
/**
147+
* Restore the database using the backup file.
148+
*
149+
* @param string $database
150+
* @param string $filename
151+
*
152+
* @return void
153+
*/
154+
private function restoreDatabaseUsingBackupFile($database, $filename)
155+
{
156+
$definition = 'pg_restore -L %s --host=%s --port=%s --username=%s --dbname=%s %s';
157+
158+
$command = sprintf(
159+
$definition,
160+
$this->getDatabaseList(),
161+
$this->getHost(),
162+
$this->getPort(),
163+
$this->getUser(),
164+
$database,
165+
$filename
166+
);
167+
168+
passthru($command);
169+
}
170+
171+
/**
172+
* Alter search path for database. Use legacy i-Educar configuration.
173+
*
174+
* @param string $database
175+
*
176+
* @return void
177+
*/
178+
private function alterSearchPathInDatabase($database)
179+
{
180+
$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';
181+
182+
$command = sprintf(
183+
$definition,
184+
$database,
185+
$this->getHost(),
186+
$this->getPort(),
187+
$this->getUser()
188+
);
189+
190+
passthru($command);
191+
}
192+
193+
/**
194+
* Delete the database list created.
195+
*
196+
* @return void
197+
*/
198+
private function deleteDatabaseList()
199+
{
200+
if (file_exists($this->getDatabaseList())) {
201+
unlink($this->getDatabaseList());
202+
}
203+
}
204+
205+
/**
206+
* Execute the console command.
207+
*
208+
* @return mixed
209+
*/
210+
public function handle()
211+
{
212+
$database = $this->argument('database');
213+
$filename = $this->argument('filename');
214+
215+
$this->createDatabaseList($filename);
216+
$this->removeTableDataFromDatabaseList($this->getAuditTables());
217+
$this->dropAndCreateDatabase($database);
218+
$this->restoreDatabaseUsingBackupFile($database, $filename);
219+
$this->alterSearchPathInDatabase($database);
220+
$this->deleteDatabaseList();
221+
}
222+
}

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"description": "Software livre de gestão escolar",
44
"type": "project",
55
"license": "GPL-2.0-or-later",
6-
"version": "2.1.0",
6+
"version": "2.1.1",
77
"keywords": [
88
"Portabilis",
99
"i-Educar"
@@ -15,6 +15,7 @@
1515
"cocur/slugify": "^3.1",
1616
"composer/semver": "^1.4",
1717
"cossou/jasperphp": "^2.7",
18+
"doctrine/dbal": "^2.9",
1819
"fideloper/proxy": "^4.0",
1920
"google/recaptcha": "^1.2",
2021
"honeybadger-io/honeybadger-laravel": "^1.4",

0 commit comments

Comments
 (0)