Skip to content

Instantly share code, notes, and snippets.

@laradevitt
Created September 27, 2015 19:08
Show Gist options
  • Save laradevitt/b3b9f04116b65572e684 to your computer and use it in GitHub Desktop.
Save laradevitt/b3b9f04116b65572e684 to your computer and use it in GitHub Desktop.
(Drupal 7) Create a table in a database other than the default during module installation. Database must already be specified in settings.php. Original source: http://drupal.stackexchange.com/a/51673/28700
<?php
/**
* @file
* Install, update and uninstall functions for the d7module module.
*/
function d7module_schema_shared() {
$schema['mytable'] = array(
'description' => 'My table description.',
'fields' => array(
'myfield' => array(
'description' => 'My field description.',
'type' => 'serial',
'size' => 'medium',
'not null' => TRUE,
'unsigned' => TRUE,
),
),
'primary key' => array('myfield'),
);
return $schema;
}
/**
* Implements hook_install().
*/
function d7module_install() {
db_set_active('otherdb');
$schema = d7module_schema_shared();
foreach ($schema as $name => $table) {
db_create_table($name, $table);
}
db_set_active();
}
/**
* Implements hook_uninstall().
*/
function d7module_uninstall() {
db_set_active('otherdb');
$schema = d7module_schema_shared();
foreach ($schema as $name => $table) {
db_drop_table($name);
}
db_set_active();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment