Skip to content

Instantly share code, notes, and snippets.

@paydro
Last active January 10, 2016 21:39
Show Gist options
  • Save paydro/feaf71d34027ecd39cdc to your computer and use it in GitHub Desktop.
Save paydro/feaf71d34027ecd39cdc to your computer and use it in GitHub Desktop.
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