You are here

function subscriptions_update_3 in Subscriptions 5.2

Same name and namespace in other branches
  1. 6 subscriptions.install \subscriptions_update_3()

Database update function 3 for 5.x-2.0 rewrite.

File

./subscriptions.install, line 234

Code

function subscriptions_update_3() {
  $ret = array();
  $t = get_t();
  if (db_table_exists('subscriptions_holding')) {
    db_query("DROP TABLE {subscriptions_holding}");

    // old left-over from 5.x-1.x
  }
  if (db_table_exists('subscriptions_sent')) {

    // old left-over from first
    $ret[] = update_sql("DROP TABLE {subscriptions_sent}");

    // incarnation of 5.x-2.0
  }
  if (db_table_exists('subscriptions_old')) {
    db_query("DROP TABLE {subscriptions_old}");
  }
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql('RENAME TABLE {subscriptions} TO {subscriptions_old}');
      $ret[] = update_sql("CREATE TABLE {subscriptions} (\n          sid int unsigned NOT NULL auto_increment,\n          module varchar(64) default NULL,\n          field varchar(32) default NULL,\n          value varchar(237) default NULL,\n          recipient_uid int NOT NULL,\n          send_interval int default NULL,\n          author_uid int NOT NULL,\n          send_updates tinyint NOT NULL default 0,\n          send_comments tinyint NOT NULL default 0,\n          PRIMARY KEY (sid),\n          KEY (module, field, value),\n          KEY (recipient_uid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;
    case 'pgsql':
      $ret[] = 'ALTER TABLE {subscriptions} RENAME TO {subscriptions_old}';
      $ret[] = update_sql("CREATE TABLE {subscriptions} (\n          sid serial,\n          module varchar(255) default NULL,\n          field varchar(255) default NULL,\n          value varchar(255) default NULL,\n          recipient_uid integer NOT NULL,\n          send_interval integer default NULL,\n          author_uid integer NOT NULL,\n          send_updates smallint NOT NULL default 0,\n          send_comments smallint NOT NULL default 0,\n          PRIMARY KEY (sid)\n        )");
      $ret[] = update_sql("CREATE INDEX {subscriptions}_module_field_value_index ON {subscriptions} (module, field, value)");
      $ret[] = update_sql("CREATE INDEX {subscriptions}_recipient_uid_index ON {subscriptions} (recipient_uid)");
      break;
  }
  $ret[] = update_sql("INSERT INTO {subscriptions}\n      (module, field, value, recipient_uid, send_interval, author_uid, send_updates, send_comments)\n      SELECT 'node', 'nid', sid, uid, send_interval, -1, 0, 1\n        FROM {subscriptions_old}\n        WHERE stype = 'node'");
  $ret[] = update_sql("INSERT INTO {subscriptions}\n      (module, field, value, recipient_uid, send_interval, author_uid, send_updates, send_comments)\n      SELECT 'node', 'tid', sid, uid, send_interval, -1, 0, 1\n        FROM {subscriptions_old}\n        WHERE stype = 'taxa'");
  $ret[] = update_sql("INSERT INTO {subscriptions}\n      (module, field, value, recipient_uid, send_interval, author_uid, send_updates, send_comments)\n      SELECT 'node', 'type', SUBSTRING(stype FROM 5), uid, send_interval, -1, 0, 1\n        FROM {subscriptions_old}\n        WHERE stype LIKE 'type%'");
  $ret[] = update_sql("UPDATE {subscriptions} SET send_interval = 1 WHERE send_interval < 1");
  $ret[] = db_query("DROP TABLE {subscriptions_old}");
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      $ret[] = update_sql("CREATE TABLE {subscriptions_queue} (\n          sqid int unsigned NOT NULL auto_increment,\n          uid int default NULL,\n          name varchar(60) default NULL,\n          mail varchar(64) default NULL,\n          language varchar(12) default NULL,\n          module varchar(64) default NULL,\n          field varchar(32) default NULL,\n          value varchar(237) default NULL,\n          author_uid int default NULL,\n          send_interval int default NULL,\n          digest tinyint default NULL,\n          load_args varchar(255) NOT NULL default '',\n          load_function varchar(60) NOT NULL default '',\n          last_sent int unsigned NOT NULL default '0',\n          PRIMARY KEY (sqid),\n          KEY (load_args, load_function, uid)\n         ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      $ret[] = update_sql("CREATE TABLE {subscriptions_user} (\n          uid integer NOT NULL,\n          digest tinyint NOT NULL default 0,\n          last_sent int unsigned NOT NULL default 0,\n          PRIMARY KEY (uid)\n        ) /*!40100 DEFAULT CHARACTER SET utf8 */");
      break;
    case 'pgsql':
      $ret[] = update_sql("CREATE TABLE {subscriptions_queue} (\n          sqid serial,\n          uid integer default NULL,\n          name varchar(60) default NULL,\n          mail varchar(64) default NULL,\n          language varchar(12) default NULL,\n          module varchar(64) default NULL,\n          field varchar(32) default NULL,\n          value varchar(237) default NULL,\n          author_uid integer default NULL,\n          send_interval integer default NULL,\n          digest smallint default NULL,\n          load_args varchar(255) NOT NULL default '',\n          load_function varchar(60) NOT NULL default '',\n          last_sent int_unsigned NOT NULL default 0,\n          PRIMARY KEY (sqid)\n        )");
      $ret[] = update_sql("CREATE INDEX {subscriptions_queue}_load_args_load_function_uid_index ON {subscriptions_queue} (load_args, load_function, uid)");
      $ret[] = update_sql("CREATE TABLE {subscriptions_user} (\n          uid integer NOT NULL,\n          digest smallint NOT NULL default 0,\n          last_sent int_unsigned NOT NULL default 0,\n          PRIMARY KEY (uid)\n        )");
      break;
  }
  $ret[] = update_sql("INSERT INTO {subscriptions_user} (uid) SELECT uid FROM {users} WHERE uid > 0");
  include_once drupal_get_path('module', 'subscriptions') . '/subscriptions.install.inc';
  _subscriptions_install_information();
  return $ret;
}