Class: ActivePartition::Adapters::PostgresqlAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/active_partition/adapters/postgresql_adapter.rb

Instance Method Summary collapse

Constructor Details

#initialize(connection, table_name) ⇒ PostgresqlAdapter

Returns a new instance of PostgresqlAdapter.



5
6
7
8
# File 'lib/active_partition/adapters/postgresql_adapter.rb', line 5

def initialize(connection, table_name)
  @connection = connection
  @table_name = table_name
end

Instance Method Details

#detach_partition(partition_name) ⇒ void

This method returns an undefined value.

Detaches a partition from the table.

Parameters:

  • partition_name (String)

    The name of the partition to detach.



47
48
49
50
51
# File 'lib/active_partition/adapters/postgresql_adapter.rb', line 47

def detach_partition(partition_name)
  @connection.execute <<~SQL
    ALTER TABLE IF EXISTS #{@table_name} DETACH PARTITION #{partition_name};
  SQL
end

#drop_partition(partition_name) ⇒ void

This method returns an undefined value.

Drops a partition table with the given name.

Parameters:

  • partition_name (String)

    the name of the partition table to drop



57
58
59
60
61
# File 'lib/active_partition/adapters/postgresql_adapter.rb', line 57

def drop_partition(partition_name)
  @connection.execute <<~SQL
    DROP TABLE IF EXISTS #{partition_name};
  SQL
end

#exec_create_partition_by_time_range(partition_name, unix_from, unix_to) ⇒ Range

Creates a new partition for the table based on the specified time range.

Parameters:

  • from (Time)

    The start time of the partition range.

  • to (Time)

    The end time of the partition range.

Returns:

  • (Range)

    The time range of the created partition.



14
15
16
17
18
19
20
21
22
23
# File 'lib/active_partition/adapters/postgresql_adapter.rb', line 14

def exec_create_partition_by_time_range(partition_name, unix_from, unix_to)
  sql_from = unix_from.utc.strftime("%Y-%m-%d %H:%M:%S")
  sql_to = unix_to.utc.strftime("%Y-%m-%d %H:%M:%S")

  @connection.execute <<~SQL
  CREATE TABLE IF NOT EXISTS #{partition_name}
    PARTITION OF #{@table_name}
    FOR VALUES FROM ('#{sql_from}') TO ('#{sql_to}');
  SQL
end

#get_all_supported_partition_tablesArray<String>

Retrieves all supported partition tables for a given table name.

Returns:

  • (Array<String>)

    An array of table names representing the supported partition tables.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/active_partition/adapters/postgresql_adapter.rb', line 28

def get_all_supported_partition_tables
  table_names_tuples = @connection.execute <<~SQL
  SELECT relname
    FROM pg_class c
    JOIN pg_namespace n ON n.oid = c.relnamespace
  WHERE nspname = 'public' AND
          relname LIKE '#{@table_name}_%' AND
          relkind = 'r'
  SQL

  table_names = table_names_tuples.map { |tuple| tuple["relname"] }
  # Filter supported partition names
  table_names.select { |name| name.match(/#{@table_name}_p_[0-9]{6}_[0-9]{2}_[0-9]{10}_[0-9]{10}/) }
end