blocks MDL-24374 upgrading code to change block_search_documents table.docdate and .updated to be integer type instead of datetime

This commit is contained in:
Aparup Banerjee 2010-10-18 08:33:13 +00:00
parent d490ca8161
commit d997cc88cb
4 changed files with 133 additions and 5 deletions

View file

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<XMLDB PATH="blocks/search/db" VERSION="20070811" COMMENT="XMLDB file for Moodle search engine" <XMLDB PATH="blocks/search/db" VERSION="20101012" COMMENT="XMLDB file for Moodle search engine"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd" xsi:noNamespaceSchemaLocation="../../../lib/xmldb/xmldb.xsd"
> >
@ -12,8 +12,8 @@
<FIELD NAME="itemtype" TYPE="char" LENGTH="32" NOTNULL="true" DEFAULT="standard" SEQUENCE="false" PREVIOUS="doctype" NEXT="title"/> <FIELD NAME="itemtype" TYPE="char" LENGTH="32" NOTNULL="true" DEFAULT="standard" SEQUENCE="false" PREVIOUS="doctype" NEXT="title"/>
<FIELD NAME="title" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="itemtype" NEXT="url"/> <FIELD NAME="title" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="itemtype" NEXT="url"/>
<FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="title" NEXT="docdate"/> <FIELD NAME="url" TYPE="char" LENGTH="255" NOTNULL="true" SEQUENCE="false" PREVIOUS="title" NEXT="docdate"/>
<FIELD NAME="docdate" TYPE="datetime" NOTNULL="true" SEQUENCE="false" PREVIOUS="url" NEXT="updated"/> <FIELD NAME="docdate" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="url" NEXT="updated"/>
<FIELD NAME="updated" TYPE="datetime" NOTNULL="true" SEQUENCE="false" PREVIOUS="docdate" NEXT="courseid"/> <FIELD NAME="updated" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="docdate" NEXT="courseid"/>
<FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="updated" NEXT="groupid"/> <FIELD NAME="courseid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="updated" NEXT="groupid"/>
<FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="courseid"/> <FIELD NAME="groupid" TYPE="int" LENGTH="10" NOTNULL="true" UNSIGNED="true" DEFAULT="0" SEQUENCE="false" PREVIOUS="courseid"/>
</FIELDS> </FIELDS>
@ -27,4 +27,4 @@
</INDEXES> </INDEXES>
</TABLE> </TABLE>
</TABLES> </TABLES>
</XMLDB> </XMLDB>

View file

@ -0,0 +1,82 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Keeps track of upgrades to the global search block
*
* @package blocks
* @subpackage search
* @copyright 2010 Aparup Banerjee <aparup@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
function xmldb_block_search_upgrade($oldversion) {
global $CFG, $DB;
require('upgradelib.php');
$result = TRUE;
$dbman = $DB->get_manager();
if ($oldversion < 2010101800) {
// See MDL-24374
// Changing type of field docdate on table block_search_documents to int
// Changing type of field updated on table block_search_documents to int
$table = new xmldb_table('block_search_documents');
$field_docdate_new = new xmldb_field('docdate_new', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'docdate');
$field_updated_new = new xmldb_field('updated_new', XMLDB_TYPE_INTEGER, '10', XMLDB_UNSIGNED, XMLDB_NOTNULL, null, '0', 'updated');
$field_docdate_old = new xmldb_field('docdate');
$field_updated_old = new xmldb_field('updated');
// Conditionally launch add temporary fields
if (!$dbman->field_exists($table, $field_docdate_new)) {
$dbman->add_field($table, $field_docdate_new);
}
if (!$dbman->field_exists($table, $field_updated_new)) {
$dbman->add_field($table, $field_updated_new);
}
$sql = "SELECT id, docdate, updated FROM {block_search_documents}";
$search_documents = $DB->get_records_sql($sql);
if ($search_documents) {
foreach ($search_documents as $sd) {
$sd->docdate_new = convert_datetime_upgrade($sd->docdate);
$sd->updated_new = convert_datetime_upgrade($sd->updated);
$DB->update_record('block_search_documents', $sd);
}
}
// Conditionally launch drop the old fields
if ($dbman->field_exists($table, $field_docdate_old)) {
$dbman->drop_field($table, $field_docdate_old);
}
if ($dbman->field_exists($table, $field_updated_old)) {
$dbman->drop_field($table, $field_updated_old);
}
//rename the new fields to the original field names.
$dbman->rename_field($table, $field_docdate_new, 'docdate');
$dbman->rename_field($table, $field_updated_new, 'updated');
// search savepoint reached
upgrade_block_savepoint(true, 2010101800, 'search');
}
return $result;
}

View file

@ -0,0 +1,46 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Global search block upgrade related helper functions
*
* @package blocks
* @subpackage search
* @copyright 2010 Aparup Banerjee
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
/*
* Function to turn a mysql(datetime) or postgres(timestamp without timezone data) or any generic date string (YYYY-MM-DD HH:MM:SS)
* read in from a database's date/time field (ie:valid) into a unix timestamp
* @param str The string to be converted to timestamp
* @return timestamp or 0
*/
function convert_datetime_upgrade($str) {
$timestamp = strtotime($str);
//process different failure returns due to different php versions
if ($timestamp === false || $timestamp < 1) {
return 0;
} else {
return $timestamp;
}
}

View file

@ -15,5 +15,5 @@
// You should have received a copy of the GNU General Public License // You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>. // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
$plugin->version = 2008031500; $plugin->version = 2010101800;
$plugin->cron = 1; $plugin->cron = 1;