Module: ActiveRecord::Timescale::SchemaMigration

Extended by:
ActiveSupport::Concern
Defined in:
lib/active_record/timescale/schema_migration.rb

Defined Under Namespace

Classes: HyperTableOptions

Instance Method Summary collapse

Instance Method Details

#add_retention_policy(relation, drop_after:, if_not_exists: false) ⇒ Object

Parameters:

  • relation (String)

    Name of the hypertable or continuous aggregate to create the policy for.

  • drop_after (String, Integer)

    Chunks fully older than this interval when the policy is run are dropped

  • if_not_exists (Boolean, nil) (defaults to: false)

    Set to true to avoid throwing an error if the drop_chunks_policy already exists. A notice is issued instead. Defaults to false.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/active_record/timescale/schema_migration.rb', line 57

def add_retention_policy(relation, drop_after:, if_not_exists: false)
  drop_after_string =
    if drop_after.is_a? String
      "INTERVAL '#{drop_after}'"
    else
      "BIGINT '#{drop_after}'"
    end

  args = []
  args << quote_table_name(relation)
  args << drop_after_string
  args << "if_not_exists => TRUE" if if_not_exists

  execute "SELECT add_retention_policy(#{args.join(", ")})"
end

#create_distributed_hyper_table(relation, **options, &block) ⇒ Object

See create_hyper_table for examples. Works exactly the same but the distributed option is forced to TRUE



50
51
52
# File 'lib/active_record/timescale/schema_migration.rb', line 50

def create_distributed_hyper_table(relation, **options, &block)
  create_hyper_table(relation, **options.merge({distributed: true}), &block)
end

#create_hyper_table(relation, time_column_name: "created_at", **options, &block) ⇒ Object

Create a Hypertable from a postgres table An existing table can be used or, if given a block, a new table will be created.

Examples:

create_hyper_table :check_ins, time_column_name: ‘checked_in_at’, id: false do |t|

t.references :user
t.datetime :checked_in_at, null: false, index: true
t.timestamps

end

# check_ins table already exists create_hyper_table :check_ins, time_column_name: ‘checked_in_at’, migrate_data: true

# Simplest example create_hyper_table :check_ins

Parameters:

  • relation (String)

    Identifier of table to convert to hypertable.

  • options.time_column_name (String)

    Name of the column containing time values as well as the primary column to partition by.

  • options.partitioning_column (String, nil)

    Name of an additional column to partition by.

  • options.number_partitions (Integer, nil)

    Number of hash partitions to use for partitioning_column. Must be > 0. Default is the number of data_nodes.

  • options.chunk_time_interval (String, Integer, nil)

    Interval in event time that each chunk covers. Must be > 0. Default is 7 days.

  • options.create_default_indexes (Boolean, nil)

    Boolean whether to create default indexes on time/partitioning columns. Default is TRUE.

  • options.if_not_exists (Boolean, nil)

    Boolean whether to print warning if table already converted to hypertable or raise exception. Default is FALSE.

  • options.partitioning_func (String, nil)

    The function to use for calculating a value’s partition.

  • options.associated_schema_name (String, nil)

    Name of the schema for internal hypertable tables. Default is _timescaledb_internal.

  • options.associated_table_prefix (String, nil)

    Prefix for internal hypertable chunk names. Default is _hyper.

  • options.migrate_data (Boolean, nil)

    Set to TRUE to migrate any existing data from the relation table to chunks in the new hypertable. A non-empty table generates an error without this option. Large tables may take significant time to migrate. Default is FALSE.

  • options.time_partitioning_func (String, nil)

    Function to convert incompatible primary time column values to compatible ones. The function must be IMMUTABLE.

  • options.replicator_factor (Integer, nil)

    The number of data nodes to which the same data is written to. This is done by creating chunk copies on this amount of data nodes. Must be >= 1; If not set, the default value is determined by the timescaledb.hypertable_replication_factor_default GUC.

  • options.data_nodes (Array<String>, nil)

    The set of data nodes used for the distributed hypertable. If not present, defaults to all data nodes known by the access node (the node on which the distributed hypertable is created).

  • options.distributed (Boolean, nil)

    Set to TRUE to create distributed hypertable. If not provided, value is determined by the timescaledb.hypertable_distributed_default GUC. When creating a distributed hypertable, consider using create_distributed_hypertable in place of create_hypertable. Default is NULL.



43
44
45
46
47
# File 'lib/active_record/timescale/schema_migration.rb', line 43

def create_hyper_table(relation, time_column_name: "created_at", **options, &block)
  hyper_table_options = HyperTableOptions.new(**options)
  create_table(relation, **hyper_table_options.args, &block) unless block.nil?
  execute "SELECT create_hyper_table(#{quote_table_name(relation)}, #{quote_column_name(time_column_name)}, #{hyper_table_options.to_sql})"
end