Module: MultiTenant::MigrationExtensions
- Defined in:
- lib/activerecord-multi-tenant/migrations.rb
Instance Method Summary collapse
- #citus_version ⇒ Object
- #create_distributed_table(table_name, partition_key) ⇒ Object
- #create_reference_table(table_name) ⇒ Object
- #enable_extension_on_all_nodes(extension) ⇒ Object
- #execute_on_all_nodes(sql) ⇒ Object
- #rebalance_table_shards ⇒ Object
- #undistribute_table(table_name) ⇒ Object
Instance Method Details
#citus_version ⇒ Object
59 60 61 62 63 |
# File 'lib/activerecord-multi-tenant/migrations.rb', line 59 def citus_version execute("SELECT extversion FROM pg_extension WHERE extname = 'citus'").getvalue(0, 0).try(:split, '-').try(:first) rescue ArgumentError => e raise unless e. == 'invalid tuple number 0' end |
#create_distributed_table(table_name, partition_key) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 |
# File 'lib/activerecord-multi-tenant/migrations.rb', line 3 def create_distributed_table(table_name, partition_key) return unless citus_version.present? reversible do |dir| dir.up do execute "SELECT create_distributed_table($$#{table_name}$$, $$#{partition_key}$$)" end dir.down do undistribute_table(table_name) end end end |
#create_reference_table(table_name) ⇒ Object
16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/activerecord-multi-tenant/migrations.rb', line 16 def create_reference_table(table_name) return unless citus_version.present? reversible do |dir| dir.up do execute "SELECT create_reference_table($$#{table_name}$$)" end dir.down do undistribute_table(table_name) end end end |
#enable_extension_on_all_nodes(extension) ⇒ Object
55 56 57 |
# File 'lib/activerecord-multi-tenant/migrations.rb', line 55 def enable_extension_on_all_nodes(extension) execute_on_all_nodes "CREATE EXTENSION IF NOT EXISTS \"#{extension}\"" end |
#execute_on_all_nodes(sql) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/activerecord-multi-tenant/migrations.rb', line 41 def execute_on_all_nodes(sql) execute sql case citus_version when '6.0' execute "SELECT citus_run_on_all_workers($$#{sql}$$)" # initial citus_tools.sql with different names when nil # Do nothing, this is regular Postgres else # 6.1 and newer execute "SELECT run_command_on_workers($$#{sql}$$)" end end |
#rebalance_table_shards ⇒ Object
35 36 37 38 39 |
# File 'lib/activerecord-multi-tenant/migrations.rb', line 35 def rebalance_table_shards return unless citus_version.present? execute 'SELECT rebalance_table_shards()' end |
#undistribute_table(table_name) ⇒ Object
29 30 31 32 33 |
# File 'lib/activerecord-multi-tenant/migrations.rb', line 29 def undistribute_table(table_name) return unless citus_version.present? execute "SELECT undistribute_table($$#{table_name}$$))" end |