Skip to content

Commit 0ff6d1e

Browse files
committed
improve ux
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
1 parent be487cc commit 0ff6d1e

File tree

5 files changed

+66
-24
lines changed

5 files changed

+66
-24
lines changed

lib/Command/PointList.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
125125

126126
$table = new Table($output);
127127
$table->setHeaders(
128-
['Restoring Point', 'Date', 'Parent', 'Comment', 'Status', 'Instance', 'Health']
128+
['Restoring Point', 'Date', 'NC', 'Parent', 'Comment', 'Status', 'Instance', 'Health']
129129
);
130130
$table->render();
131131

@@ -164,6 +164,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
164164
'<comment>' . (($point->isLocked()) ? 'L' : '') .
165165
(($point->isArchive()) ? 'A' : '') . '</comment> ' . (($fresh) ? $displayPointId : ''),
166166
($fresh) ? date('Y-m-d H:i:s', $point->getDate()) : '',
167+
($fresh) ? $point->getNCVersion() : '',
167168
($fresh) ? $point->getParent() : '',
168169
$comment,
169170
implode(',', $status),

lib/Command/PointRestore.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private function requestSqlParams(): array {
443443

444444
$helper = $this->getHelper('question');
445445
$question = new Question(
446-
'<comment> - Do you want to import the dump in the current database, or cancel the import ?</comment> (yes/No/cancel) ',
446+
' - <comment>Do you want to import the dump in the current database, or cancel the import ?</comment> (yes/No/cancel) ',
447447
'no',
448448
);
449449
$question->setAutocompleterValues(['cancel', 'yes', 'no']);
@@ -698,7 +698,7 @@ private function importSqlDump(RestoringPoint $point, RestoringData $data, array
698698
}
699699

700700
// $config = $this->extractDatabaseConfig();
701-
$sqlDump = $this->pointService->getSqlDump();
701+
$sqlDump = $this->pointService->getSqlDump($sqlParams);
702702
$sqlDump->setup($sqlParams);
703703

704704
$sqlDump->import($sqlParams, $read);

lib/Service/PointService.php

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -274,11 +274,11 @@ public function create(bool $complete): RestoringPoint {
274274

275275
$this->activityService->newActivity(
276276
'backup_create', [
277-
'id' => $point->getId(),
278-
'duration' => $point->getDuration(),
279-
'status' => $point->getStatus(),
280-
'complete' => $complete
281-
]
277+
'id' => $point->getId(),
278+
'duration' => $point->getDuration(),
279+
'status' => $point->getStatus(),
280+
'complete' => $complete
281+
]
282282
);
283283

284284
return $point;
@@ -522,7 +522,8 @@ private function backupSql(RestoringPoint $point) {
522522
}
523523

524524

525-
/**
525+
/** // TODO: add a way to get sql params from current config/config.php directly
526+
*
526527
* @return array
527528
*/
528529
public function getSqlParams(): array {
@@ -545,11 +546,19 @@ public function getSqlParams(): array {
545546
/**
546547
* return temp file name/path
547548
*
549+
* @param array $params
550+
*
548551
* @return ISqlDump
549552
* @throws SqlDumpException
550553
*/
551-
public function getSqlDump(): ISqlDump {
552-
switch ($this->configService->getSystemValue(ISqlDump::DB_TYPE)) {
554+
public function getSqlDump(array $params = []): ISqlDump {
555+
if (empty($params)) {
556+
$dbType = $this->configService->getSystemValue(ISqlDump::DB_TYPE);
557+
} else {
558+
$dbType = $this->get(ISqlDump::DB_TYPE, $params);
559+
}
560+
561+
switch ($dbType) {
553562
case ISqlDump::MYSQL:
554563
$sqlDump = new SqlDumpMySQL();
555564
break;

lib/SqlDump/SqlDumpMySQL.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public function __construct() {
6363
* @param array $params
6464
* @param string $filename
6565
*
66-
* @return string
66+
* @return void
6767
* @throws SqlDumpException
6868
*/
6969
public function export(array $params, string $filename): void {

lib/SqlDump/SqlDumpPgSQL.php

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
use ArtificialOwl\MySmallPhpTools\Traits\TArrayTools;
3535
use OCA\Backup\Exceptions\SqlDumpException;
36+
use OCA\Backup\Exceptions\SqlParamsException;
3637
use OCA\Backup\ISqlDump;
3738
use Spatie\DbDumper\Databases\PostgreSql;
3839
use Throwable;
@@ -62,14 +63,19 @@ public function __construct() {
6263
*/
6364
public function export(array $params, string $filename): void {
6465
try {
65-
PostgreSql::create()
66-
->setDbName($this->get('dbname', $params))
67-
->setUserName($this->get('dbuser', $params))
68-
->setPassword($this->get('dbpassword', $params))
69-
->setHost($this->get('dbhost', $params))
70-
->setPort($this->getInt('dbport', $params))
71-
->addExtraOption('--clean --inserts')
72-
->dumpToFile($filename);
66+
$dump = PostgreSql::create();
67+
$dump->setDbName($this->get(ISqlDump::DB_NAME, $params))
68+
->setUserName($this->get(ISqlDump::DB_USER, $params))
69+
->setPassword($this->get(ISqlDump::DB_PASS, $params))
70+
->setHost($this->get(ISqlDump::DB_HOST, $params));
71+
72+
$port = $this->getInt(ISqlDump::DB_PORT, $params);
73+
if ($port > 0) {
74+
$dump->setPort($port);
75+
}
76+
77+
$dump->addExtraOption('--clean --inserts')
78+
->dumpToFile($filename);
7379
} catch (Throwable $t) {
7480
throw new SqlDumpException($t->getMessage());
7581
}
@@ -78,8 +84,34 @@ public function export(array $params, string $filename): void {
7884

7985
/**
8086
* @param array $params
87+
*
88+
* @throws SqlParamsException
8189
*/
8290
public function setup(array $params): void {
91+
$port = $this->getInt(ISqlDump::DB_PORT, $params);
92+
if ($port === 0) {
93+
$port = null;
94+
}
95+
$sql = pg_connect(
96+
'host=' . $this->get(ISqlDump::DB_HOST, $params) .
97+
' dbname=' . $this->get(ISqlDump::DB_NAME, $params) .
98+
' user=' . $this->get(ISqlDump::DB_USER, $params) .
99+
' password=' . $this->get(ISqlDump::DB_PASS, $params)
100+
);
101+
102+
if (is_bool($sql) || is_null($sql)) {
103+
throw new SqlParamsException('cannot connect to database');
104+
}
105+
106+
$dbName = $this->get(ISqlDump::DB_NAME, $params);
107+
$check = pg_query($sql, 'SELECT FROM pg_database WHERE datname=\'' . $dbName . '\'');
108+
if (pg_num_rows($check) === 0) {
109+
pg_query($sql, 'CREATE DATABASE \'' . $dbName . '\'');
110+
$check = pg_query($sql, 'SELECT FROM pg_database WHERE datname=\'' . $dbName . '\'');
111+
if (pg_num_rows($check) === 0) {
112+
throw new SqlParamsException('cannot create database');
113+
}
114+
}
83115
}
84116

85117

@@ -91,10 +123,10 @@ public function setup(array $params): void {
91123
*/
92124
public function import(array $params, $read): bool {
93125
$sql = pg_connect(
94-
'host=' . $params['dbhost'] .
95-
' dbname=' . $this->get('dbname', $params) .
96-
' user=' . $this->get('dbuser', $params) .
97-
' password=' . $this->get('dbpassword', $params)
126+
'host=' . $this->get(ISqlDump::DB_HOST, $params) .
127+
' dbname=' . $this->get(ISqlDump::DB_NAME, $params) .
128+
' user=' . $this->get(ISqlDump::DB_USER, $params) .
129+
' password=' . $this->get(ISqlDump::DB_PASS, $params)
98130
);
99131

100132
$request = '';

0 commit comments

Comments
 (0)