Last active
January 10, 2016 21:39
-
-
Save paydro/feaf71d34027ecd39cdc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PlayerShard < ActiveRecord::Base | |
def self.with_date(date) | |
current_table_name = self.table_name | |
self.table_name = self.shard_table_name(date) | |
create_shard(date) | |
yield | |
ensure | |
self.table_name = current_table_name | |
end | |
def self.create_shard(date) | |
connection.execute(<<-CREATE_SHARD) | |
CREATE TABLE IF NOT EXISTS `player_shards_#{date.strftime("%Y%m%d")}` ( | |
`id` int(11) unsigned NOT NULL AUTO_INCREMENT, | |
`user_id` int(11) NOT NULL, | |
-- other columns | |
PRIMARY KEY (`id`), | |
-- you'll probably want indexes too! | |
KEY `index_player_sessions_on_user_id` (`user_id`) | |
) ENGINE=InnoDB DEFAULT CHARSET=utf8; | |
CREATE_SHARD | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment