summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--classes/Managed_DataObject.php4
-rw-r--r--lib/schema.php70
2 files changed, 72 insertions, 2 deletions
diff --git a/classes/Managed_DataObject.php b/classes/Managed_DataObject.php
index 3bc99c2eb..31aa7c359 100644
--- a/classes/Managed_DataObject.php
+++ b/classes/Managed_DataObject.php
@@ -21,9 +21,9 @@
* Wrapper for Memcached_DataObject which knows its own schema definition.
* Builds its own damn settings from a schema definition.
*
- * @author brion
+ * @author Brion Vibber <brion@status.net>
*/
-class Managed_DataObject extends Memcached_DataObject
+abstract class Managed_DataObject extends Memcached_DataObject
{
/**
* The One True Thingy that must be defined and declared.
diff --git a/lib/schema.php b/lib/schema.php
index e5def514e..9ebef8aa2 100644
--- a/lib/schema.php
+++ b/lib/schema.php
@@ -41,6 +41,7 @@ if (!defined('STATUSNET')) {
* @category Database
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
+ * @author Brion Vibber <brion@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
@@ -491,6 +492,75 @@ class Schema
return $sql;
}
+
+ /**
+ * Convert an old-style set of ColumnDef objects into the current
+ * Drupal-style schema definition array, for backwards compatibility
+ * with plugins written for 0.9.x.
+ *
+ * @param string $tableName
+ * @param array $defs
+ * @return array
+ */
+ function oldToNew($tableName, $defs)
+ {
+ $table = array();
+ $prefixes = array(
+ 'tiny',
+ 'small',
+ 'medium',
+ 'big',
+ );
+ foreach ($defs as $cd) {
+ $cd->addToTableDef($table);
+ $column = array();
+ $column['type'] = $cd->type;
+ foreach ($prefixes as $prefix) {
+ if (substr($cd->type, 0, strlen($prefix)) == $prefix) {
+ $column['type'] = substr($cd->type, strlen($prefix));
+ $column['size'] = $prefix;
+ break;
+ }
+ }
+
+ if ($cd->size) {
+ if ($cd->type == 'varchar' || $cd->type == 'char') {
+ $column['length'] = $cd->size;
+ }
+ }
+ if (!$cd->nullable) {
+ $column['not null'] = true;
+ }
+ if ($cd->autoincrement) {
+ $column['type'] = 'serial';
+ }
+ if ($cd->default) {
+ $column['default'] = $cd->default;
+ }
+ $table['fields'][$cd->name] = $column;
+
+ if ($cd->key == 'PRI') {
+ // If multiple columns are defined as primary key,
+ // we'll pile them on in sequence.
+ if (!isset($table['primary key'])) {
+ $table['primary key'] = array();
+ }
+ $table['primary key'][] = $cd->name;
+ } else if ($cd->key == 'MUL') {
+ // Individual multiple-value indexes are only per-column
+ // using the old ColumnDef syntax.
+ $idx = "{$tableName}_{$cd->name}_idx";
+ $table['indexes'][$idx] = array($cd->name);
+ } else if ($cd->key == 'UNI') {
+ // Individual unique-value indexes are only per-column
+ // using the old ColumnDef syntax.
+ $idx = "{$tableName}_{$cd->name}_idx";
+ $table['unique keys'][$idx] = array($cd->name);
+ }
+ }
+
+ return $table;
+ }
}
class SchemaTableMissingException extends Exception