class ActiveTree::Store

Attributes

debug_sql[RW]
options[RW]

Public Class Methods

new(owner_id = nil, options = ACTIVE_TREE_OPTIONS) click to toggle source
# File lib/active_tree/store.rb, line 12
def initialize(owner_id = nil, options = ACTIVE_TREE_OPTIONS)
    raise StoreException.new("Unspecified owner_id, please pass an unique identifier for the partitions: ActiveTree::Store.new(your_owner_id)") if !owner_id
    raise StoreException.new("owner_id must be an integer!") if !owner_id.is_a?(Integer)
    @owner_id = owner_id
    @options = options
    @debug_sql = false
end

Public Instance Methods

assign_to_partition() click to toggle source
# File lib/active_tree/store.rb, line 128
def assign_to_partition
    run "grant all privileges on #{partition_name} to #{ role_name }"
end
clear_schema_cache() click to toggle source
# File lib/active_tree/store.rb, line 30
def clear_schema_cache
    ActiveRecord::Base.connection.schema_cache.clear!
end
create_index(index) click to toggle source
# File lib/active_tree/store.rb, line 90
def create_index index
    return false if has_index? index
    name = index_name index
    using = name.include?("path") ? "using gist" : ""

    cols = [index].flatten.join(",")

    run "create index #{name} on #{partition_name} #{using} ( #{ cols } )"
end
create_partition(indexes = []) click to toggle source
# File lib/active_tree/store.rb, line 45
def create_partition(indexes = [])
    return false if has_partition?

    run "create table if not exists #{partition_name} partition of #{ @options[:table_name] } for values in ( #{@owner_id} )"

    indexes.each do |index|
        create_index(index) if !has_index?( index )
    end if indexes.size > 0
end
create_partition_for_owner(indexes = [ :id, :owner_id, :type, :parent_entity_id, :path, [:data_provider, :data_external_id ] ]) click to toggle source
# File lib/active_tree/store.rb, line 41
def create_partition_for_owner(indexes = [ :id, :owner_id, :type, :parent_entity_id, :path, [:data_provider, :data_external_id ] ])
    create_partition indexes
end
create_role() click to toggle source
# File lib/active_tree/store.rb, line 124
def create_role
    run "create role #{role_name}"
end
detach_partition() click to toggle source
# File lib/active_tree/store.rb, line 71
def detach_partition
    return false if !has_partition?
    run "alter table #{@options[:table_name]} detach partition #{partition_name}"
end
down!() click to toggle source
# File lib/active_tree/store.rb, line 25
def down!
    drop_role if @options[:create_postgrest_roles] == true
    remove_partition
end
drop_index(index) click to toggle source
# File lib/active_tree/store.rb, line 99
def drop_index index
    return false if !has_index? index
    run "drop index if exists #{ index_name(index) }"
end
drop_partition() click to toggle source
# File lib/active_tree/store.rb, line 75
def drop_partition
    return false if !has_partition?
    run "drop table #{partition_name}"
end
drop_role() click to toggle source
# File lib/active_tree/store.rb, line 120
def drop_role
    run "drop role if exists #{role_name}"
end
has_index?(name) click to toggle source
# File lib/active_tree/store.rb, line 87
def has_index? name
    partition_indexes.include?( index_name(name).truncate(63) )
end
has_partition?() click to toggle source

partition management

# File lib/active_tree/store.rb, line 36
def has_partition?
    clear_schema_cache
    ActiveRecord::Base.connection.schema_cache.data_source_exists? partition_name
end
index_name(identifier) click to toggle source
# File lib/active_tree/store.rb, line 104
def index_name identifier
    i = [identifier].flatten.join("_")
    "#{partition_name}_by_#{i}"
end
partition_indexes() click to toggle source

index management

# File lib/active_tree/store.rb, line 82
def partition_indexes
    clear_schema_cache
    ActiveRecord::Base.connection.indexes( partition_name ).map(&:name)
end
partition_name() click to toggle source
# File lib/active_tree/store.rb, line 55
def partition_name
    "#{@options[:table_name]}_#{@owner_id}"
end
remove_partition() click to toggle source
# File lib/active_tree/store.rb, line 63
def remove_partition
    if should_drop_partition?
        drop_partition
    else
        detach_partition
    end
end
role_name() click to toggle source
# File lib/active_tree/store.rb, line 116
def role_name
    "active_tree_owner_#{@owner_id}_#{ @options[:owner_role_suffix] }"
end
run(sql) click to toggle source

running SQL

# File lib/active_tree/store.rb, line 134
def run sql
    puts sql if @debug_sql
    ActiveRecord::Base.connection.execute sql
    true
end
setup_role() click to toggle source

TODO role management

# File lib/active_tree/store.rb, line 111
def setup_role
    drop_role
    create_role
end
should_drop_partition?() click to toggle source
# File lib/active_tree/store.rb, line 59
def should_drop_partition?
    @options[:destroy_partition_on_owner_destroy] == true
end
up!() click to toggle source
# File lib/active_tree/store.rb, line 20
def up!
    create_partition_for_owner # also adds indexes
    setup_role if @options[:create_postgrest_roles] == true
end