Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
helper
100.00% covered (success)
100.00%
46 / 46
100.00% covered (success)
100.00%
2 / 2
13
100.00% covered (success)
100.00%
1 / 1
 get_schema_steps
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
8
 reverse_update_data
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3*
4* This file is part of 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* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14namespace phpbb\db\migration;
15
16/**
17* The migrator is responsible for applying new migrations in the correct order.
18*/
19class helper
20{
21    /**
22     * Get the schema steps from an array of schema changes
23     *
24     * This splits up $schema_changes into individual changes so that the
25     * changes can be chunked
26     *
27     * @param array $schema_changes from migration
28     * @return array
29     */
30    public function get_schema_steps($schema_changes)
31    {
32        $steps = array();
33
34        // Nested level of data (only supports 1/2 currently)
35        $nested_level = array(
36            'drop_tables'        => 1,
37            'add_tables'        => 1,
38            'change_columns'    => 2,
39            'add_columns'        => 2,
40            'drop_keys'            => 2,
41            'drop_columns'        => 2,
42            'add_primary_keys'    => 2, // perform_schema_changes only uses one level, but second is in the function
43            'add_unique_index'    => 2,
44            'add_index'            => 2,
45            'rename_index'        => 1,
46        );
47
48        foreach ($nested_level as $change_type => $data_depth)
49        {
50            if (!empty($schema_changes[$change_type]))
51            {
52                foreach ($schema_changes[$change_type] as $key => $value)
53                {
54                    if ($data_depth === 1)
55                    {
56                        $steps[] = array(
57                            'dbtools.perform_schema_changes', array(array(
58                                    $change_type    => array(
59                                        (!is_int($key)) ? $key : 0    => $value,
60                                ),
61                            )),
62                        );
63                    }
64                    else if ($data_depth === 2)
65                    {
66                        foreach ($value as $key2 => $value2)
67                        {
68                            $steps[] = array(
69                                'dbtools.perform_schema_changes', array(array(
70                                    $change_type    => array(
71                                        $key => array(
72                                            $key2    => $value2,
73                                        ),
74                                    ),
75                                )),
76                            );
77                        }
78                    }
79                }
80            }
81        }
82
83        return $steps;
84    }
85
86    /**
87     * Reverse the update steps from an array of data changes
88     *
89     * 'If' statements and custom methods will be skipped, for all
90     * other calls the reverse method of the tool class will be called
91     *
92     * @param array $steps Update changes from migration
93     *
94     * @return array
95     */
96    public function reverse_update_data($steps)
97    {
98        $reversed_array = array();
99
100        foreach ($steps as $step)
101        {
102            $parts = explode('.', $step[0]);
103            $parameters = $step[1];
104
105            $class = $parts[0];
106            $method = isset($parts[1]) ? $parts[1] : false;
107
108            if ($class !== 'if' && $class !== 'custom')
109            {
110                array_unshift($parameters, $method);
111                $reversed_array[] = array($class . '.reverse', $parameters);
112            }
113        }
114
115        return array_reverse($reversed_array);
116    }
117}