require 'migrate'

class CleanupOsmDb < ActiveRecord::Migration
  def self.up
    change_column "current_nodes", "latitude", :double, :null => false
    change_column "current_nodes", "longitude", :double, :null => false
    change_column "current_nodes", "user_id", :bigint, :limit => 20, :null => false
    change_column "current_nodes", "visible", :boolean, :null => false
    change_column "current_nodes", "timestamp", :datetime, :null => false
    add_primary_key "current_nodes", ["id"]
    remove_index "current_nodes", :name => "current_nodes_id_idx"

    change_column "current_segments", "node_a", :bigint, :limit => 64, :null => false
    change_column "current_segments", "node_b", :bigint, :limit => 64, :null => false
    change_column "current_segments", "user_id", :bigint, :limit => 20, :null => false
    change_column "current_segments", "visible", :boolean, :null => false
    change_column "current_segments", "timestamp", :datetime, :null => false
    add_primary_key "current_segments", ["id"]
    remove_index "current_segments", :name => "current_segments_id_visible_idx"

    change_column "current_way_segments", "id", :bigint, :limit => 64, :null => false
    change_column "current_way_segments", "segment_id", :bigint, :limit => 64, :null => false
    change_column "current_way_segments", "sequence_id", :bigint, :limit => 11, :null => false
    add_primary_key "current_way_segments", ["id", "sequence_id"]
    remove_index "current_way_segments", :name => "current_way_segments_id_idx"

    change_column "current_way_tags", "id", :bigint, :limit => 64, :null => false

    change_column "current_ways", "user_id", :bigint, :limit => 20, :null => false
    change_column "current_ways", "timestamp", :datetime, :null => false
    change_column "current_ways", "visible", :boolean, :null => false
    change_engine "current_ways", "InnoDB"

    change_column "diary_entries", "title", :string, :null => false
    change_column "diary_entries", "body", :text, :null => false
    change_column "diary_entries", "created_at", :datetime, :null => false
    change_column "diary_entries", "updated_at", :datetime, :null => false

    add_index "friends", ["user_id"], :name => "friends_user_id_idx"

    remove_index "gps_points", :name => "points_uid_idx"
    remove_index "gps_points", :name => "points_idx"
    remove_column "gps_points", "user_id"
    add_index "gps_points", ["latitude", "longitude"], :name => "points_idx"
    change_column "gps_points", "trackid", :integer, :null => false
    change_column "gps_points", "latitude", :integer, :null => false
    change_column "gps_points", "longitude", :integer, :null => false
    change_column "gps_points", "gpx_id", :bigint, :limit => 64, :null => false

    change_column "gpx_file_tags", "tag", :string, :null => false

    change_column "gpx_files", "user_id", :bigint,   :limit => 20, :null => false
    change_column "gpx_files", "timestamp", :datetime, :null => false
    change_column "gpx_files", "description", :string, :default => "", :null => false
    change_column "gpx_files", "inserted", :boolean, :null => false

    drop_table "gpx_pending_files"

    remove_index "messages", :name => "from_name_idx"
    remove_column "messages", "user_id"
    remove_column "messages", "from_display_name"
    change_column "messages", "title", :string, :null => false
    change_column "messages", "body", :text, :null => false
    change_column "messages", "sent_on", :datetime, :null => false
    change_column "messages", "message_read", :boolean, :default => false, :null => false
    add_index "messages", ["to_user_id"], :name => "messages_to_user_id_idx"

    drop_table "meta_areas"

    change_column "nodes", "id", :bigint, :limit => 64, :null => false
    change_column "nodes", "latitude", :double, :null => false
    change_column "nodes", "longitude", :double, :null => false
    change_column "nodes", "user_id", :bigint, :limit => 20, :null => false
    change_column "nodes", "visible", :boolean, :null => false
    change_column "nodes", "timestamp", :datetime, :null => false
    add_index "nodes", ["timestamp"], :name => "nodes_timestamp_idx"

    change_column "segments", "id", :bigint, :limit => 64, :null => false
    change_column "segments", "node_a", :bigint, :limit => 64, :null => false
    change_column "segments", "node_b", :bigint, :limit => 64, :null => false
    change_column "segments", "user_id", :bigint, :limit => 20, :null => false
    change_column "segments", "visible", :boolean, :null => false
    change_column "segments", "timestamp", :datetime, :null => false
    add_index "segments", ["timestamp"], :name => "segments_timestamp_idx"

    remove_column "users", "preferences"
    change_column "users", "email", :string, :null => false
    change_column "users", "pass_crypt", :string, :null => false
    change_column "users", "creation_time", :datetime, :null => false
    change_column "users", "display_name", :string, :default => "", :null => false
    change_column "users", "data_public", :boolean, :default => false, :null => false
    change_column "users", "home_lat", :double, :default => nil
    change_column "users", "home_lon", :double, :default => nil
    remove_index "users", :name => "users_email_idx"
    add_index "users", ["email"], :name => "users_email_idx", :unique => true
    remove_index "users", :name => "users_display_name_idx"
    add_index "users", ["display_name"], :name => "users_display_name_idx", :unique => true

    change_column "way_segments", "segment_id", :bigint, :limit => 64, :null => false
 
    change_column "way_tags", "k", :string, :null => false
    change_column "way_tags", "v", :string, :null => false
    change_column "way_tags", "version", :bigint, :limit => 20, :null => false

    change_column "ways", "user_id", :bigint, :limit => 20, :null => false
    change_column "ways", "timestamp", :datetime, :null => false
    change_column "ways", "visible", :boolean, :default => true, :null => false
    remove_index "ways", :name => "ways_id_version_idx"
    add_index "ways", ["timestamp"], :name => "ways_timestamp_idx"
  end

  def self.down
    remove_index "ways", :name => "ways_timestamp_idx"
    add_index "ways", ["id"], :name => "ways_id_version_idx"
    change_column "ways", "visible", :boolean, :default => true
    change_column "ways", "timestamp", :datetime
    change_column "ways", "user_id", :bigint, :limit => 20

    change_column "way_tags", "version", :bigint, :limit => 20
    change_column "way_tags", "v", :string, :default => nil
    change_column "way_tags", "k", :string, :default => nil

    change_column "way_segments", "segment_id", :integer
 
    remove_index "users", :name => "users_display_name_idx"
    add_index "users", ["display_name"], :name => "users_display_name_idx"
    remove_index "users", :name => "users_email_idx"
    add_index "users", ["email"], :name => "users_email_idx"
    change_column "users", "home_lon", :double, :default => 1
    change_column "users", "home_lat", :double, :default => 1
    change_column "users", "data_public", :boolean, :default => false
    change_column "users", "display_name", :string, :default => ""
    change_column "users", "creation_time", :datetime
    change_column "users", "pass_crypt", :string, :default => nil
    change_column "users", "email", :string, :default => nil
    add_column "users", "preferences", :text

    remove_index "segments", :name => "segments_timestamp_idx"
    change_column "segments", "timestamp", :datetime
    change_column "segments", "visible", :boolean
    change_column "segments", "user_id", :bigint, :limit => 20
    change_column "segments", "node_b", :bigint, :limit => 64
    change_column "segments", "node_a", :bigint, :limit => 64
    change_column "segments", "id", :bigint, :limit => 64

    remove_index "nodes", :name => "nodes_timestamp_idx"
    change_column "nodes", "timestamp", :datetime
    change_column "nodes", "visible", :boolean
    change_column "nodes", "user_id", :bigint, :limit => 20
    change_column "nodes", "longitude", :double
    change_column "nodes", "latitude", :double
    change_column "nodes", "id", :bigint, :limit => 64

    create_table "meta_areas", myisam_table do |t|
      t.column "id",        :bigint_pk_64, :null => false
      t.column "user_id",   :bigint,  :limit => 20
      t.column "timestamp", :datetime
    end

    remove_index "messages", :name => "messages_to_user_id_idx"
    change_column "messages", "message_read", :boolean, :default => false
    change_column "messages", "sent_on", :datetime
    change_column "messages", "body", :text
    change_column "messages", "title", :string, :default => nil
    add_column "messages", "from_display_name", :string, :default => ""
    add_column "messages", "user_id", :bigint, :limit => 20, :null => false
    add_index "messages", ["from_display_name"], :name => "from_name_idx"

    create_table "gpx_pending_files", myisam_table do |t|
      t.column "originalname", :string
      t.column "tmpname",      :string
      t.column "user_id",      :bigint,  :limit => 20
    end

    change_column "gpx_files", "inserted", :boolean
    change_column "gpx_files", "description", :string, :default => ""
    change_column "gpx_files", "timestamp", :datetime
    change_column "gpx_files", "user_id", :bigint,   :limit => 20

    change_column "gpx_file_tags", "tag", :string, :default => nil

    change_column "gps_points", "gpx_id", :integer, :limit => 20
    change_column "gps_points", "longitude", :integer
    change_column "gps_points", "latitude", :integer
    change_column "gps_points", "trackid", :integer
    add_column "gps_points", "user_id", :integer, :limit => 20
    add_index "gps_points", ["user_id"], :name => "points_uid_idx"

    remove_index "friends", :name => "friends_user_id_idx"

    change_column "diary_entries", "updated_at", :datetime
    change_column "diary_entries", "created_at", :datetime
    change_column "diary_entries", "body", :text
    change_column "diary_entries", "title", :string, :default => nil

    change_engine "current_ways", "MyISAM"
    change_column "current_ways", "visible", :boolean
    change_column "current_ways", "timestamp", :datetime
    change_column "current_ways", "user_id", :bigint, :limit => 20

    change_column "current_way_tags", "id", :bigint, :limit => 64

    add_index "current_way_segments", ["id"], :name => "current_way_segments_id_idx"
    remove_primary_key "current_way_segments"
    change_column "current_way_segments", "sequence_id", :bigint, :limit => 11
    change_column "current_way_segments", "segment_id", :bigint, :limit => 11
    change_column "current_way_segments", "id", :bigint, :limit => 64

    add_index "current_segments", ["id", "visible"], :name => "current_segments_id_visible_idx"
    remove_primary_key "current_segments"
    change_column "current_segments", "timestamp", :datetime
    change_column "current_segments", "visible", :boolean
    change_column "current_segments", "user_id", :bigint, :limit => 20
    change_column "current_segments", "node_b", :bigint, :limit => 64
    change_column "current_segments", "node_a", :bigint, :limit => 64
    
    add_index "current_nodes", ["id"], :name => "current_nodes_id_idx"
    remove_primary_key "current_nodes"
    change_column "current_nodes", "timestamp", :datetime
    change_column "current_nodes", "visible", :boolean
    change_column "current_nodes", "user_id", :bigint, :limit => 20
    change_column "current_nodes", "longitude", :double
    change_column "current_nodes", "latitude", :double
    change_column "current_nodes", "id", :bigint_auto_64
  end
end
