You are here

function _flashnode_migrate_from_flash in Flash Node 5.6

Same name and namespace in other branches
  1. 5.2 flashnode.install \_flashnode_migrate_from_flash()
  2. 5.3 flashnode.install \_flashnode_migrate_from_flash()

If flashnode is being installed in place of flash then to Drupal it looks like the first time this module has been installed. In reality we have an existing table we want to keep with our existing flash content, so we have to simulate the update routines as part of installation.

1 call to _flashnode_migrate_from_flash()
flashnode_install in ./flashnode.install
Implementation of hook_install().

File

./flashnode.install, line 260

Code

function _flashnode_migrate_from_flash() {

  /**
   * Do update to utf8, if required - at this stage there are no text fields to
   * convert so it will just change the default character set if required
   */
  _flashnode_update_utf8();

  /**
   * See if fid column exists, and drop it if it does. Not all versions of flash
   * included fid so it may already not exist! Not all Postgres can drop
   * columns, so omit dropping in that case
   */
  if (db_num_rows(db_query("SHOW COLUMNS FROM {%s} LIKE '%s'", 'flash', 'fid'))) {
    switch ($GLOBALS['db_type']) {
      case 'mysql':
      case 'mysqli':
        db_query('ALTER TABLE {flash} DROP fid');
        break;
      case 'pgsql':

        //db_query('ALTER TABLE {flash} DROP COLUMN fid');
        break;
    }
  }

  /**
   * Drop version and build, unless Postgres, then rename table from {flash} to
   * {flashnode} to bring table name in line with node name
   */
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query('ALTER TABLE {flash} DROP version');
      db_query('ALTER TABLE {flash} DROP build');
      db_query('RENAME TABLE {flash} TO {flashnode}');
      break;
    case 'pgsql':

      //db_query('ALTER TABLE {flash} DROP COLUMN version');

      //db_query('ALTER TABLE {flash} DROP COLUMN build');
      db_query('ALTER TABLE {flash} RENAME TO {flashnode}');
      break;
  }

  // Update filters table
  db_query("UPDATE {filters} SET module='flashnode' WHERE module='flash'");

  // Get all existing nodes that contain macros
  $result = db_query("SELECT vid, body, teaser FROM {node_revisions} WHERE body LIKE '%[flash|nid=%' OR teaser LIKE '%[flash|nid=%'");

  // Iterate through the results
  while ($node = db_fetch_object($result)) {

    // Update body and teaser
    $node->body = str_replace('[flash|nid=', '[flashnode|nid=', $node->body);
    $node->teaser = str_replace('[flash|nid=', '[flashnode|nid=', $node->teaser);

    // Update the database
    db_query("UPDATE {node_revisions} SET body='%s', teaser='%s' WHERE vid=%d", $node->body, $node->teaser, $node->vid);
  }

  // Reset the cache to ensure any pages using filters are updated
  cache_clear_all('*', 'cache_filter', true);

  /**
   * Amend the variable table by deleting default version and build which are no
   * longer required, and change the name on the remaining variables to
   * flashnode_ to make consistent with module name
   */
  db_query("UPDATE {variable} SET name='flashnode_default_path' WHERE name='flash_default_path'");
  db_query("UPDATE {variable} SET name='flashnode_updated' WHERE name='flash_updated'");
  db_query("DELETE FROM {variable} WHERE name='flash_version'");
  db_query("DELETE FROM {variable} WHERE name='flash_build'");

  // Update files table - change filename from _flash to _flashnode
  db_query("UPDATE {files} SET filename='_flashnode' WHERE filename='_flash'");

  // Update node table - change type from flash to flashnode
  db_query("UPDATE {node} SET type='flashnode' WHERE type='flash'");

  // Add substitution and base column
  switch ($GLOBALS['db_type']) {
    case 'mysql':
    case 'mysqli':
      db_query("ALTER TABLE {flashnode} ADD substitution longtext NOT NULL");
      db_query("ALTER TABLE {flashnode} ADD flashvars longtext NOT NULL");
      db_query("ALTER TABLE {flashnode} ADD base VARCHAR(255) NOT NULL");
      break;
    case 'pgsql':
      _flashnode_db_add_column($ret, 'flashnode', 'substitution', 'text', array(
        'default' => '',
        'not null' => TRUE,
      ));
      _flashnode_db_add_column($ret, 'flashnode', 'flashvars', 'text', array(
        'default' => '',
        'not null' => TRUE,
      ));
      _flashnode_db_add_column($ret, 'flashnode', 'base', 'varchar(255)', array(
        'default' => '',
        'not null' => TRUE,
      ));
      break;
  }

  // Set default value of substitution
  db_query("UPDATE {flashnode} SET substitution='!default'");

  // End of update routine
  drupal_set_message('Successfully migrated database from {flash} to {flashnode}.');
  return;
}