Sha256: 223f6befe5d1622d4e3a10fd5dbe7895894516b65b00bdc475ef1a45f71423c4

Contents?: true

Size: 1.2 KB

Versions: 4

Compression:

Stored size: 1.2 KB

Contents

# Extend Rails to support adding timestamp indexes for created_at & updated_at using day granularity.
#
# Example:
#   # rails_root/config/application.rb
#   config.after_initialize do
#     Footing.patch! ActiveRecord::ConnectionAdapters::AbstractAdapter, Footing::PGSchemaStatements
#   end
#
module Footing
  module PGSchemaStatements

    # Adds indexes to the created_at and updated_at timestamp columns with 'day' granularity.
    # This method is available to ActiveRecord::Migration change, up, down methods.
    def add_timestamp_indexes(table_name)
      %w(created_at updated_at).each do |column_name|
        name = "index_#{table_name}_on_#{column_name}"
        execute "create index #{name} on #{quote_table_name(table_name)} (date_trunc('day', #{quote_column_name(column_name)}))"
      end
    end

    # Removes indexes to the created_at and updated_at timestamp columns with 'day' granularity.
    # This method is available to ActiveRecord::Migration change, up, down methods.
    def remove_timestamp_indexes(table_name)
      %w(created_at updated_at).each do |column_name|
        name = "index_#{table_name}_on_#{column_name}"
        execute "drop index if exists #{name}"
      end
    end

  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
footing-0.1.0 lib/extensions/schema_statements.rb
footing-0.0.9 lib/extensions/schema_statements.rb
footing-0.0.8 lib/extensions/schema_statements.rb
footing-0.0.7 lib/extensions/schema_statements.rb