Completed
Pull Request — master (#25)
by Matt
02:10
created

postgres_index::depends_on()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * Precise Similar Topics
5
 *
6
 * @copyright (c) 2018 Matt Friedman
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace vse\similartopics\migrations\release_1_5_x;
12
13
class postgres_index extends \phpbb\db\migration\migration
14
{
15
	/**
16
	 * Do not run this migration if the DB is not PostgreSQL
17
	 *
18
	 * @return bool
19
	 */
20
	public function effectively_installed()
21
	{
22
		return $this->db->get_sql_layer() !== 'postgres' || $this->config->offsetExists('pst_postgres_ts_name');
23
	}
24
25
	public static function depends_on()
26
	{
27
		return array('\vse\similartopics\migrations\release_1_4_3_data');
28
	}
29
30
	public function update_data()
31
	{
32
		return array(
33
			array('if', array(
34
				$this->db->get_sql_layer() === 'postgres',
35
				array('custom', array(array($this, 'create_postgres_index'))),
36
			)),
37
			array('config.update', array('similar_topics_sense', 1)),
38
			array('config.add', array('pst_postgres_ts_name', $this->get_ts_name())),
39
		);
40
	}
41
42 View Code Duplication
	public function revert_data()
43
	{
44
		return array(
45
			array('if', array(
46
				$this->db->get_sql_layer() === 'postgres',
47
				array('custom', array(array($this, 'drop_postgres_changes'))),
48
			)),
49
		);
50
	}
51
52
	/**
53
	 * Create PostgreSQL FULLTEXT index for the topic_title
54
	 */
55
	public function create_postgres_index()
56
	{
57
		$driver = $this->get_driver();
58
		$driver->set_ts_name($this->get_ts_name())
59
			->create_fulltext_index();
60
	}
61
62
	/**
63
	 * Drop the PostgreSQL FULLTEXT index on phpbb_topics.topic_title
64
	 */
65
	public function drop_postgres_changes()
66
	{
67
		$driver = $this->get_driver();
68
69
		foreach ($driver->get_pg_indexes() as $index)
70
		{
71
			$sql = 'DROP INDEX ' . $index;
72
			$this->db->sql_query($sql);
73
		}
74
	}
75
76
	/**
77
	 * Get an instance of the similartopics POSTGRES driver
78
	 *
79
	 * @return \vse\similartopics\driver\postgres
80
	 */
81
	protected function get_driver()
82
	{
83
		return new \vse\similartopics\driver\postgres($this->db, $this->config);
84
	}
85
86
	protected function get_ts_name()
87
	{
88
		return $this->config->offsetExists('fulltext_postgres_ts_name') ? $this->config['fulltext_postgres_ts_name'] : 'simple';
89
	}
90
}
91