class ForeignKeys < ActiveRecord::Migration
  def up
    execute "ALTER TABLE accounts ADD CONSTRAINT fk_accounts_on_user_id FOREIGN KEY index_accounts_on_user_id (user_id) REFERENCES users (id);"
    execute "ALTER TABLE bookmarks ADD CONSTRAINT fk_bookmarks_on_owner_id FOREIGN KEY index_bookmarks_on_owner_id (owner_id) REFERENCES users (id);"
    execute "ALTER TABLE comments MODIFY COLUMN node_id int(11) NOT NULL;"
    execute "ALTER TABLE comments ADD CONSTRAINT fk_comments_on_node_id FOREIGN KEY index_comments_on_node_id (node_id) REFERENCES nodes (id);"
    execute "ALTER TABLE comments ADD CONSTRAINT fk_comments_on_user_id FOREIGN KEY index_comments_on_user_id (user_id) REFERENCES users (id);"
    execute "ALTER TABLE diaries ADD CONSTRAINT fk_diaries_on_owner_id FOREIGN KEY index_diaries_on_owner_id (owner_id) REFERENCES users (id);"
    execute "ALTER TABLE diaries ADD CONSTRAINT fk_diaries_on_converted_news_id FOREIGN KEY index_diaries_on_converted_news_id (converted_news_id) REFERENCES news (id);"
    execute "ALTER TABLE links ADD CONSTRAINT fk_links_on_news_id FOREIGN KEY index_links_on_news_id (news_id) REFERENCES news (id);"
    execute "ALTER TABLE logs MODIFY COLUMN account_id int(11) NOT NULL;"
    execute "ALTER TABLE logs ADD CONSTRAINT fk_logs_on_account_id FOREIGN KEY index_logs_on_account_id (account_id) REFERENCES accounts (id);"
    execute "ALTER TABLE logs ADD CONSTRAINT fk_logs_on_user_id FOREIGN KEY index_logs_on_user_id (user_id) REFERENCES users (id);"
    execute "ALTER TABLE news MODIFY COLUMN section_id int(11) NOT NULL;"
    execute "ALTER TABLE news ADD CONSTRAINT fk_news_on_moderator_id FOREIGN KEY index_news_on_moderator_id (moderator_id) REFERENCES users (id);"
    execute "ALTER TABLE news ADD CONSTRAINT fk_news_on_section_id FOREIGN KEY index_news_on_section_id (section_id) REFERENCES sections (id);"
    execute "ALTER TABLE news_versions MODIFY COLUMN news_id int(11) NOT NULL;"
    execute "ALTER TABLE news_versions ADD CONSTRAINT fk_news_versions_on_news_id FOREIGN KEY index_news_versions_on_news_id (news_id) REFERENCES news (id);"
    execute "ALTER TABLE news_versions ADD CONSTRAINT fk_news_versions_on_user_id FOREIGN KEY index_news_versions_on_user_id (user_id) REFERENCES users (id);"
    execute "ALTER TABLE nodes ADD CONSTRAINT fk_nodes_on_user_id FOREIGN KEY index_nodes_on_user_id (user_id) REFERENCES users (id);"
    execute "ALTER TABLE oauth_access_grants MODIFY COLUMN application_id int(11) NOT NULL;"
    execute "ALTER TABLE oauth_access_grants ADD CONSTRAINT fk_oauth_access_grants_on_resource_owner_id FOREIGN KEY index_oauth_access_grants_on_resource_owner_id (resource_owner_id) REFERENCES accounts (id);"
    execute "ALTER TABLE oauth_access_grants ADD CONSTRAINT fk_oauth_access_grants_on_application_id FOREIGN KEY index_oauth_access_grants_on_application_id (application_id) REFERENCES oauth_applications (id);"
    execute "ALTER TABLE oauth_access_tokens MODIFY COLUMN application_id int(11) NOT NULL;"
    execute "ALTER TABLE oauth_access_tokens ADD CONSTRAINT fk_oauth_access_tokens_on_resource_owner_id FOREIGN KEY index_oauth_access_tokens_on_resource_owner_id (resource_owner_id) REFERENCES accounts (id);"
    execute "ALTER TABLE oauth_access_tokens ADD CONSTRAINT fk_oauth_access_tokens_on_application_id FOREIGN KEY index_oauth_access_tokens_on_application_id (application_id) REFERENCES oauth_applications (id);"
    execute "ALTER TABLE oauth_applications MODIFY COLUMN owner_id int(11) NOT NULL;"
    execute "ALTER TABLE oauth_applications ADD CONSTRAINT fk_oauth_applications_on_owner_id FOREIGN KEY index_oauth_applications_on_owner_id (owner_id) REFERENCES accounts (id);"
    execute "ALTER TABLE paragraphs ADD CONSTRAINT fk_paragraphs_on_news_id FOREIGN KEY index_paragraphs_on_news_id (news_id) REFERENCES news (id);"
    execute "ALTER TABLE poll_answers MODIFY COLUMN poll_id int(11) NOT NULL;"
    execute "ALTER TABLE poll_answers ADD CONSTRAINT fk_poll_answers_on_poll_id FOREIGN KEY index_poll_answers_on_poll_id (poll_id) REFERENCES polls (id);"
    execute "ALTER TABLE posts MODIFY COLUMN forum_id int(11) NOT NULL;"
    execute "ALTER TABLE posts ADD CONSTRAINT fk_posts_on_forum_id FOREIGN KEY index_posts_on_forum_id (forum_id) REFERENCES forums (id);"
    execute "ALTER TABLE taggings MODIFY COLUMN tag_id int(11) NOT NULL;"
    execute "ALTER TABLE taggings MODIFY COLUMN node_id int(11) NOT NULL;"
    execute "ALTER TABLE taggings ADD CONSTRAINT fk_taggings_on_tag_id FOREIGN KEY index_taggings_on_tag_id (tag_id) REFERENCES tags (id);"
    execute "ALTER TABLE taggings ADD CONSTRAINT fk_taggings_on_node_id FOREIGN KEY index_taggings_on_node_id (node_id) REFERENCES nodes (id);"
    execute "ALTER TABLE taggings ADD CONSTRAINT fk_taggings_on_user_id FOREIGN KEY index_taggings_on_user_id (user_id) REFERENCES users (id);"
    execute "ALTER TABLE trackers MODIFY COLUMN category_id int(11) NOT NULL;"
    execute "ALTER TABLE trackers ADD CONSTRAINT fk_trackers_on_category_id FOREIGN KEY index_trackers_on_category_id (category_id) REFERENCES categories (id);"
    execute "ALTER TABLE trackers ADD CONSTRAINT fk_trackers_on_assigned_to_user_id FOREIGN KEY index_trackers_on_assigned_to_user_id (assigned_to_user_id) REFERENCES users (id);"
    execute "ALTER TABLE wiki_versions MODIFY COLUMN wiki_page_id int(11) NOT NULL;"
    execute "ALTER TABLE wiki_versions ADD CONSTRAINT fk_wiki_versions_on_wiki_page_id FOREIGN KEY index_wiki_versions_on_wiki_page_id (wiki_page_id) REFERENCES wiki_pages (id);"
    execute "ALTER TABLE wiki_versions ADD CONSTRAINT fk_wiki_versions_on_user_id FOREIGN KEY index_wiki_versions_on_user_id (user_id) REFERENCES users (id);"
  end

  def down
    execute "ALTER TABLE accounts DROP FOREIGN KEY fk_accounts_on_user_id;"
    execute "ALTER TABLE bookmarks DROP FOREIGN KEY fk_bookmarks_on_owner_id;"
    execute "ALTER TABLE comments MODIFY COLUMN node_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE comments DROP FOREIGN KEY fk_comments_on_node_id;"
    execute "ALTER TABLE comments DROP FOREIGN KEY fk_comments_on_user_id;"
    execute "ALTER TABLE diaries DROP FOREIGN KEY fk_diaries_on_owner_id;"
    execute "ALTER TABLE diaries DROP FOREIGN KEY fk_diaries_on_converted_news_id;"
    execute "ALTER TABLE links DROP FOREIGN KEY fk_links_on_news_id;"
    execute "ALTER TABLE logs MODIFY COLUMN account_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE logs DROP FOREIGN KEY fk_logs_on_account_id;"
    execute "ALTER TABLE logs DROP FOREIGN KEY fk_logs_on_user_id;"
    execute "ALTER TABLE news MODIFY COLUMN section_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE news DROP FOREIGN KEY fk_news_on_moderator_id;"
    execute "ALTER TABLE news DROP FOREIGN KEY fk_news_on_section_id;"
    execute "ALTER TABLE news_versions MODIFY COLUMN news_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE news_versions DROP FOREIGN KEY fk_news_versions_on_news_id;"
    execute "ALTER TABLE news_versions DROP FOREIGN KEY fk_news_versions_on_user_id;"
    execute "ALTER TABLE nodes DROP FOREIGN KEY fk_nodes_on_user_id;"
    execute "ALTER TABLE oauth_access_grants MODIFY COLUMN application_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE oauth_access_grants DROP FOREIGN KEY fk_oauth_access_grants_on_resource_owner_id;"
    execute "ALTER TABLE oauth_access_grants DROP FOREIGN KEY fk_oauth_access_grants_on_application_id;"
    execute "ALTER TABLE oauth_access_tokens MODIFY COLUMN application_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE oauth_access_tokens DROP FOREIGN KEY fk_oauth_access_tokens_on_resource_owner_id;"
    execute "ALTER TABLE oauth_access_tokens DROP FOREIGN KEY fk_oauth_access_tokens_on_application_id;"
    execute "ALTER TABLE oauth_applications MODIFY COLUMN owner_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE oauth_applications DROP FOREIGN KEY fk_oauth_applications_on_owner_id;"
    execute "ALTER TABLE paragraphs DROP FOREIGN KEY fk_paragraphs_on_news_id;"
    execute "ALTER TABLE poll_answers MODIFY COLUMN poll_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE poll_answers DROP FOREIGN KEY fk_poll_answers_on_poll_id;"
    execute "ALTER TABLE posts MODIFY COLUMN forum_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE posts DROP FOREIGN KEY fk_posts_on_forum_id;"
    execute "ALTER TABLE taggings MODIFY COLUMN tag_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE taggings MODIFY COLUMN node_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE taggings DROP FOREIGN KEY fk_taggings_on_tag_id;"
    execute "ALTER TABLE taggings DROP FOREIGN KEY fk_taggings_on_node_id;"
    execute "ALTER TABLE taggings DROP FOREIGN KEY fk_taggings_on_user_id;"
    execute "ALTER TABLE trackers MODIFY COLUMN category_id int(11) DEFAULT NULL;"
    execute "ALTER TABLE trackers DROP FOREIGN KEY fk_trackers_on_category_id;"
    execute "ALTER TABLE trackers DROP FOREIGN KEY fk_trackers_on_assigned_to_user_id;"
    execute "ALTER TABLE wiki_versions DROP FOREIGN KEY fk_wiki_versions_on_wiki_page_id;"
    execute "ALTER TABLE wiki_versions DROP FOREIGN KEY fk_wiki_versions_on_user_id;"
  end

  def db_name
    Rails.configuration.database_configuration[Rails.env]['database']
  end
end
