Module: AutoMigrations
- Defined in:
- lib/db_auto_migrations.rb
Defined Under Namespace
Modules: ClassMethods
Class Method Summary collapse
Class Method Details
.included(base) ⇒ Object
43 44 45 46 47 48 49 50 51 |
# File 'lib/db_auto_migrations.rb', line 43 def self.included(base) base.extend ClassMethods class << base cattr_accessor :tables_in_schema, :indexes_in_schema self.tables_in_schema, self.indexes_in_schema = [], [] alias_method :method_missing_without_auto_migration, :method_missing alias_method :method_missing, :method_missing_with_auto_migration end end |
.run ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/db_auto_migrations.rb', line 5 def self.run # Turn off schema_info code for auto-migration class << ActiveRecord::Schema alias :old_define :define attr_accessor :version def define(info={}, &block) @version = Time.now.utc.strftime("%Y%m%d%H%M%S"); instance_eval(&block) end end load(File.join(Rails.root, 'db', 'schema.rb')) ActiveRecord::Migration.drop_unused_tables ActiveRecord::Migration.drop_unused_indexes ActiveRecord::Migration.update_schema_version(ActiveRecord::Schema.version) if ActiveRecord::Schema.version class << ActiveRecord::Schema alias :define :old_define end end |
.schema_to_migration(with_reset = false) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/db_auto_migrations.rb', line 23 def self.schema_to_migration(with_reset = false) schema_in = File.read(File.join(Rails.root, "db", "schema.rb")) schema_in.gsub!(/#(.)+\n/, '') schema_in.sub!(/ActiveRecord::Schema.define(.+)do[ ]?\n/, '') schema_in.gsub!(/^/, ' ') schema = "class InitialSchema < ActiveRecord::Migration\n def self.up\n" schema += " # We're resetting the migrations database...\n" + " drop_table :schema_migrations\n" + " initialize_schema_migrations_table\n\n" if with_reset schema += schema_in schema << "\n def self.down\n" schema << ((ActiveRecord::VERSION::MAJOR >= 5 ? ActiveRecord::Base.connection.data_sources : ActiveRecord::Base.connection.tables) - %w(schema_info schema_migrations)).map do |table| " drop_table :#{table}\n" end.join schema << " end\nend\n" migration_file = File.join(Rails.root, "db", "migrate", "001_initial_schema.rb") File.open(migration_file, "w") { |f| f << schema } puts "Migration created at db/migrate/001_initial_schema.rb" end |