Module: Footing::PGSchemaStatements

Defined in:
lib/footing/extensions/schema_statements.rb

Instance Method Summary collapse

Instance Method Details

#add_datetime_index(table_name, column_name, options = {}) ⇒ Object

Adds an index on a datetime column using the specified precision.

Parameters:

  • table_name (Symbol, String)

    The name of the table to migrate.

  • column_name (Symbol, String)

    The name of the column to migrate.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :precision (Symbol, String)
    • microseconds

    • milliseconds

    • second

    • minute *default

    • hour

    • day

    • week

    • month

    • quarter

    • year

    • decade

    • century

    • millennium



44
45
46
47
48
# File 'lib/footing/extensions/schema_statements.rb', line 44

def add_datetime_index(table_name, column_name, options={})
  options[:precision] ||= :minute
  index_name = "index_#{table_name}_on_#{column_name}_by_#{options[:precision]}"
  execute "create index #{index_name} on #{quote_table_name(table_name)} (date_trunc('#{options[:precision]}', #{quote_column_name(column_name)}))"
end

#add_timestamp_indexes(table_name) ⇒ Object

Adds indexes to the created_at and updated_at timestamp columns with ‘day’ granularity.



13
14
15
16
17
# File 'lib/footing/extensions/schema_statements.rb', line 13

def add_timestamp_indexes(table_name)
  %w(created_at updated_at).each do |column_name|
    add_datetime_index(table_name, column_name, :precision => :day)
  end
end

#remove_datetime_index(table_name, column_name, options = {}) ⇒ Object

Removes an index on a datetime column using the specified precision.

Parameters:

  • table_name (Symbol, String)

    The name of the table to migrate.

  • column_name (Symbol, String)

    The name of the column to migrate.

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :precision (Symbol, String)
    • microseconds

    • milliseconds

    • second

    • minute *default

    • hour

    • day

    • week

    • month

    • quarter

    • year

    • decade

    • century

    • millennium



68
69
70
71
72
# File 'lib/footing/extensions/schema_statements.rb', line 68

def remove_datetime_index(table_name, column_name, options={})
  options[:precision] ||= :minute
  index_name = "index_#{table_name}_on_#{column_name}_by_#{options[:precision]}"
  execute "drop index if exists #{index_name}"
end

#remove_timestamp_indexes(table_name) ⇒ Object

Removes indexes to the created_at and updated_at timestamp columns with ‘day’ granularity.



20
21
22
23
24
# File 'lib/footing/extensions/schema_statements.rb', line 20

def remove_timestamp_indexes(table_name)
  %w(created_at updated_at).each do |column_name|
    remove_datetime_index(table_name, column_name, :precision => :day)
  end
end