Class: ActivePartition::Adapters::PostgresqlAdapter
- Inherits:
-
Object
- Object
- ActivePartition::Adapters::PostgresqlAdapter
- Defined in:
- lib/active_partition/adapters/postgresql_adapter.rb
Instance Method Summary collapse
-
#detach_partition(partition_name) ⇒ void
Detaches a partition from the table.
-
#drop_partition(partition_name) ⇒ void
Drops a partition table with the given name.
-
#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.
-
#get_all_supported_partition_tables ⇒ Array<String>
Retrieves all supported partition tables for a given table name.
-
#initialize(connection, table_name) ⇒ PostgresqlAdapter
constructor
A new instance of PostgresqlAdapter.
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.
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.
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.
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_tables ⇒ Array<String>
Retrieves all supported partition tables for a given table name.
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 |