From: stronk7 Date: Tue, 28 Aug 2007 23:03:40 +0000 (+0000) Subject: Adding one utility to detect and propose DB changes in X-Git-Url: http://git.mjollnir.org/gw?a=commitdiff_plain;h=498a550d010da613c68e25043b1b610b5fd15892;p=moodle.git Adding one utility to detect and propose DB changes in order to have all the int(10) fields properly defined. MDL-11038 Test, test, test!!! --- diff --git a/admin/xmldb/actions/check_bigints/check_bigints.class.php b/admin/xmldb/actions/check_bigints/check_bigints.class.php new file mode 100644 index 0000000000..c908fabf46 --- /dev/null +++ b/admin/xmldb/actions/check_bigints/check_bigints.class.php @@ -0,0 +1,271 @@ +loadStrings(array( + 'confirmcheckbigints' => 'xmldb', + 'ok' => '', + 'wrong' => 'xmldb', + 'table' => 'xmldb', + 'field' => 'xmldb', + 'searchresults' => 'xmldb', + 'wrongints' => 'xmldb', + 'completelogbelow' => 'xmldb', + 'nowrongintsfound' => 'xmldb', + 'yeswrongintsfound' => 'xmldb', + 'yes' => '', + 'no' => '', + 'error' => '', + 'back' => 'xmldb' + + )); + } + + /** + * Invoke method, every class will have its own + * returns true/false on completion, setting both + * errormsg and output as necessary + */ + function invoke() { + parent::invoke(); + + $result = true; + + /// Set own core attributes + $this->does_generate = ACTION_GENERATE_HTML; + + /// These are always here + global $CFG, $XMLDB, $db; + + /// And we nedd some ddl suff + require_once ($CFG->libdir . '/ddllib.php'); + + /// Here we'll acummulate all the wrong fields found + $wrong_fields = array(); + + /// Correct fields must be type bigint for MySQL and int8 for PostgreSQL + switch ($CFG->dbfamily) { + case 'mysql': + $correct_type = 'bigint'; + break; + case 'postgres': + $correct_type = 'int8'; + break; + default: + $correct_type = NULL; + } + + /// Do the job, setting $result as needed + + /// Get the confirmed to decide what to do + $confirmed = optional_param('confirmed', false, PARAM_BOOL); + + /// If not confirmed, show confirmation box + if (!$confirmed) { + $o = ''; + $o.= ' '; + $o.= '
'; + $o.= '

' . $this->str['confirmcheckbigints'] . '

'; + $o.= ' '; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + $o.= '
'; + + $this->output = $o; + } else { + /// The back to edit table button + $b = '

'; + $b .= '[' . $this->str['back'] . ']'; + $b .= '

'; + + /// Iterate over $XMLDB->dbdirs, loading their XML data to memory + if ($XMLDB->dbdirs) { + $dbdirs =& $XMLDB->dbdirs; + $o=''; + } + + /// We have finished, let's show the results of the search + $s = ''; + $r = ''; + $r.= ' '; + $r.= ' '; + $r.= ' '; + $r.= '
'; + $r.= '

' . $this->str['searchresults'] . '

'; + $r.= '

' . $this->str['wrongints'] . ': ' . count($wrong_fields) . '

'; + $r.= '
'; + + /// If we have found wrong integers inform about them + if (count($wrong_fields)) { + $r.= '

' . $this->str['yeswrongintsfound'] . '

'; + $r.= '
    '; + foreach ($wrong_fields as $obj) { + $xmldb_table = $obj->table; + $xmldb_field = $obj->field; + /// MySQL directly supports this + if ($CFG->dbfamily == 'mysql') { + $sqlarr = $xmldb_table->getAlterFieldSQL($CFG->dbtype, $CFG->prefix, $xmldb_field, true); + /// PostgreSQL (XMLDB implementation) is a bit, er... imperfect. + } else if ($CFG->dbfamily == 'postgres') { + $sqlarr = array('ALTER TABLE ' . $CFG->prefix . $xmldb_table->getName() . + ' ALTER COLUMN ' . $xmldb_field->getName() . ' TYPE BIGINT;'); + } + $r.= '
  • ' . $this->str['table'] . ': ' . $xmldb_table->getName() . '. ' . + $this->str['field'] . ': ' . $xmldb_field->getName() . '
  • '; + /// Add to output if we have sentences + if ($sqlarr) { + $s.= '' . str_replace("\n", '
    ', implode('
    ', $sqlarr)) . '

    '; + } + } + $r.= '
'; + /// Add the SQL statements (all together) + $r.= '
' . $s; + } else { + $r.= '

' . $this->str['nowrongintsfound'] . '

'; + } + $r.= '
'; + /// Add the complete log message + $r.= '

' . $this->str['completelogbelow'] . '

'; + $r.= '
'; + + $this->output = $b . $r . $o; + } + + /// Launch postaction if exists (leave this here!) + if ($this->getPostAction() && $result) { + return $this->launch($this->getPostAction()); + } + + /// Return ok if arrived here + return $result; + } +} +?> diff --git a/admin/xmldb/actions/main_view/main_view.class.php b/admin/xmldb/actions/main_view/main_view.class.php index 673cb885a0..982ac68fc6 100644 --- a/admin/xmldb/actions/main_view/main_view.class.php +++ b/admin/xmldb/actions/main_view/main_view.class.php @@ -48,7 +48,8 @@ class main_view extends XMLDBAction { 'reservedwords' => 'xmldb', 'test' => 'xmldb', 'gotolastused' => 'xmldb', - 'checkindexes' => 'xmldb' + 'checkindexes' => 'xmldb', + 'checkbigints' => 'xmldb' )); } @@ -87,6 +88,10 @@ class main_view extends XMLDBAction { $b .= ' [' . $this->str['test'] . ']'; /// The check indexes button $b .= ' [' . $this->str['checkindexes'] . ']'; + /// The check bigints button (only for MySQL and PostgreSQL) MDL-11038a + if ($CFG->dbfamily == 'mysql' || $CFG->dbfamily == 'postgres') { + $b .= ' [' . $this->str['checkbigints'] . ']'; + } $b .= '

'; /// Send buttons to output $o .= $b;