Module: MiniRecord::AutoSchema::ClassMethods
- Defined in:
- lib/mini_record/auto_schema.rb
Instance Method Summary collapse
-
#add_foreign_keys ⇒ Object
Add foreign keys for indexes with :foreign=>true option, if the key doesn't exists.
- #add_index(column_name, options = {}) ⇒ Object (also: #index)
- #auto_upgrade! ⇒ Object
- #clear_tables! ⇒ Object
- #connection? ⇒ Boolean
- #field(*args) ⇒ Object (also: #key, #property, #col)
- #fields ⇒ Object
- #fields_in_db ⇒ Object
- #foreign_keys ⇒ Object
- #get_sql_field_type(field) ⇒ Object
- #indexes ⇒ Object
- #indexes_in_db ⇒ Object
- #init_table_definition(connection) ⇒ Object
-
#remove_foreign_keys ⇒ Object
Remove foreign keys for indexes with :foreign=>false option.
- #reset_table_definition! ⇒ Object (also: #reset_schema!)
- #schema {|table_definition| ... } ⇒ Object
- #schema_tables ⇒ Object
- #table_definition ⇒ Object
- #timestamps ⇒ Object
Instance Method Details
#add_foreign_keys ⇒ Object
Add foreign keys for indexes with :foreign=>true option, if the key doesn't exists
171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/mini_record/auto_schema.rb', line 171 def add_foreign_keys indexes.each do |name, | if [:foreign] column = [:column].to_s unless foreign_keys.detect { |fk| fk[:options][:column] == column } to_table = reflect_on_all_associations.detect { |a| a.foreign_key.to_s==column }.table_name connection.add_foreign_key(table_name, to_table, ) foreign_keys << { :options=> { :column=>column } } end end end end |
#add_index(column_name, options = {}) ⇒ Object Also known as: index
130 131 132 133 134 |
# File 'lib/mini_record/auto_schema.rb', line 130 def add_index(column_name, ={}) index_name = connection.index_name(table_name, :column => column_name) indexes[index_name] = .merge(:column => column_name) unless indexes.key?(index_name) index_name end |
#auto_upgrade! ⇒ Object
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/mini_record/auto_schema.rb', line 184 def auto_upgrade! return unless connection? return if respond_to?(:abstract_class?) && abstract_class? if self == ActiveRecord::Base descendants.each(&:auto_upgrade!) clear_tables! else # If table doesn't exist, create it unless connection.tables.include?(table_name) # TODO: create_table options class << connection; attr_accessor :table_definition; end unless connection.respond_to?(:table_definition=) connection.table_definition = table_definition connection.create_table(table_name) connection.table_definition = init_table_definition(connection) end # Add this to our schema tables schema_tables << table_name unless schema_tables.include?(table_name) # Generate fields from associations if reflect_on_all_associations.any? reflect_on_all_associations.each do |association| foreign_key = association.[:foreign_key] || "#{association.name}_id" type_key = "#{association.name.to_s}_type" case association.macro when :belongs_to field foreign_key, :as => :integer unless fields.key?(foreign_key.to_s) if association.[:polymorphic] field type_key, :as => :string unless fields.key?(type_key.to_s) index [foreign_key, type_key] else index foreign_key end when :has_and_belongs_to_many table = if name = association.[:join_table] name.to_s else [table_name, association.name.to_s].sort.join("_") end unless connection.tables.include?(table.to_s) foreign_key = association.[:foreign_key] || association.foreign_key association_foreign_key = association.[:association_foreign_key] || association.association_foreign_key connection.create_table(table, :id => false) do |t| t.integer foreign_key t.integer association_foreign_key end index_name = connection.index_name(table, :column => [foreign_key, association_foreign_key]) index_name = index_name[0...connection.index_name_length] if index_name.length > connection.index_name_length connection.add_index table, [foreign_key, association_foreign_key], :name => index_name, :unique => true end # Add join table to our schema tables schema_tables << table unless schema_tables.include?(table) end end end # Add to schema inheritance column if necessary if descendants.present? field inheritance_column, :as => :string unless fields.key?(inheritance_column.to_s) index inheritance_column end # Remove fields from db no longer in schema (fields_in_db.keys - fields.keys & fields_in_db.keys).each do |field| column = fields_in_db[field] connection.remove_column table_name, column.name end # Add fields to db new to schema (fields.keys - fields_in_db.keys).each do |field| column = fields[field] = {:limit => column.limit, :precision => column.precision, :scale => column.scale} [:default] = column.default unless column.default.nil? [:null] = column.null unless column.null.nil? connection.add_column table_name, column.name, column.type.to_sym, end # Change attributes of existent columns (fields.keys & fields_in_db.keys).each do |field| if field != primary_key #ActiveRecord::Base.get_primary_key(table_name) changed = false # flag new_type = fields[field].type.to_sym new_attr = {} # First, check if the field type changed old_sql_type = get_sql_field_type(fields_in_db[field]) new_sql_type = get_sql_field_type(fields[field]) # Strip off the "(10,0)", from "decimal(10,0)" - we re-check this below. # This is all a bit hacky but different versions of Rails act differently. old_base_type = old_sql_type.sub(/\(.*/,'') new_base_type = new_sql_type.sub(/\(.*/,'') if old_sql_type != new_sql_type and old_base_type != new_base_type logger.debug "[MiniRecord] Detected schema change for #{table_name}.#{field}#type " + " from #{old_sql_type.inspect} to #{new_sql_type.inspect}" if logger changed = true end # Special catch for precision/scale, since *both* must be specified together # Always include them in the attr struct, but they'll only get applied if changed = true new_attr[:precision] = fields[field][:precision] new_attr[:scale] = fields[field][:scale] # If we have precision this is also the limit fields[field][:limit] ||= fields[field][:precision] # Next, iterate through our extended attributes, looking for any differences # This catches stuff like :null, :precision, etc # Ignore junk attributes that different versions of Rails include [:name, :limit, :precision, :scale, :default, :null].each do |att| value = fields[field][att] value = true if att == :null && value.nil? # Skip unspecified limit/precision/scale as DB will set them to defaults, # and on subsequent runs, this will be erroneously detected as a change. next if value.nil? and [:limit, :precision, :scale].include?(att) old_value = fields_in_db[field].send(att) if value != old_value logger.debug "[MiniRecord] Detected schema change for #{table_name}.#{field}##{att} " + "from #{old_value.inspect} to #{value.inspect}" if logger new_attr[att] = value changed = true end end # Change the column if applicable connection.change_column table_name, field, new_type, new_attr if changed end end remove_foreign_keys if connection.respond_to?(:foreign_keys) # Remove old index (indexes_in_db.keys - indexes.keys).each do |name| connection.remove_index(table_name, :name => name) end # Add indexes indexes.each do |name, | = .dup unless connection.indexes(table_name).detect { |i| i.name == name } connection.add_index(table_name, .delete(:column), ) end end add_foreign_keys if connection.respond_to?(:foreign_keys) # Reload column information reset_column_information end end |
#clear_tables! ⇒ Object
144 145 146 147 148 149 |
# File 'lib/mini_record/auto_schema.rb', line 144 def clear_tables! (connection.tables - schema_tables).each do |name| connection.drop_table(name) schema_tables.delete(name) end end |
#connection? ⇒ Boolean
137 138 139 140 141 142 |
# File 'lib/mini_record/auto_schema.rb', line 137 def connection? !!connection rescue Exception => e puts "\e[31m%s\e[0m" % e..strip false end |
#field(*args) ⇒ Object Also known as: key, property, col
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 |
# File 'lib/mini_record/auto_schema.rb', line 78 def field(*args) return unless connection? = args. type = .delete(:as) || .delete(:type) || :string index = .delete(:index) args.each do |column_name| # Allow custom types like: # t.column :type, "ENUM('EMPLOYEE','CLIENT','SUPERUSER','DEVELOPER')" if type.is_a?(String) # will be converted in: t.column :type, "ENUM('EMPLOYEE','CLIENT')" table_definition.column(column_name, type, .reverse_merge(:limit => 0)) else # wil be converted in: t.string :name table_definition.send(type, column_name, ) end # Get the correct column_name i.e. in field :category, :as => :references column_name = table_definition.columns[-1].name # Parse indexes case index when Hash add_index(.delete(:column) || column_name, index) when TrueClass add_index(column_name) when String, Symbol, Array add_index(index) end end end |
#fields ⇒ Object
64 65 66 67 68 69 |
# File 'lib/mini_record/auto_schema.rb', line 64 def fields table_definition.columns.inject({}) do |hash, column| hash[column.name] = column hash end end |
#fields_in_db ⇒ Object
71 72 73 74 75 76 |
# File 'lib/mini_record/auto_schema.rb', line 71 def fields_in_db connection.columns(table_name).inject({}) do |hash, column| hash[column.name] = column hash end end |
#foreign_keys ⇒ Object
151 152 153 154 155 |
# File 'lib/mini_record/auto_schema.rb', line 151 def foreign_keys # fk cache to minimize quantity of sql queries @foreign_keys ||= {} @foreign_keys[:table_name] ||= connection.foreign_keys(table_name) end |
#get_sql_field_type(field) ⇒ Object
54 55 56 57 58 59 60 61 62 |
# File 'lib/mini_record/auto_schema.rb', line 54 def get_sql_field_type(field) if field.respond_to?(:sql_type) # Rails 3.2 and earlier field.sql_type.to_s.downcase else # Rails 4 connection.type_to_sql(field.type.to_sym, field.limit, field.precision, field.scale) end end |
#indexes ⇒ Object
41 42 43 44 45 |
# File 'lib/mini_record/auto_schema.rb', line 41 def indexes return superclass.indexes unless (superclass == ActiveRecord::Base) || (superclass.respond_to?(:abstract_class?) && superclass.abstract_class?) @_indexes ||= {} end |
#indexes_in_db ⇒ Object
47 48 49 50 51 52 |
# File 'lib/mini_record/auto_schema.rb', line 47 def indexes_in_db connection.indexes(table_name).inject({}) do |hash, index| hash[index.name] = index hash end end |
#init_table_definition(connection) ⇒ Object
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/mini_record/auto_schema.rb', line 8 def init_table_definition(connection) #connection.create_table(table_name) unless connection.table_exists?(table_name) case ActiveRecord::ConnectionAdapters::TableDefinition.instance_method(:initialize).arity when 1 # Rails 3.2 and earlier ActiveRecord::ConnectionAdapters::TableDefinition.new(connection) when 4 # Rails 4 ActiveRecord::ConnectionAdapters::TableDefinition.new(connection.native_database_types, table_name, false, {}) when -5 # Rails 4.1 ActiveRecord::ConnectionAdapters::TableDefinition.new(connection.native_database_types, table_name, false, {}, nil) else raise ArgumentError, "Unsupported number of args for ActiveRecord::ConnectionAdapters::TableDefinition.new()" end end |
#remove_foreign_keys ⇒ Object
Remove foreign keys for indexes with :foreign=>false option
158 159 160 161 162 163 164 165 166 167 168 |
# File 'lib/mini_record/auto_schema.rb', line 158 def remove_foreign_keys indexes.each do |name, | if [:foreign]==false foreign_key = foreign_keys.detect { |fk| fk.[:column] == [:column].to_s } if foreign_key connection.remove_foreign_key(table_name, :name => foreign_key.[:name]) foreign_keys.delete(foreign_key) end end end end |
#reset_table_definition! ⇒ Object Also known as: reset_schema!
119 120 121 |
# File 'lib/mini_record/auto_schema.rb', line 119 def reset_table_definition! @_table_definition = nil end |
#schema {|table_definition| ... } ⇒ Object
124 125 126 127 128 |
# File 'lib/mini_record/auto_schema.rb', line 124 def schema reset_table_definition! yield table_definition table_definition end |
#schema_tables ⇒ Object
27 28 29 |
# File 'lib/mini_record/auto_schema.rb', line 27 def schema_tables @@_schema_tables ||= [] end |
#table_definition ⇒ Object
31 32 33 34 35 36 37 38 39 |
# File 'lib/mini_record/auto_schema.rb', line 31 def table_definition return superclass.table_definition unless (superclass == ActiveRecord::Base) || (superclass.respond_to?(:abstract_class?) && superclass.abstract_class?) @_table_definition ||= begin tb = init_table_definition(connection) tb.primary_key(primary_key) tb end end |
#timestamps ⇒ Object
115 116 117 |
# File 'lib/mini_record/auto_schema.rb', line 115 def field :created_at, :updated_at, :as => :datetime, :null => false end |