Module: AutoMigrations::ClassMethods
- Defined in:
- lib/auto_migrations.rb
Instance Method Summary collapse
- #auto_add_index(method, *args, &block) ⇒ Object
- #auto_create_table(method, *args) {|table_definition| ... } ⇒ Object
- #drop_unused_indexes ⇒ Object
- #drop_unused_tables ⇒ Object
- #method_missing_with_auto_migration(method, *args, &block) ⇒ Object
- #update_schema_version(version) ⇒ Object
Instance Method Details
#auto_add_index(method, *args, &block) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/auto_migrations.rb', line 107 def auto_add_index(method, *args, &block) table_name = args.shift.to_s fields = Array(args.shift).map(&:to_s) = args.shift index_name = [:name] if index_name ||= "index_#{table_name}_on_#{fields.join('_and_')}" (self.indexes_in_schema ||= []) << index_name unless ActiveRecord::Base.connection.indexes(table_name).detect { |i| i.name == index_name } method_missing_without_auto_migration(method, *[table_name, fields, ], &block) end end |
#auto_create_table(method, *args) {|table_definition| ... } ⇒ Object
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 |
# File 'lib/auto_migrations.rb', line 60 def auto_create_table(method, *args, &block) table_name = args.shift.to_s = args.pop || {} (self.tables_in_schema ||= []) << table_name # Table doesn't exist, create it unless ActiveRecord::Base.connection.tables.include?(table_name) return method_missing_without_auto_migration(method, *[table_name, ], &block) end # Grab database columns fields_in_db = ActiveRecord::Base.connection.columns(table_name).inject({}) do |hash, column| hash[column.name] = column hash end # Grab schema columns (lifted from active_record/connection_adapters/abstract/schema_statements.rb) table_definition = ActiveRecord::ConnectionAdapters::TableDefinition.new(ActiveRecord::Base.connection) table_definition.primary_key([:primary_key] || "id") unless [:id] == false yield table_definition fields_in_schema = table_definition.columns.inject({}) do |hash, column| hash[column.name.to_s] = column hash end # Add fields to db new to schema (fields_in_schema.keys - fields_in_db.keys).each do |field| column = fields_in_schema[field] add_column table_name, column.name, column.type.to_sym, :limit => column.limit, :precision => column.precision, :scale => column.scale, :default => column.default, :null => column.null end # Remove fields from db no longer in schema (fields_in_db.keys - fields_in_schema.keys & fields_in_db.keys).each do |field| column = fields_in_db[field] remove_column table_name, column.name end # Change field type if schema is different from db (fields_in_schema.keys & fields_in_db.keys).each do |field| if (field != 'id') && (fields_in_schema[field].type.to_sym != fields_in_db[field].type.to_sym) change_column table_name, fields_in_schema[field].name.to_sym, fields_in_schema[field].type.to_sym end end end |
#drop_unused_indexes ⇒ Object
128 129 130 131 132 133 134 135 |
# File 'lib/auto_migrations.rb', line 128 def drop_unused_indexes tables_in_schema.each do |table_name| indexes_in_db = ActiveRecord::Base.connection.indexes(table_name).map(&:name) (indexes_in_db - indexes_in_schema & indexes_in_db).each do |index_name| remove_index table_name, :name => index_name end end end |
#drop_unused_tables ⇒ Object
122 123 124 125 126 |
# File 'lib/auto_migrations.rb', line 122 def drop_unused_tables (ActiveRecord::Base.connection.tables - tables_in_schema - %w(schema_info schema_migrations)).each do |table| drop_table table end end |
#method_missing_with_auto_migration(method, *args, &block) ⇒ Object
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/auto_migrations.rb', line 49 def method_missing_with_auto_migration(method, *args, &block) case method when :create_table auto_create_table(method, *args, &block) when :add_index auto_add_index(method, *args, &block) else method_missing_without_auto_migration(method, *args, &block) end end |
#update_schema_version(version) ⇒ Object
137 138 139 140 |
# File 'lib/auto_migrations.rb', line 137 def update_schema_version(version) schema_info_table_name = ActiveRecord::Migrator.schema_info_table_name rescue ActiveRecord::Migrator.schema_migrations_table_name ActiveRecord::Base.connection.update("UPDATE #{schema_info_table_name} SET version = #{version}") end |