Sha256: 4fc46f3ad7e415207953fe53439c4503e1a81ff63f5faab61bc3ff588391e962

Contents?: true

Size: 1.74 KB

Versions: 21

Compression:

Stored size: 1.74 KB

Contents

module Historiographer
  module HistoryMigrationMysql
    #
    # class CreateAdGroupHistories < ActiveRecord::Migration
    #  def change
    #    create_table :ad_group_histories do |t|
    #      t.histories
    #    end
    #  end
    # end
    #
    # t.histories(except: ["name"]) # don't include name column
    # t.histories(only: ["name"])   # only include name column
    # t.histories(no_business_columns: true) # only add history timestamps and history_user_id; manually add your own columns
    #
    # Will automatically add user_id, history_started_at,
    # and history_ended_at columns
    #
    def histories(except: [], only: [], no_business_columns: false, index_names: {})
      index_names.symbolize_keys!

      original_table_name = self.name.gsub(/_histories$/) { }.pluralize
      foreign_key = original_table_name.singularize.foreign_key

      class_definer = Class.new(ActiveRecord::Base) do
      end

      class_name = original_table_name.classify
      klass = Object.const_set(class_name, class_definer)
      original_columns = klass.columns.reject { |c| c.name == "id" || except.include?(c.name) || (only.any? && only.exclude?(c.name)) || no_business_columns }

      integer foreign_key.to_sym, null: false

      original_columns.each do |column|
        opts = {}
        opts.merge!(column.as_json.clone)

        send(column.type, column.name, opts.symbolize_keys!)
      end

      datetime :history_started_at, null: false
      datetime :history_ended_at
      integer :history_user_id

      index :history_started_at
      index :history_ended_at
      index :history_user_id
      index foreign_key

      ActiveRecord::Base.connection.indexes(original_table_name).each do |index|
        index index.columns
      end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
historiographer-4.1.14 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.13 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.12 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.11 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.10 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.9 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.8 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.7 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.5 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.4 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.3 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.2 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.1 lib/historiographer/history_migration_mysql.rb
historiographer-4.1.0 /Users/brettshollenberger/programming/historiographer/lib/historiographer/history_migration_mysql.rb
historiographer-4.0.0 lib/historiographer/history_migration_mysql.rb
historiographer-3.1.2 lib/historiographer/history_migration_mysql.rb
historiographer-3.1.1 lib/historiographer/history_migration_mysql.rb
historiographer-3.1.0 lib/historiographer/history_migration_mysql.rb
historiographer-3.0.0 lib/historiographer/history_migration_mysql.rb
historiographer-2.0.1 lib/historiographer/history_migration_mysql.rb