How to get ActiveRecord to work with legacy partitioned / fined databases / tables?

thanks for your time first ... after all the google search, github and here, and more embarrassed about the big words (partition / shard / fedorate), I believe that I should describe the specific problem that I met and ask around.

Huge users and orders work in the databases of my company, so we separate databases and tables in various ways, some of which are described below:

way             database and table name      shard by (maybe it should be called partitioned by?)
YZ.X            db_YZ.tb_X                   order serial number last three digits
YYYYMMDD.       db_YYYYMMDD.tb               date
YYYYMM.DD       db_YYYYMM.tb_ DD             date too

The basic concept is that the databases and tables are separated by field (not necessarily the primary key), and there are too many databases and too many tables, so that the record or the magic creation of one database.yml configuration for each database and one model for each table is impossible or at least not the best solution.

I studied smart solutions, as well as data and even the source code of an active record, maybe I could use ERB to generate database.yml and connect to the database around the filter, and maybe I could use named_scope to dynamically solve the table name problem to search, but update / create operations are limited to "self.class.quoted_table_table", so I could not easily solve my problem. And even I could create one model for each table, because its number is up to 30 most.

But it's just not DRY!

I need a clean solution, for example the following DSL:

class Order < ActiveRecord::Base
   shard_by :order_serialno do |key|
      [get_db_config_by(key), #because some or all of the databaes might share the same machine in a regular way or can be configed by a hash of regex, and it can also be a const
       get_db_name_by(key), 
       get_tb_name_by(key),        
      ]
   end
end

Can anyone tell me? Any help would be greatly appreciated ~~~~

+3
source share
3

2 ( db) DbCharmer. sharding DbCharmer, .

, :

  • sharding, , sharded dabatase, shard_for(key) db.

  • :

    class MyModel < ActiveRecord::Base
      db_magic :sharded => { :sharded_connection => :my_sharding_method }
    
      def switch_shard(key)
        set_table_name(table_for_key(key))  # switch table
        shard_for(key)                      # switch connection
      end
    end
    
  • :

    MyModel.switch_shard(key).first
    MyModel.switch_shard(key).count
    

    , , shard_for(key) , switch_shard, :

    m = MyModel.switch_shard(key) # Switch connection and get a connection proxy
    m.first                       # Call any AR methods on the proxy
    m.count 
    
+1

, DSL, -, , , ActiveRecord , ​​. , , . , , , .

+1

, SQL.

/ ( -), couchDB noSQL. , REST, , .

(, ).

It would be much easier to switch to a noSQL solution and then rewrite activeRecord.

+1
source

Source: https://habr.com/ru/post/1721668/


All Articles