Exponential 6.0.13 Exponential Enterprise Open Source CMS - PHP Class Reference

ezdbschemachecker.php
Go to the documentation of this file.
1 <?php
18 class eZDbSchemaChecker
19 {
34 static function diff( $schema1, $schema2 = array(), $schema1Type = false, $schema2Type = false )
35 {
36 if ( !is_array( $schema1 ) )
37 {
38 return false;
39 }
40 $diff = array();
41
42 foreach ( $schema2 as $name => $def )
43 {
44 // Skip the info structure, this is not a table
45 if ( $name == '_info' )
46 continue;
47
48 if ( !isset( $schema1[$name] ) )
49 {
50 $diff['new_tables'][$name] = $def;
51 }
52 else
53 {
54 $table_diff = eZDbSchemaChecker::diffTable( $schema1[$name], $def, $schema1Type, $schema2Type );
55 if ( count( $table_diff ) )
56 {
57 $diff['table_changes'][$name] = $table_diff;
58 }
59 }
60 }
61
62 /* Check if there are tables removed */
63 foreach ( $schema1 as $name => $def )
64 {
65 // Skip the info structure, this is not a table
66 if ( $name == '_info' )
67 continue;
68
69 if ( !isset( $schema2[$name] ) )
70 {
71 $diff['removed_tables'][$name] = $def;
72 }
73 else if ( isset( $schema2[$name]['removed'] ) and
74 isset( $schema2[$name]['removed'] ) )
75 {
76 $diff['removed_tables'][$name] = $def;
77 }
78 }
79
80 return $diff;
81 }
82
96 static function diffTable( $table1, $table2, $schema1Type, $schema2Type )
97 {
98 $table_diff = array();
99
100 /* See if all the fields in table 1 exist in table 2 */
101 foreach ( $table2['fields'] as $name => $def )
102 {
103 if ( !isset( $table1['fields'][$name] ) )
104 {
105 $table_diff['added_fields'][$name] = $def;
106 }
107 }
108 /* See if there are any removed fields in table 2 */
109 foreach ( $table1['fields'] as $name => $def )
110 {
111 if ( !isset( $table2['fields'][$name] ) )
112 {
113 $table_diff['removed_fields'][$name] = true;
114 }
115 else if ( isset( $table2['fields'][$name]['removed'] ) and
116 $table2['fields'][$name]['removed'] )
117 {
118 $table_diff['removed_fields'][$name] = true;
119 }
120 }
121 /* See if there are any changed definitions */
122 foreach ( $table1['fields'] as $name => $def )
123 {
124 if ( isset( $table2['fields'][$name] ) )
125 {
126 if ( is_array( $field_diff = eZDbSchemaChecker::diffField( $def, $table2['fields'][$name], $schema1Type, $schema2Type ) ) )
127 {
128 $table_diff['changed_fields'][$name] = $field_diff;
129 }
130 }
131 }
132
133 $table1Indexes = $table1['indexes'];
134 $table2Indexes = $table2['indexes'];
135
136 /* See if all the indexes in table 1 exist in table 2 */
137 foreach ( $table2Indexes as $name => $def )
138 {
139 if ( !isset( $table1Indexes[$name] ) )
140 {
141 $table_diff['added_indexes'][$name] = $def;
142 }
143 }
144 /* See if there are any removed indexes in table 2 */
145 foreach ( $table1Indexes as $name => $def )
146 {
147 if ( !isset( $table2Indexes[$name] ) )
148 {
149 $table_diff['removed_indexes'][$name] = $def;
150 }
151 else if ( isset( $table2Indexes[$name]['removed'] ) and
152 $table2Indexes[$name]['removed'] )
153 {
154 if ( isset( $table2Indexes[$name]['comments'] ) )
155 $def['comments'] = array_merge( isset( $def['comments'] ) ? $def['comments'] : array(),
156 $table2Indexes[$name]['comments'] );
157 $table_diff['removed_indexes'][$name] = $def;
158 }
159 }
160 /* See if there are any changed definitions */
161 foreach ( $table1Indexes as $name => $def )
162 {
163 if ( isset( $table2Indexes[$name] ) )
164 {
165 if ( is_array( $index_diff = eZDbSchemaChecker::diffIndex( $def, $table2Indexes[$name], $schema1Type, $schema2Type ) ) )
166 {
167 $table_diff['changed_indexes'][$name] = $index_diff;
168 }
169 }
170 }
171
172 return $table_diff;
173 }
174
181 static function diffField( $field1, $field2, $schema1Type, $schema2Type )
182 {
183 /* Type is always available */
184 if ( $field1['type'] != $field2['type'] )
185 {
186 return array( 'different-options' => array( 'type' ), 'field-def' => $field2 );
187 }
188
189 $test_fields = array( 'default', 'not_null' );
190
191 // Ignore length for int types
192 if ( $field1['type'] === $field2['type'] && $field1['type'] !== 'int' )
193 $test_fields[] = 'length';
194
195 $different_options = array();
196
197 foreach ( $test_fields as $test_field )
198 {
199 if ( isset( $field1[$test_field] ) )
200 {
201 if ( !isset( $field2[$test_field] ) ||
202 ( $field1[$test_field] != $field2[$test_field] ) )
203 {
204 $different_options[] = $test_field;
205 }
206 }
207 else
208 {
209 if ( isset( $field2[$test_field] ) && $field2[$test_field] )
210 {
211 $different_options[] = $test_field;
212 }
213 }
214 }
215
216 if ( $different_options )
217 return array( 'different-options' => $different_options, 'field-def' => $field2 );
218 else
219 return false;
220 }
221
228 static function diffIndex( $index1, $index2, $schema1Type, $schema2Type )
229 {
230 if ( ( $index1['type'] != $index2['type'] ) ||
231 serialize( $index1 ) !== serialize( $index2 ) )
232 {
233 return $index2;
234 }
235
236 $test_fields = array( 'link_table' );
237 foreach ( $test_fields as $test_field )
238 {
239 if ( isset($index1[$test_field] ) )
240 {
241 if ( !isset( $index2[$test_field] ) ||
242 ( $index1[$test_field] != $index2[$test_field] ) )
243 {
244 return $index2;
245 }
246 }
247 else
248 {
249 if ( isset($index2[$test_field] ) )
250 {
251 return $index2;
252 }
253 }
254 }
255
256 $test_fields = array( 'fields', 'link_fields' );
257 foreach ( $test_fields as $test_field )
258 {
259 if ( isset( $index1[$test_field] ) )
260 {
261 if ( !isset( $index2[$test_field] ) ||
262 !( $index1[$test_field] == $index2[$test_field] ) )
263 {
264 return $index2;
265 }
266 }
267 else
268 {
269 if ( isset( $index2[$test_field] ) )
270 {
271 return $index2;
272 }
273 }
274 }
275 }
276 }
277 ?>
eZDbSchemaChecker
Checks differences between schemas.
Definition: ezdbschemachecker.php:19
eZDbSchemaChecker\diffField
static diffField( $field1, $field2, $schema1Type, $schema2Type)
Definition: ezdbschemachecker.php:181
eZDbSchemaChecker\diff
static diff( $schema1, $schema2=array(), $schema1Type=false, $schema2Type=false)
Definition: ezdbschemachecker.php:34
eZDbSchemaChecker\diffIndex
static diffIndex( $index1, $index2, $schema1Type, $schema2Type)
Definition: ezdbschemachecker.php:228
eZDbSchemaChecker\diffTable
static diffTable( $table1, $table2, $schema1Type, $schema2Type)
Definition: ezdbschemachecker.php:96
Copyright © 1998 – 2026 7x. All rights reserved. Licensed under the GNU GPLv2+.