Class: DbDiff::Database
- Inherits:
-
Object
- Object
- DbDiff::Database
- Defined in:
- lib/dbdiff/database.rb
Overview
XXX validate state of information_schema?
Instance Attribute Summary collapse
-
#dbh ⇒ Object
readonly
Returns the value of attribute dbh.
-
#deltas ⇒ Object
Returns the value of attribute deltas.
-
#functions ⇒ Object
readonly
Returns the value of attribute functions.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#password ⇒ Object
readonly
Returns the value of attribute password.
-
#procedures ⇒ Object
readonly
Returns the value of attribute procedures.
-
#tables ⇒ Object
readonly
Returns the value of attribute tables.
-
#user ⇒ Object
readonly
Returns the value of attribute user.
-
#views ⇒ Object
readonly
Returns the value of attribute views.
Instance Method Summary collapse
-
#_select(table, where = {}, &block) ⇒ Object
easy way of selecting.
- #add(element) ⇒ Object
-
#all_tables ⇒ Object
XXX may be able to get rid of this.
- #charset(collation) ⇒ Object
- #drop(element) ⇒ Object
-
#initialize(params = {}) ⇒ Database
constructor
XXX add version check.
- #initialize_columns ⇒ Object
- #initialize_foreign_keys ⇒ Object
- #initialize_functions ⇒ Object
- #initialize_keys ⇒ Object
- #initialize_procedures ⇒ Object
- #initialize_tables ⇒ Object
- #initialize_triggers ⇒ Object
- #initialize_views ⇒ Object
- #load_rows(t) ⇒ Object
- #modify(element, new_element) ⇒ Object
- #select(table, where = {}, &block) ⇒ Object
- #select_is(table, where = {}, &block) ⇒ Object
- #select_mysql(table, where = {}, &block) ⇒ Object
- #table(name) ⇒ Object
Constructor Details
#initialize(params = {}) ⇒ Database
XXX add version check
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dbdiff/database.rb', line 22 def initialize(params = {}) @dbh = Mysql.real_connect(params[:host], params[:user], params[:password], 'information_schema') @name = params[:name] @host = params[:host] @user = params[:user] @password = params[:password] @deltas = [] initialize_tables initialize_functions initialize_procedures initialize_triggers initialize_views initialize_columns initialize_keys initialize_foreign_keys end |
Instance Attribute Details
#dbh ⇒ Object (readonly)
Returns the value of attribute dbh.
18 19 20 |
# File 'lib/dbdiff/database.rb', line 18 def dbh @dbh end |
#deltas ⇒ Object
Returns the value of attribute deltas.
19 20 21 |
# File 'lib/dbdiff/database.rb', line 19 def deltas @deltas end |
#functions ⇒ Object (readonly)
Returns the value of attribute functions.
18 19 20 |
# File 'lib/dbdiff/database.rb', line 18 def functions @functions end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
18 19 20 |
# File 'lib/dbdiff/database.rb', line 18 def host @host end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
18 19 20 |
# File 'lib/dbdiff/database.rb', line 18 def name @name end |
#password ⇒ Object (readonly)
Returns the value of attribute password.
18 19 20 |
# File 'lib/dbdiff/database.rb', line 18 def password @password end |
#procedures ⇒ Object (readonly)
Returns the value of attribute procedures.
18 19 20 |
# File 'lib/dbdiff/database.rb', line 18 def procedures @procedures end |
#tables ⇒ Object (readonly)
Returns the value of attribute tables.
18 19 20 |
# File 'lib/dbdiff/database.rb', line 18 def tables @tables end |
#user ⇒ Object (readonly)
Returns the value of attribute user.
18 19 20 |
# File 'lib/dbdiff/database.rb', line 18 def user @user end |
#views ⇒ Object (readonly)
Returns the value of attribute views.
18 19 20 |
# File 'lib/dbdiff/database.rb', line 18 def views @views end |
Instance Method Details
#_select(table, where = {}, &block) ⇒ Object
easy way of selecting
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/dbdiff/database.rb', line 161 def _select(table, where = {}, &block) query = "SELECT * from #{table} " if where.length > 0 query += " WHERE " query += where.map do |k,v| if v "#{k}='" + Mysql.escape_string(v) + "'" else "#{k} IS NULL" end end.join(" AND ") end # puts "sending query #{query}" result = @dbh.query(query) if block result.each_hash do |h| yield(h) end else return result.fetch_hash end end |
#add(element) ⇒ Object
187 188 189 |
# File 'lib/dbdiff/database.rb', line 187 def add(element) @deltas += element.add_delta.to_a end |
#all_tables ⇒ Object
XXX may be able to get rid of this
41 42 43 |
# File 'lib/dbdiff/database.rb', line 41 def all_tables @tables + @deltas.find_all{|d| d.class == Delta::AddTable }.map {|x| x.element} end |
#charset(collation) ⇒ Object
84 85 86 87 |
# File 'lib/dbdiff/database.rb', line 84 def charset(collation) hash = select_is(:character_sets, :default_collate_name => collation) return hash['CHARACTER_SET_NAME'] end |
#drop(element) ⇒ Object
195 196 197 |
# File 'lib/dbdiff/database.rb', line 195 def drop(element) @deltas += element.drop_delta.to_a end |
#initialize_columns ⇒ Object
93 94 95 96 97 98 99 100 101 102 |
# File 'lib/dbdiff/database.rb', line 93 def initialize_columns # puts "table name => #{self.name}" select_is(:columns) do |h| c = Column.new(h) t = table(c.table_name) # we may not have the table since we get view columns here t.columns << c if t end end |
#initialize_foreign_keys ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
# File 'lib/dbdiff/database.rb', line 121 def initialize_foreign_keys select_is(:key_column_usage) do |h| next unless h['REFERENCED_TABLE_NAME'] c = ForeignKey.new(h) t = table(c.table_name) if y = t.foreign_keys.find {|z| z.name == c.name} y.merge(c) else t.foreign_keys << c end end end |
#initialize_functions ⇒ Object
53 54 55 56 57 58 |
# File 'lib/dbdiff/database.rb', line 53 def initialize_functions @functions = [] select_mysql(:proc, :type => 'FUNCTION') do |h| @functions << Function.new(h) end end |
#initialize_keys ⇒ Object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/dbdiff/database.rb', line 104 def initialize_keys @tables.each do|t| @dbh.select_db(name) res = @dbh.query("SHOW INDEXES FROM #{t.name}") res.each_hash do |h| k = Key.new(t.name, h) if y = t.keys.find {|z| z.name == k.name} y.merge(k) else t.keys << k end end end end |
#initialize_procedures ⇒ Object
60 61 62 63 64 65 |
# File 'lib/dbdiff/database.rb', line 60 def initialize_procedures @procedures = [] select_mysql(:proc, :type => 'PROCEDURE') do |h| @procedures << Procedure.new(h) end end |
#initialize_tables ⇒ Object
74 75 76 77 78 79 80 81 82 |
# File 'lib/dbdiff/database.rb', line 74 def initialize_tables @tables = [] select_is(:tables, :table_schema => self.name, :table_type => 'BASE TABLE') do |h| t = Table.new(h) t.charset = charset(t.collation) @tables << t end end |
#initialize_triggers ⇒ Object
45 46 47 48 49 50 51 |
# File 'lib/dbdiff/database.rb', line 45 def initialize_triggers select_is(:triggers, :trigger_schema => self.name) do |h| t = Trigger.new(h) tb = table(t.table_name) tb.triggers << t if tb end end |
#initialize_views ⇒ Object
67 68 69 70 71 72 |
# File 'lib/dbdiff/database.rb', line 67 def initialize_views @views = [] select_is(:views, :table_schema => self.name) do |h| @views << View.new(h) end end |
#load_rows(t) ⇒ Object
199 200 201 202 203 204 205 206 207 208 209 210 211 |
# File 'lib/dbdiff/database.rb', line 199 def load_rows(t) t.each do |table_name| table = table(table_name) if table select(table_name) do |h| table.rows << Row.new(table, h) end end end end |
#modify(element, new_element) ⇒ Object
191 192 193 |
# File 'lib/dbdiff/database.rb', line 191 def modify(element, new_element) @deltas += element.modify_delta(new_element).to_a end |
#select(table, where = {}, &block) ⇒ Object
155 156 157 158 |
# File 'lib/dbdiff/database.rb', line 155 def select(table, where = {}, &block) @dbh.select_db(self.name) self._select(table, where, &block) end |
#select_is(table, where = {}, &block) ⇒ Object
146 147 148 149 150 151 152 153 |
# File 'lib/dbdiff/database.rb', line 146 def select_is(table, where = {}, &block) @dbh.select_db('information_schema') # XXX where[:table_schema] = self.name unless table == :character_sets || table == :triggers self._select(table, where, &block) end |
#select_mysql(table, where = {}, &block) ⇒ Object
139 140 141 142 143 144 |
# File 'lib/dbdiff/database.rb', line 139 def select_mysql(table, where = {}, &block) @dbh.select_db('mysql') # XXX where[:db] = self.name self._select(table, where, &block) end |
#table(name) ⇒ Object
89 90 91 |
# File 'lib/dbdiff/database.rb', line 89 def table(name) @tables.find{|t| t.name == name} end |