Completed
Pull Request — master (#25)
by Matt
03:01
created

postgres_index::get_ts_name()   A

Complexity

Conditions 2
Paths 2

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 2
eloc 2
nc 2
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('config.update', array('similar_topics_sense', 1)),
34
			array('config.add', array('pst_postgres_ts_name', $this->get_ts_name())),
35
			array('if', array(
36
				$this->db->get_sql_layer() === 'postgres',
37
				array('custom', array(array($this, 'create_postgres_index'))),
38
			)),
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->create_fulltext_index();
59
	}
60
61
	/**
62
	 * Drop the PostgreSQL FULLTEXT index on phpbb_topics.topic_title
63
	 */
64
	public function drop_postgres_changes()
65
	{
66
		$driver = $this->get_driver();
67
68
		foreach ($driver->get_fulltext_indexes() as $index)
69
		{
70
			$sql = 'DROP INDEX ' . $index;
71
			$this->db->sql_query($sql);
72
		}
73
	}
74
75
	/**
76
	 * Get an instance of the similartopics POSTGRES driver
77
	 *
78
	 * @return \vse\similartopics\driver\postgres
79
	 */
80
	protected function get_driver()
81
	{
82
		return new \vse\similartopics\driver\postgres($this->db, $this->config);
83
	}
84
85
	protected function get_ts_name()
86
	{
87
		return $this->config->offsetExists('fulltext_postgres_ts_name') ? $this->config['fulltext_postgres_ts_name'] : 'simple';
88
	}
89
}
90