Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
45 / 45
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
helper
100.00% covered (success)
100.00%
45 / 45
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%
35 / 35
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        );
46
47        foreach ($nested_level as $change_type => $data_depth)
48        {
49            if (!empty($schema_changes[$change_type]))
50            {
51                foreach ($schema_changes[$change_type] as $key => $value)
52                {
53                    if ($data_depth === 1)
54                    {
55                        $steps[] = array(
56                            'dbtools.perform_schema_changes', array(array(
57                                    $change_type    => array(
58                                        (!is_int($key)) ? $key : 0    => $value,
59                                ),
60                            )),
61                        );
62                    }
63                    else if ($data_depth === 2)
64                    {
65                        foreach ($value as $key2 => $value2)
66                        {
67                            $steps[] = array(
68                                'dbtools.perform_schema_changes', array(array(
69                                    $change_type    => array(
70                                        $key => array(
71                                            $key2    => $value2,
72                                        ),
73                                    ),
74                                )),
75                            );
76                        }
77                    }
78                }
79            }
80        }
81
82        return $steps;
83    }
84
85    /**
86     * Reverse the update steps from an array of data changes
87     *
88     * 'If' statements and custom methods will be skipped, for all
89     * other calls the reverse method of the tool class will be called
90     *
91     * @param array $steps Update changes from migration
92     *
93     * @return array
94     */
95    public function reverse_update_data($steps)
96    {
97        $reversed_array = array();
98
99        foreach ($steps as $step)
100        {
101            $parts = explode('.', $step[0]);
102            $parameters = $step[1];
103
104            $class = $parts[0];
105            $method = isset($parts[1]) ? $parts[1] : false;
106
107            if ($class !== 'if' && $class !== 'custom')
108            {
109                array_unshift($parameters, $method);
110                $reversed_array[] = array($class . '.reverse', $parameters);
111            }
112        }
113
114        return array_reverse($reversed_array);
115    }
116}