install.php

  1. drupal
    1. 4.7 developer/hooks/install.php
    2. 5 install.php
    3. 5 developer/hooks/install.php
    4. 6 install.php
    5. 6 developer/hooks/install.php
    6. 7 install.php
    7. 8 core/install.php

Documentation for the update system.

The update system is used by modules to provide database updates which are run with update.php.

Functions & methods

NameDescription
hook_installInstall the current version of the database schema.
hook_update_NPerform a single update. For each patch which requires a database change add a new hook_update_N() which will be called by update.php.

File

developer/hooks/install.php
View source
  1. <?php
  2. /**
  3. * @file
  4. * Documentation for the update system.
  5. *
  6. * The update system is used by modules to provide database updates which are
  7. * run with update.php.
  8. */
  9. /**
  10. * @addtogroup hooks
  11. * @{
  12. */
  13. /**
  14. * Install the current version of the database schema.
  15. *
  16. * The hook will be called the first time a module is installed, and the
  17. * module's schema version will be set to the module's greatest numbered update
  18. * hook. Because of this, anytime a hook_update_N() is added to the module, this
  19. * function needs to be updated to reflect the current version of the database
  20. * schema.
  21. *
  22. * Table names in the CREATE queries should be wrapped with curly braces so that
  23. * they're prefixed correctly, see db_prefix_tables() for more on this.
  24. *
  25. * Note that since this function is called from a full bootstrap, all functions
  26. * (including those in modules enabled by the current page request) are
  27. * available when this hook is called. Use cases could be displaying a user
  28. * message, or calling a module function necessary for initial setup, etc.
  29. *
  30. * Implementations of this hook should be placed in a mymodule.install file in
  31. * the same directory as mymodule.module.
  32. */
  33. function hook_install() {
  34. switch ($GLOBALS['db_type']) {
  35. case 'mysql':
  36. case 'mysqli':
  37. db_query("CREATE TABLE {event} (
  38. nid int(10) unsigned NOT NULL default '0',
  39. event_start int(10) unsigned NOT NULL default '0',
  40. event_end int(10) unsigned NOT NULL default '0',
  41. timezone int(10) NOT NULL default '0',
  42. PRIMARY KEY (nid),
  43. KEY event_start (event_start)
  44. ) TYPE=MyISAM /*!40100 DEFAULT CHARACTER SET utf8 */;"
  45. );
  46. break;
  47. case 'pgsql':
  48. db_query("CREATE TABLE {event} (
  49. nid int NOT NULL default '0',
  50. event_start int NOT NULL default '0',
  51. event_end int NOT NULL default '0',
  52. timezone int NOT NULL default '0',
  53. PRIMARY KEY (nid)
  54. );"
  55. );
  56. break;
  57. }
  58. }
  59. /**
  60. * Perform a single update. For each patch which requires a database change add
  61. * a new hook_update_N() which will be called by update.php.
  62. *
  63. * The database updates are numbered sequentially starting with 1 in each
  64. * module. The first is mymodule_update_1().
  65. *
  66. * A good rule of thumb is to remove updates older than two major releases of
  67. * Drupal. Never renumber update functions.
  68. *
  69. * Whenever possible implement both PostgreSQL and MySQL at the same time. If
  70. * PostgreSQL updates are added later, add a new update function which only does
  71. * the PostgreSQL update. Be sure to use comments to describe which updates are
  72. * the same if they do get separated.
  73. *
  74. * Implementations of this hook should be placed in a mymodule.install file in
  75. * the same directory as mymodule.module. Drupal core's updates are implemented
  76. * using the system module as a name and stored in database/updates.inc.
  77. *
  78. * The following examples serve as a quick guide to MySQL to PostgreSQL conversion.
  79. * Usually (but not always!) you will use following SQL statements:
  80. *
  81. * - Adding a key (an index)
  82. * - MySQL: ALTER TABLE {$table} ADD KEY $column ($column)
  83. * - PostgreSQL: CREATE INDEX {$table}_$column_idx ON {$table}($column) // Please note the _idx "extension"
  84. * - Adding a primary key
  85. * - MySQL: ALTER TABLE {$table} ADD PRIMARY KEY $column ($column)
  86. * - PostgreSQL: ALTER TABLE {$table} ADD PRIMARY KEY ($column)
  87. * - Dropping a primary key
  88. * - MySQL: ALTER TABLE {$table} DROP PRIMARY KEY
  89. * - PostgreSQL:ALTER TABLE {$table} DROP CONSTRAINT {$table}_pkey
  90. * - Dropping a column
  91. * - MySQL: ALTER TABLE {$table} DROP $column
  92. * - Postgres: ALTER TABLE {$table} DROP $column
  93. * - Dropping an index
  94. * - MySQL: ALTER TABLE {$table} DROP INDEX $index
  95. * - Postgres:
  96. * - DROP INDEX {$table}_$column_idx // When index was defined by CREATE INDEX
  97. * - ALTER TABLE {$table} DROP CONSTRAINT {$table}_$column_key // In case of UNIQUE($column)
  98. * - Adding a column
  99. * - MySQL: $ret = update_sql("ALTER TABLE {vocabulary} ADD tags tinyint(3) unsigned default '0' NOT NULL");
  100. * - Postgres: db_add_column($ret, 'vocabulary', 'tags', 'smallint', array('default' => 0, 'not null' => TRUE));
  101. * - Changing a column
  102. * - MySQL: $ret[] = update_sql("ALTER TABLE {locales_source} CHANGE location location varchar(255) NOT NULL default ''");
  103. * - Postgres: db_change_column($ret, 'locales_source', 'location', 'location', 'varchar(255)', array('not null' => TRUE, 'default' => "''"));
  104. */
  105. function hook_update_N() {
  106. $ret = array();
  107. switch ($GLOBALS['db_type']) {
  108. case 'pgsql':
  109. db_add_column($ret, 'contact', 'weight', 'smallint', array('not null' => TRUE, 'default' => 0));
  110. db_add_column($ret, 'contact', 'selected', 'smallint', array('not null' => TRUE, 'default' => 0));
  111. break;
  112. case 'mysql':
  113. case 'mysqli':
  114. $ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN weight tinyint(3) NOT NULL DEFAULT 0");
  115. $ret[] = update_sql("ALTER TABLE {contact} ADD COLUMN selected tinyint(1) NOT NULL DEFAULT 0");
  116. break;
  117. }
  118. return $ret;
  119. }
  120. /**
  121. * @} End of "addtogroup hooks".
  122. */
Login or register to post comments