m6_migrate_old_tables::effectively_installed()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 *
4
 * Ideas extension for the phpBB Forum Software package.
5
 *
6
 * @copyright (c) phpBB Limited <https://www.phpbb.com>
7
 * @license GNU General Public License, version 2 (GPL-2.0)
8
 *
9
 */
10
11
namespace phpbb\ideas\migrations;
12
13
class m6_migrate_old_tables extends \phpbb\db\migration\migration
14
{
15
	public function effectively_installed()
16
	{
17
		return $this->db_tools->sql_column_exists($this->table_prefix . 'ideas_ideas', 'duplicate_id');
18
	}
19
20
	public static function depends_on()
21
	{
22
		return array(
23
			'\phpbb\ideas\migrations\m1_initial_schema',
24
			'\phpbb\ideas\migrations\m4_update_statuses',
25
		);
26
	}
27
28
	public function update_schema()
29
	{
30
		return array(
31
			'add_columns'		=> array(
32
				$this->table_prefix . 'ideas_ideas'			=> array(
33
					'duplicate_id'		=> array('UINT', 0),
34
					'ticket_id'			=> array('UINT', 0),
35
					'rfc_link'			=> array('VCHAR', ''),
36
				),
37
			),
38
		);
39
	}
40
41
	public function revert_schema()
42
	{
43
		return array(
44
			'drop_columns'        => array(
45
				$this->table_prefix . 'ideas_ideas'        => array(
46
					'duplicate_id',
47
					'ticket_id',
48
					'rfc_link',
49
				),
50
			),
51
		);
52
	}
53
54
	public function update_data()
55
	{
56
		return array(
57
			array('custom', array(array($this, 'migrate_tables'))),
58
		);
59
	}
60
61
	public function migrate_tables()
62
	{
63
		$this->move_table_data('ideas_rfcs', 'rfc_link');
64
		$this->move_table_data('ideas_duplicates', 'duplicate_id');
65
		$this->move_table_data('ideas_tickets', 'ticket_id');
66
	}
67
68
	/**
69
	 * Move data into the ideas table
70
	 *
71
	 * @param string $table  The name of the old table to get data from
72
	 * @param string $column The name of the column to get data from
73
	 */
74
	public function move_table_data($table, $column)
75
	{
76
		$data = array();
77
78
		$sql = 'SELECT *
79
			FROM ' . $this->table_prefix . $table;
80
		$result = $this->db->sql_query($sql);
81
		while ($row = $this->db->sql_fetchrow($result))
82
		{
83
			$data[$row['idea_id']] = $row[$column];
84
		}
85
		$this->db->sql_freeresult($result);
86
87
		$this->db->sql_transaction('begin');
88
89
		if (sizeof($data))
90
		{
91
			foreach ($data as $idea_id => $value)
92
			{
93
				$sql = 'UPDATE ' . $this->table_prefix . "ideas_ideas
94
					SET $column = '" . $this->db->sql_escape($value) . "'
95
					WHERE idea_id = " . (int) $idea_id;
96
				$this->db->sql_query($sql);
97
			}
98
		}
99
100
		$this->db->sql_transaction('commit');
101
	}
102
}
103