Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Ticket #6838: timestamped_migrations.v1.patch

File timestamped_migrations.v1.patch, 3.6 kB (added by francois.beausoleil, 2 years ago)

Work in progress to replace versions with a timestamp on migration files

  • vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb

    old new  
    239239        rescue ActiveRecord::StatementInvalid 
    240240          # Schema has been intialized 
    241241        end 
     242         
     243        begin 
     244          execute "CREATE TABLE #{ActiveRecord::Migrator.schema_migrations_table_name} (migration #{type_to_sql(:string, 14)}, migrated_at #{type_to_sql(:datetime)})" 
     245          execute "CREATE INDEX by_migration ON #{ActiveRecord::Migrator.schema_migrations_table_name}(migration)" 
     246        rescue ActiveRecord::StatementInvalid 
     247          # Schema has been intialized 
     248        end 
    242249      end 
    243250 
    244251      def dump_schema_information #:nodoc: 
  • vendor/rails/activerecord/lib/active_record/migration.rb

    old new  
    303303      def schema_info_table_name 
    304304        Base.table_name_prefix + "schema_info" + Base.table_name_suffix 
    305305      end 
     306       
     307      def schema_migrations_table_name 
     308        Base.table_name_prefix + "schema_migrations" + Base.table_name_suffix 
     309      end 
    306310 
    307311      def current_version 
    308         (Base.connection.select_one("SELECT version FROM #{schema_info_table_name}") || {"version" => 0})["version"].to_i 
     312        (Base.connection.select_one("SELECT MAX(migration) AS migration FROM #{schema_migrations_table_name}") || {"migration" => 0})["migration"].to_i 
    309313      end 
    310314 
    311315      def proper_table_name(name) 
     
    370374      end 
    371375       
    372376      def set_schema_version(version) 
    373         Base.connection.update("UPDATE #{self.class.schema_info_table_name} SET version = #{down? ? version.to_i - 1 : version.to_i}") 
     377        if down? then 
     378          Base.connection.delete("DELETE FROM #{self.class.schema_migrations_table_name} WHERE migration = #{version}") 
     379        else 
     380          Base.connection.insert("INSERT INTO #{self.class.schema_migrations_table_name}(migration, migrated_at) VALUES (#{version}, '#{Time.now.utc.to_formatted_s(:db)}')") 
     381        end 
    374382      end 
    375383       
    376384      def up? 
     
    386394      end 
    387395       
    388396      def irrelevant_migration?(version) 
    389         (up? && version.to_i <= current_version) || (down? && version.to_i > current_version) 
     397        count = Base.connection.select_values("SELECT COUNT(*) FROM #{self.class.schema_migrations_table_name} WHERE migration = #{version}").first 
     398        !(count.blank? || count.to_i.zero?) 
    390399      end 
    391400  end 
    392401end 
  • vendor/rails/railties/lib/rails_generator/commands.rb

    old new  
     1$stderr.puts "loading vendor/rails/railties/lib/rails_generator/commands.rb" 
    12require 'delegate' 
    23require 'optparse' 
    34require 'fileutils' 
     
    7677          end 
    7778 
    7879          def next_migration_number 
    79             current_migration_number + 1 
     80            Time.now.utc.strftime('%Y%m%d%H%M%S').to_i 
    8081          end 
    8182 
    82           def next_migration_string(padding = 3
     83          def next_migration_string(padding = 14
    8384            "%.#{padding}d" % next_migration_number 
    8485          end 
    8586