aboutsummaryrefslogtreecommitdiffstats
path: root/phpBB/phpbb/db/migration/helper.php
blob: bce2efff51071bf2b31a50eeb6669995bd2e434a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/

namespace phpbb\db\migration;

/**
* The migrator is responsible for applying new migrations in the correct order.
*/
class helper
{
	/**
	 * Get the schema steps from an array of schema changes
	 *
	 * This splits up $schema_changes into individual changes so that the
	 * changes can be chunked
	 *
	 * @param array $schema_changes from migration
	 * @return array
	 */
	public function get_schema_steps($schema_changes)
	{
		$steps = array();

		// Nested level of data (only supports 1/2 currently)
		$nested_level = array(
			'drop_tables'		=> 1,
			'add_tables'		=> 1,
			'change_columns'	=> 2,
			'add_columns'		=> 2,
			'drop_keys'			=> 2,
			'drop_columns'		=> 2,
			'add_primary_keys'	=> 2, // perform_schema_changes only uses one level, but second is in the function
			'add_unique_index'	=> 2,
			'add_index'			=> 2,
		);

		foreach ($nested_level as $change_type => $data_depth)
		{
			if (!empty($schema_changes[$change_type]))
			{
				foreach ($schema_changes[$change_type] as $key => $value)
				{
					if ($data_depth === 1)
					{
						$steps[] = array(
							'dbtools.perform_schema_changes', array(array(
									$change_type	=> array(
										(!is_int($key)) ? $key : 0	=> $value,
								),
							)),
						);
					}
					else if ($data_depth === 2)
					{
						foreach ($value as $key2 => $value2)
						{
							$steps[] = array(
								'dbtools.perform_schema_changes', array(array(
									$change_type	=> array(
										$key => array(
											$key2	=> $value2,
										),
									),
								)),
							);
						}
					}
				}
			}
		}

		return $steps;
	}

	/**
	 * Reverse the update steps from an array of data changes
	 *
	 * 'If' statements and custom methods will be skipped, for all
	 * other calls the reverse method of the tool class will be called
	 *
	 * @param array $steps Update changes from migration
	 *
	 * @return array
	 */
	public function reverse_update_data($steps)
	{
		$reversed_array = array();

		foreach ($steps as $step)
		{
			$parts = explode('.', $step[0]);
			$parameters = $step[1];

			$class = $parts[0];
			$method = isset($parts[1]) ? $parts[1] : false;

			if ($class !== 'if' && $class !== 'custom')
			{
				array_unshift($parameters, $method);
				$reversed_array[] = array($class . '.reverse', $parameters);
			}
		}

		return array_reverse($reversed_array);
	}
}