| Versionen | |
|---|---|
| drupal7 | protected DatabaseSchema_sqlite::alterTable(&$ret, $table, $new_schema) |
Create a table with a new schema containing the old content.
As SQLite does not support ALTER TABLE (with a few exceptions) it is necessary to create a new table and copy over the old content.
$ret Array to which query results will be added.
$table Name of the table to be altered.
$new_schema The new schema array for the table.
includes/
<?php
protected function alterTable(&$ret, $table, $new_schema) {
$i = 0;
do {
$new_table = $table . '_' . $i++;
} while ($this->tableExists($new_table));
$this->createTable($ret, $new_table, $new_schema);
$fields = implode(', ', array_keys($new_schema['fields']));
$ret[] = update_sql('INSERT INTO {' . $new_table . "} ($fields) SELECT $fields FROM {" . $table . '}');
$old_count = db_query('SELECT COUNT(*) FROM {' . $table . '}')->fetchField();
$new_count = db_query('SELECT COUNT(*) FROM {' . $new_table . '}')->fetchField();
if ($old_count == $new_count) {
do {
$temp_table = $table . '_' . $i++;
} while ($this->tableExists($temp_table));
$this->renameTable($ret, $table, $temp_table);
$this->renameTable($ret, $new_table, $table);
$this->dropTable($ret, $temp_table);
}
}
?>
Kommentare
Kommentar hinzufügen