Updater   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 55
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 6
dl 0
loc 55
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 5 1
A update() 0 10 3
A handleTable() 0 8 2
A createTable() 0 3 1
A migrateTable() 0 12 2
1
<?php
2
3
namespace Wikibase\QueryEngine\SQLStore\Setup;
4
5
use Doctrine\DBAL\DBALException;
6
use Doctrine\DBAL\Schema\AbstractSchemaManager;
7
use Doctrine\DBAL\Schema\Comparator;
8
use Doctrine\DBAL\Schema\Table;
9
use Psr\Log\LoggerInterface;
10
use Wikibase\QueryEngine\QueryEngineException;
11
use Wikibase\QueryEngine\QueryStoreUpdater;
12
use Wikibase\QueryEngine\SQLStore\StoreSchema;
13
14
/**
15
 * @licence GNU GPL v2+
16
 * @author Jeroen De Dauw < [email protected] >
17
 */
18
class Updater implements QueryStoreUpdater {
19
20
	private $logger;
21
	private $storeSchema;
22
	private $schemaManager;
23
24
	public function __construct( LoggerInterface $logger, StoreSchema $storeSchema, AbstractSchemaManager $schemaManager ) {
25
		$this->logger = $logger;
26
		$this->storeSchema = $storeSchema;
27
		$this->schemaManager = $schemaManager;
28
	}
29
30
	/**
31
	 * @see QueryStoreUpdater::update
32
	 *
33
	 * @throws QueryEngineException
34
	 */
35
	public function update() {
36
		foreach ( $this->storeSchema->getTables() as $table ) {
37
			try {
38
				$this->handleTable( $table );
39
			}
40
			catch ( DBALException $ex ) {
41
				$this->logger->alert( $ex->getMessage(), array( 'exception' => $ex ) );
42
			}
43
		}
44
	}
45
46
	private function handleTable( Table $table ) {
47
		if ( $this->schemaManager->tablesExist( array( $table->getName() ) ) ) {
48
			$this->migrateTable( $table );
49
		}
50
		else {
51
			$this->createTable( $table );
52
		}
53
	}
54
55
	private function createTable( Table $table ) {
56
		$this->schemaManager->createTable( $table );
57
	}
58
59
	private function migrateTable( Table $table ) {
60
		$comparator = new Comparator();
61
62
		$tableDiff = $comparator->diffTable(
63
			$this->schemaManager->listTableDetails( $table->getName() ),
64
			$table
65
		);
66
67
		if ( $tableDiff !== false ) {
68
			$this->schemaManager->alterTable( $tableDiff );
69
		}
70
	}
71
72
}
73