Class: ActiveTree::Store
- Inherits:
-
Object
- Object
- ActiveTree::Store
- Defined in:
- lib/active_tree/store.rb
Defined Under Namespace
Classes: StoreException
Instance Attribute Summary collapse
-
#debug_sql ⇒ Object
Returns the value of attribute debug_sql.
-
#options ⇒ Object
Returns the value of attribute options.
Instance Method Summary collapse
-
#assign_to_partition ⇒ Object
create role.
-
#clear_schema_cache ⇒ Object
down!.
-
#create_index(index) ⇒ Object
has_index.
- #create_partition(indexes = []) ⇒ Object
- #create_partition_for_owner(indexes = [ :id, :owner_id, :type, :parent_entity_id, :path, [:data_provider, :data_external_id ] ]) ⇒ Object
-
#create_role ⇒ Object
drop role.
-
#detach_partition ⇒ Object
remove_partition.
-
#down! ⇒ Object
up!.
-
#drop_index(index) ⇒ Object
create_index.
-
#drop_partition ⇒ Object
detach_partition.
-
#drop_role ⇒ Object
role_name.
- #has_index?(name) ⇒ Boolean
-
#has_partition? ⇒ Boolean
partition management.
-
#index_name(identifier) ⇒ Object
drop_index.
-
#initialize(owner_id = nil, options = ACTIVE_TREE_OPTIONS) ⇒ Store
constructor
A new instance of Store.
-
#partition_indexes ⇒ Object
index management.
-
#partition_name ⇒ Object
create_partition.
- #remove_partition ⇒ Object
-
#role_name ⇒ Object
setup_role.
-
#run(sql) ⇒ Object
running SQL.
-
#setup_role ⇒ Object
TODO role management.
-
#should_drop_partition? ⇒ Boolean
partition_name.
- #up! ⇒ Object
Constructor Details
#initialize(owner_id = nil, options = ACTIVE_TREE_OPTIONS) ⇒ Store
Returns a new instance of Store.
12 13 14 15 16 17 18 |
# File 'lib/active_tree/store.rb', line 12 def initialize(owner_id = nil, = 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 = @debug_sql = false end |
Instance Attribute Details
#debug_sql ⇒ Object
Returns the value of attribute debug_sql.
3 4 5 |
# File 'lib/active_tree/store.rb', line 3 def debug_sql @debug_sql end |
#options ⇒ Object
Returns the value of attribute options.
2 3 4 |
# File 'lib/active_tree/store.rb', line 2 def @options end |
Instance Method Details
#assign_to_partition ⇒ Object
create role
128 129 130 |
# 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 ⇒ Object
down!
30 31 32 |
# File 'lib/active_tree/store.rb', line 30 def clear_schema_cache ActiveRecord::Base.connection.schema_cache.clear! end |
#create_index(index) ⇒ Object
has_index
90 91 92 93 94 95 96 97 98 |
# 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 = []) ⇒ Object
45 46 47 48 49 50 51 52 53 |
# 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 ] ]) ⇒ Object
41 42 43 |
# 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 ⇒ Object
drop role
124 125 126 |
# File 'lib/active_tree/store.rb', line 124 def create_role run "create role #{role_name}" end |
#detach_partition ⇒ Object
remove_partition
71 72 73 74 |
# 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! ⇒ Object
up!
25 26 27 28 |
# File 'lib/active_tree/store.rb', line 25 def down! drop_role if @options[:create_postgrest_roles] == true remove_partition end |
#drop_index(index) ⇒ Object
create_index
99 100 101 102 |
# 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 ⇒ Object
detach_partition
75 76 77 78 |
# File 'lib/active_tree/store.rb', line 75 def drop_partition return false if !has_partition? run "drop table #{partition_name}" end |
#drop_role ⇒ Object
role_name
120 121 122 |
# File 'lib/active_tree/store.rb', line 120 def drop_role run "drop role if exists #{role_name}" end |
#has_index?(name) ⇒ Boolean
87 88 89 |
# File 'lib/active_tree/store.rb', line 87 def has_index? name partition_indexes.include?( index_name(name).truncate(63) ) end |
#has_partition? ⇒ Boolean
partition management
36 37 38 39 |
# 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) ⇒ Object
drop_index
104 105 106 107 |
# File 'lib/active_tree/store.rb', line 104 def index_name identifier i = [identifier].flatten.join("_") "#{partition_name}_by_#{i}" end |
#partition_indexes ⇒ Object
index management
82 83 84 85 |
# 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 ⇒ Object
create_partition
55 56 57 |
# File 'lib/active_tree/store.rb', line 55 def partition_name "#{@options[:table_name]}_#{@owner_id}" end |
#remove_partition ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/active_tree/store.rb', line 63 def remove_partition if should_drop_partition? drop_partition else detach_partition end end |
#role_name ⇒ Object
setup_role
116 117 118 |
# File 'lib/active_tree/store.rb', line 116 def role_name "active_tree_owner_#{@owner_id}_#{ @options[:owner_role_suffix] }" end |
#run(sql) ⇒ Object
running SQL
134 135 136 137 138 |
# 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 ⇒ Object
TODO role management
111 112 113 114 |
# File 'lib/active_tree/store.rb', line 111 def setup_role drop_role create_role end |
#should_drop_partition? ⇒ Boolean
partition_name
59 60 61 |
# File 'lib/active_tree/store.rb', line 59 def should_drop_partition? @options[:destroy_partition_on_owner_destroy] == true end |
#up! ⇒ Object
20 21 22 23 |
# 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 |