1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Daikon\CouchDb\Migration; |
4
|
|
|
|
5
|
|
|
use Daikon\Dbal\Exception\MigrationException; |
6
|
|
|
use Daikon\Dbal\Migration\MigrationTrait; |
7
|
|
|
use GuzzleHttp\Exception\RequestException; |
8
|
|
|
|
9
|
|
|
trait CouchDbMigrationTrait |
10
|
|
|
{ |
11
|
|
|
use MigrationTrait; |
12
|
|
|
|
13
|
|
View Code Duplication |
private function createDatabase(): void |
|
|
|
|
14
|
|
|
{ |
15
|
|
|
$client = $this->connector->getConnection(); |
16
|
|
|
$databaseName = $this->getDatabaseName(); |
17
|
|
|
|
18
|
|
|
try { |
19
|
|
|
$client->put('/'.$databaseName); |
20
|
|
|
} catch (RequestException $error) { |
21
|
|
|
if (!$error->hasResponse() || !$error->getResponse()->getStatusCode() === 409) { |
22
|
|
|
throw new MigrationException($error->getMessage()); |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
} |
26
|
|
|
|
27
|
|
View Code Duplication |
private function deleteDatabase(): void |
|
|
|
|
28
|
|
|
{ |
29
|
|
|
$client = $this->connector->getConnection(); |
30
|
|
|
$databaseName = $this->getDatabaseName(); |
31
|
|
|
|
32
|
|
|
try { |
33
|
|
|
$client->delete('/'.$databaseName); |
34
|
|
|
} catch (RequestException $error) { |
35
|
|
|
if (!$error->hasResponse() || !$error->getResponse()->getStatusCode() === 404) { |
36
|
|
|
throw new MigrationException($error->getMessage()); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function createDesignDoc(string $name, array $views): void |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$client = $this->connector->getConnection(); |
44
|
|
|
$databaseName = $this->getDatabaseName(); |
45
|
|
|
|
46
|
|
|
$body = [ |
47
|
|
|
'language' => 'javascript', |
48
|
|
|
'views' => $views |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
$requestPath = sprintf('/%s/_design/%s', $databaseName, $name); |
53
|
|
|
$client->put($requestPath, ['body' => json_encode($body)]); |
54
|
|
|
} catch (RequestException $error) { |
55
|
|
|
throw new MigrationException($error->getMessage()); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
private function deleteDesignDoc(string $name): void |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$client = $this->connector->getConnection(); |
62
|
|
|
$databaseName = $this->getDatabaseName(); |
63
|
|
|
|
64
|
|
|
try { |
65
|
|
|
$requestPath = sprintf('/%s/_design/%s', $databaseName, $name); |
66
|
|
|
$response = $client->head($requestPath); |
67
|
|
|
$revision = trim(current($response->getHeader('ETag')), '"'); |
68
|
|
|
$client->delete(sprintf('%s?rev=%s', $requestPath, $revision)); |
69
|
|
|
} catch (RequestException $error) { |
70
|
|
|
throw new MigrationException($error->getMessage()); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function getDatabaseName(): string |
75
|
|
|
{ |
76
|
|
|
$connectorSettings = $this->connector->getSettings(); |
77
|
|
|
return $connectorSettings['database']; |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|