Squiz Matrix  4.12.2
 All Data Structures Namespaces Functions Variables Pages
oracle_reset_sequences.php
1 <?php
26 if (ini_get('memory_limit') != '-1') ini_set('memory_limit', '-1');
27 error_reporting(E_ALL);
28 
29 if (php_sapi_name() != 'cli') trigger_error("You can only run this script from the command line\n", E_USER_ERROR);
30 
31 $SYSTEM_ROOT = (isset($_SERVER['argv'][1])) ? $_SERVER['argv'][1] : '';
32 
33 if (empty($SYSTEM_ROOT)) {
34  echo "ERROR: You need to supply the path to the System Root as the first argument\n";
35  exit();
36 }
37 
38 if (!is_dir($SYSTEM_ROOT) || !is_readable($SYSTEM_ROOT.'/core/include/init.inc')) {
39  echo "ERROR: Path provided doesn't point to a Matrix installation's System Root. Please provide correct path and try again.\n";
40  exit();
41 }
42 
43 define('SQ_SYSTEM_ROOT', $SYSTEM_ROOT);
44 define('SQ_LOG_PATH', SQ_SYSTEM_ROOT.'/data/private/logs');
45 
46 require_once 'DB.php';
47 require_once $SYSTEM_ROOT.'/data/private/conf/main.inc';
48 require_once 'XML/Tree.php';
49 require_once $SYSTEM_ROOT.'/fudge/dev/dev.inc';
50 require_once $SYSTEM_ROOT.'/core/include/general.inc';
51 
52 $info = parse_tables_xml($SYSTEM_ROOT.'/core/assets/tables.xml');
53 
54 $dal_conf = require_once($SYSTEM_ROOT.'/data/private/conf/db.inc');
55 $oci_db = MatrixDAL::dbConnect($dal_conf['db2'], 'db2');
56 MatrixDAL::changeDb('db2');
57 
58 // Drop any Oracle sequences that exist
59 bam('Dropping Oracle sequences');
60 
61  $oci_seqs = $oci_db->getAll('SELECT sequence_name FROM user_sequences');
62  foreach($oci_seqs as $key => $value) {
63  $del_seqs[] = strtolower(substr($value[0], 0, strlen($value[0]) - 4));
64  }
65 
66  foreach ($info['sequences'] as $sequence) {
67  if ($sequence == 'trig_id') {
68  $table = 'trig';
69  } else {
70  $table = $sequence;
71  }
72 
73  $key = $info['tables'][$table]['primary_key'][0];
74  $sequence_values[$sequence] = MatrixDAL::executeSqlOne('SELECT '.$key.' FROM sq_'.$table.' WHERE rownum = 1 ORDER BY '.$key.' DESC');
75  }
76 
77  if(isset($del_seqs)) {
78  foreach ($del_seqs as $sequence) {
79  printName('Dropping: '.strtolower($sequence).'_seq');
80  MatrixDAL::executeSql('DROP SEQUENCE '.$sequence.'_seq');
81  printUpdateStatus('OK');
82  }
83  }
84 
85 
86 // Recreate the sequences in Oracle
87 bam('Rebuilding Sequences');
88 
89 foreach ($info['sequences'] as $sequence) {
90 
91  $new_seq_start = $sequence_values[$sequence] + 1;
92 
93  printName('Creating sq_'.$sequence.'_seq (New start: '.$new_seq_start.')');
94  MatrixDAL::executeSql('CREATE SEQUENCE sq_'.$sequence.'_seq START WITH '.$new_seq_start);
95  printUpdateStatus('OK');
96 }
97 
106 function parse_tables_xml($xml_file)
107 {
108  $input = new XML_Tree($xml_file);
109  $root = &$input->getTreeFromFile();
110 
111  if (PEAR::isError($root)) {
112  trigger_error($root->getMessage()."\n".$root->getUserInfo(), E_USER_WARNING);
113  return false;
114  }
115  if ($root->name != 'schema' || $root->children[0]->name != 'tables' || $root->children[1]->name != 'sequences') {
116  trigger_error($root->getMessage()."\n".$root->getUserInfo(), E_USER_WARNING);
117  return false;
118  }
119 
120  $info = Array();
121  $info['tables'] = Array();
122 
123  //-- TABLES --//
124 
125  for ($i = 0; $i < count($root->children[0]->children); $i++) {
126  $table = &$root->children[0]->children[$i];
127  $table_name = $table->attributes['name'];
128 
129  $info['tables'][$table_name] = Array();
130  $info['tables'][$table_name]['rollback'] = (($table->attributes['require_rollback'] == 1) ? true : false);
131  $table_cols = &$table->children[0]->children;
132 
133  //-- TABLE COLUMNS --//
134 
135  $info['tables'][$table_name]['columns'] = Array();
136 
137  for ($j = 0; $j < count($table_cols); $j++) {
138  $table_column = &$table_cols[$j];
139  $column_name = $table_column->attributes['name'];
140 
141  $info['tables'][$table_name]['columns'][$column_name] = Array();
142  $info['tables'][$table_name]['columns'][$column_name]['allow_null'] = (($table_column->attributes['allow_null'] == 1) ? true : false);
143 
144  //-- TABLE COLUMN VARS --//
145 
146  $type = null;
147  $default = null;
148 
149  for ($k = 0; $k < count($table_column->children); $k++) {
150  $column_var = &$table_column->children[$k];
151 
152  switch (strtolower($column_var->name)) {
153  case 'type' :
154  // set the type of the column if it hasnt already been
155  // set in a variation (this is the default column type)
156  if (is_null($type)) $type = $column_var->content;
157  break;
158  case 'type_variations' :
159  // check for varitions of the column type for his database
160  for ($l = 0; $l < count($column_var->children); $l++) {
161  $variation = &$column_var->children[$l];
162  if ($variation->name == 'oci8') {
163  $type = $variation->content;
164  break;
165  }
166  }
167  break;
168  case 'default' :
169  if (trim($column_var->content) != '') {
170  $default = $column_var->content;
171  }
172  break;
173  default :
174  continue;
175  break;
176  }
177  }
178  $info['tables'][$table_name]['columns'][$column_name]['type'] = $type;
179  $info['tables'][$table_name]['columns'][$column_name]['default'] = $default;
180 
181  //-- KEYS --//
182 
183  $info['tables'][$table_name]['primary_key'] = Array();
184  $info['tables'][$table_name]['unique_key'] = Array();
185 
186  $table_keys = &$table->children[1]->children;
187  for ($jj = 0; $jj < count($table_keys); $jj++) {
188  $table_key = &$table_keys[$jj];
189 
190  // work out the columns in this key
191  $key_columns = Array();
192  for ($k = 0; $k < count($table_key->children); $k++) {
193  $col_name = $table_key->children[$k]->attributes['name'];
194  $key_columns[] = $col_name;
195 
196  // cache the primary key columns for this table
197  if ($table_key->name == 'primary_key') {
198  $info['tables'][$table_name]['primary_key'][] = $col_name;
199  }
200  if ($table_key->name == 'unique_key') {
201  $info['tables'][$table_name]['unique_key'][] = $col_name;
202  }
203  }
204  }
205 
206  //-- INDEXES --//
207 
208  // check for any indexes that need creating
209  $table_indexes = &$table->children[2]->children;
210  for ($kk = 0; $kk < count($table_indexes); $kk++) {
211  $table_index = &$table_indexes[$kk];
212 
213  // work out the columns in this index
214  for ($k = 0; $k < count($table_index->children); $k++) {
215  $index_col_name = $table_index->children[$k]->attributes['name'];
216  }
217 
218  // work out the name of the index
219  $index_name = array_get_index($table_index->attributes, 'name', $index_col_name);
220  $index_type = array_get_index($table_index->attributes, 'type', null);
221  $index_db_type = array_get_index($table_index->attributes, 'db', null);
222 
223  $index_info = Array(
224  'name' => $index_name,
225  'type' => $index_type,
226  'db_type' => $index_db_type,
227  );
228  $info['tables'][$table_name]['indexes'][$index_col_name] = $index_info;
229  }//end for
230 
231 
232  }
233 
234  }
235 
236  for ($i = 0; $i < count($root->children[1]->children); $i++) {
237  $sequence = &$root->children[1]->children[$i];
238  $sequence_name = $sequence->attributes['name'];
239  $info['sequences'][] = $sequence_name;
240  }
241 
242  return $info;
243 
244 }//end parse_tables_xml()
245 
246 
248  // HELPER FUNCTIONS //
250 function printName($name)
251 {
252  printf ('%s%'.(60 - strlen($name)).'s', $name, '');
253 
254 }//end printName()
255 
256 
257 function printUpdateStatus($status)
258 {
259  echo "[ $status ]\n";
260 
261 }//end printUpdateStatus()
262 
263 function dbDie(&$db)
264 {
265  echo 'Standard Message: ' . $db->getMessage() . "\n";
266  echo 'Standard Code: ' . $db->getMessage() . "\n";
267  echo 'DBMS/User Message: ' . $db->getUserInfo() . "\n";
268  echo 'DBMS/Debug Message: ' . $db->getDebugInfo() . "\n";
269  exit;
270 
271 }
272 
273 ?>