Commit 2598d31e authored by Gábor Hojtsy's avatar Gábor Hojtsy

#150210 by bjaspan and Frando: explicit table names for create table...

#150210 by bjaspan and Frando: explicit table names for create table statements and fixing a schema referencing bug
parent 69a9f5f7
......@@ -3300,8 +3300,8 @@ function drupal_install_schema($module) {
_drupal_initialize_schema($module, $schema);
$ret = array();
foreach ($schema as $table) {
db_create_table($ret, $table);
foreach ($schema as $name => $table) {
db_create_table($ret, $name, $table);
}
}
......
......@@ -401,11 +401,13 @@ function db_escape_table($string) {
*
* @param $ret
* Array to which query results will be added.
* @param $name
* The name of the table to create.
* @param $table
* A valid and processed table schema definition array.
* A Schema API table definition array.
*/
function db_create_table(&$ret, $table) {
$statements = db_create_table_sql($table);
function db_create_table(&$ret, $name, $table) {
$statements = db_create_table_sql($name, $table);
foreach ($statements as $statement) {
$ret[] = update_sql($statement);
}
......
......@@ -49,24 +49,26 @@ function db_query($query) {
}
/**
* Generate SQL to create a new table from a Drupal schema definition.
* Generate SQL to create a new table from a Drupal schema definition.
*
* @param $name
* The name of the table to create.
* @param $table
* A valid Drupal table definition array.
* A Schema API table definition array.
* @return
* An array of SQL statements to create the table.
*/
function db_create_table_sql($table) {
function db_create_table_sql($name, $table) {
if (empty($table['mysql_suffix'])) {
$table['mysql_suffix'] = "/*!40100 DEFAULT CHARACTER SET UTF8 */";
}
$sql = "CREATE TABLE {". $table['name'] ."} (\n";
$sql = "CREATE TABLE {". $name ."} (\n";
// Add the SQL statement for each field.
foreach ($table['fields'] as $name => $field) {
$sql .= _db_create_field_sql($name, _db_process_field($field)) .", \n";
foreach ($table['fields'] as $field_name => $field) {
$sql .= _db_create_field_sql($field_name, _db_process_field($field)) .", \n";
}
// Process keys & indexes.
......
......@@ -513,17 +513,19 @@ function db_type_map() {
}
/**
* Generate SQL to create a new table from a Drupal schema definition.
* Generate SQL to create a new table from a Drupal schema definition.
*
* @param $name
* The name of the table to create.
* @param $table
* A valid Drupal table definition array.
* A Schema API table definition array.
* @return
* An array of SQL statements to create the table.
*/
function db_create_table_sql($table) {
function db_create_table_sql($name, $table) {
$sql_fields = array();
foreach ($table['fields'] as $name => $field) {
$sql_fields[] = _db_create_field_sql($name, _db_process_field($field));
foreach ($table['fields'] as $field_name => $field) {
$sql_fields[] = _db_create_field_sql($field_name, _db_process_field($field));
}
$sql_keys = array();
......@@ -531,12 +533,12 @@ function db_create_table_sql($table) {
$sql_keys[] = 'PRIMARY KEY ('. implode(', ', $table['primary key']) .')';
}
if (isset($table['unique keys']) && is_array($table['unique keys'])) {
foreach ($table['unique keys'] as $keyname => $key) {
$sql_keys[] = 'CONSTRAINT {'. $table['name'] .'}_'. $keyname .'_key UNIQUE ('. implode(', ', $key) .')';
foreach ($table['unique keys'] as $key_name => $key) {
$sql_keys[] = 'CONSTRAINT {'. $name .'}_'. $key_name .'_key UNIQUE ('. implode(', ', $key) .')';
}
}
$sql = "CREATE TABLE {". $table['name'] ."} (\n\t";
$sql = "CREATE TABLE {". $name ."} (\n\t";
$sql .= implode(",\n\t", $sql_fields);
if (count($sql_keys) > 0) {
$sql .= ",\n\t";
......@@ -546,8 +548,8 @@ function db_create_table_sql($table) {
$statements[] = $sql;
if (isset($table['indexes']) && is_array($table['indexes'])) {
foreach ($table['indexes'] as $keyname => $key) {
$statements[] = _db_create_index_sql($table['name'], $keyname, $key);
foreach ($table['indexes'] as $key_name => $key) {
$statements[] = _db_create_index_sql($name, $key_name, $key);
}
}
......
......@@ -3288,12 +3288,71 @@ function system_update_6019() {
function system_update_6020() {
$ret = array();
$schema['menu_router'] = drupal_get_schema_unprocessed('system', 'menu_router');
$schema['menu_links'] = drupal_get_schema_unprocessed('system', 'menu_links');
_drupal_initialize_schema('system', $schema);
$ret = array();
foreach ($schema as $table) {
db_create_table($ret, $table);
$schema['menu_router'] = array(
'fields' => array(
'path' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'load_functions' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'to_arg_functions' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'access_callback' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'access_arguments' => array('type' => 'text', 'not null' => FALSE),
'page_callback' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'page_arguments' => array('type' => 'text', 'not null' => FALSE),
'fit' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
'number_parts' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
'tab_parent' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'tab_root' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'title' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'title_callback' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'title_arguments' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'type' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
'block_callback' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'description' => array('type' => 'text', 'not null' => TRUE),
'position' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
'file' => array('type' => 'text', 'size' => 'medium')
),
'indexes' => array(
'fit' => array('fit'),
'tab_parent' => array('tab_parent')
),
'primary key' => array('path'),
);
$schema['menu_links'] = array(
'fields' => array(
'menu_name' => array('type' => 'varchar', 'length' => 64, 'not null' => TRUE, 'default' => ''),
'mlid' => array('type' => 'serial', 'unsigned' => TRUE, 'not null' => TRUE),
'plid' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'link_path' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'router_path' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'hidden' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
'external' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
'has_children' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
'expanded' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
'weight' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
'depth' => array('type' => 'int', 'not null' => TRUE, 'default' => 0, 'size' => 'small'),
'p1' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'p2' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'p3' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'p4' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'p5' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'p6' => array('type' => 'int', 'unsigned' => TRUE, 'not null' => TRUE, 'default' => 0),
'module' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => 'system'),
'link_title' => array('type' => 'varchar', 'length' => 255, 'not null' => TRUE, 'default' => ''),
'options' => array('type' => 'text', 'not null' => FALSE)
),
'indexes' => array(
'expanded_children' => array('expanded', 'has_children'),
'menu_name_path' => array('menu_name', 'link_path'),
'plid'=> array('plid'),
'parents' => array('p1', 'p2', 'p3', 'p4', 'p5'),
'router_path' => array('router_path'),
),
'primary key' => array('mlid'),
);
foreach ($schema as $name => $table) {
db_create_table($ret, $name, $table);
}
return $ret;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment