<?php

/**
 * @file
 * Install file for the Domain Source module
 */

/**
 * Implements hook_schema().
 */
function domain_source_schema() {
  $schema['domain_source'] = array(
    'description' => 'Stores the canonical domain for each node.',
    'fields' => array(
      'nid' => array('type' => 'int', 'not null' => TRUE, 'default' => 0),
      'domain_id' => array('type' => 'int', 'not null' => TRUE, 'default' => 0)),
    'primary key' => array('nid'),
    'foreign_keys' => array(
      'nid' => array('node' => 'nid'),
      'domain_id' => array('domain' => 'domain_id'),
    ),
  );
  return $schema;
}

/**
 * Implements hook_dependencies().
 */
function domain_source_update_dependencies() {
  $dependencies['domain_source'][7300] = array(
    'domain' => 7303,
  );
  return $dependencies;
}

/**
 * Remove references to row 0.
 */
function domain_source_update_7300(&$sandbox) {
  $default_id = db_query("SELECT domain_id FROM {domain} WHERE is_default = 1")->fetchField();
  db_update('domain_source')
    ->fields(array('domain_id' => $default_id))
    ->condition('domain_id', 0)
    ->execute();
  return t('Domain Source zero records removed.');
}
