Revisions

gist: 211064 Download_button fork
public
Public Clone URL: git://gist.github.com/211064.git
Embed All Files: show embed
import_db #
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env ruby
# A small script to import a production gzip'ed dump
 
require "yaml"
require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")
 
class DatabaseLoader
  attr_reader :db_dump_filename
  
  def initialize(filename, run_migrations=false)
    @db_dump_filename = filename.dup
    @run_migrations = run_migrations ? true : false
  end
  
  def load
    # gunzip
    # import
    redefine_views
    # run_migrations
  end
  
  def gunzip
    if needs_unziping?
      puts "* Gunzip'ing #{db_dump_filename}"
      puts %x(gunzip #{db_dump_filename})
      @db_dump_filename.gsub!(".gz", "")
    end
  end
 
  def import
    puts "* Importing db dump #{db_dump_filename} to #{database_name}"
    %x(mysql -u root #{database_name} < #{db_dump_filename})
  end
  
  def run_migrations
    if should_run_migrations?
      puts "* Running migrations"
      puts `rake db:migrate`
    end
  end
 
  def redefine_views
    puts "* Redefining views under the current database user"
    ViewRedefiner.redefine_views!
  end
 
private
  
  def should_run_migrations?
    @run_migrations == true
  end
 
  def needs_unziping?
    db_dump_filename.include?(".gz")
  end
 
  def database_name
    @database_name ||= development_config["database"]
  end
  
  def database_username
    development_config["username"]
  end
  
  def database_host
    development_config["host"]
  end
  
  def development_config
    yaml_config["development"]
  end
 
  def yaml_config
    @yaml_config ||= YAML.load(File.read("config/database.yml"))
  end
 
  def db_dump_contents(&blk)
    File.open(db_dump_filename, "r+", &blk)
  end
end
 
class ViewRedefiner
  def self.redefine_views!
    new.redefine_views
  end
 
  def redefine_views
    views.each do |view|
      redefine_view(view)
    end
  end
 
  def redefine_view(view)
    definition = get_view_definition(view)
    definition.gsub(/ALGORITHM.*DEFINER=.*SQL SECURITY DEFINER\s+/, "")
    drop_view(view)
    create_view(definition)
  end
 
  def get_view_definition(view_name)
    execute_returning_hash("SHOW CREATE TABLE `#{view_name}`").first["Create View"]
  end
 
  def drop_view(view_name)
    execute "DROP VIEW `#{view_name}"
  end
 
  # [{"collation_connection"=>"utf8_general_ci",
  # "character_set_client"=>"utf8",
  # "Create View"=>
  # "CREATE ALGORITHM=UNDEFINED DEFINER=`root`@`localhost` SQL SECURITY DEFINER VIEW `causes_index` AS (select `causes`.`id` AS `id`,`causes`.`name` AS `name`,`categories`.`name` AS `category_name`,`causes`.`website` AS `website`,if((`causes`.`display_on_homepage` = 1),_utf8'Yes',_utf8'No') AS `homepage_bool`,`causes`.`image_id` AS `image_id`,`causes`.`slug` AS `slug` from (`causes` left join `categories` on((`causes`.`category_id` = `categories`.`id`))))",
  # "View"=>"causes_index"}]
  def create_view(definition)
    execute definition
  end
 
  # >> pp hash
  # [{"Table_type"=>"VIEW", "Tables_in_designcause_dev"=>"braintree_orders_index"},
  # {"Table_type"=>"VIEW", "Tables_in_designcause_dev"=>"causes_index"},
  # {"Table_type"=>"VIEW", "Tables_in_designcause_dev"=>"competitions_index"},
  # {"Table_type"=>"VIEW", "Tables_in_designcause_dev"=>"pencil_orders_index"},
  # {"Table_type"=>"VIEW", "Tables_in_designcause_dev"=>"products_index"},
  # {"Table_type"=>"VIEW", "Tables_in_designcause_dev"=>"promo_code_index"},
  # {"Table_type"=>"VIEW", "Tables_in_designcause_dev"=>"submissions_index"}]
  def views
    show_views.map { |x| x.to_a.last.last }
  end
 
  def show_views
    execute_returning_hash("SHOW FULL TABLES WHERE table_type = 'view'")
  end
 
  def execute_returning_hash(stmt)
    execute(stmt).all_hashes.dup
  end
 
  def execute(stmt)
    puts "* Executing sql: #{stmt}"
    ActiveRecord::Base.connection.execute(stmt)
  end
end
 
db_loader = DatabaseLoader.new(ARGV[0], ARGV[1])
db_loader.load