Created
January 19, 2010 22:06
-
-
Save barinek/281358 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 ActiveRecord::ConnectionAdapters::MysqlAdapter | |
def create_partitioned_table(table_name, &block) | |
partition_sql = <<-sql | |
ENGINE=InnoDB AUTO_INCREMENT=610925 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci | |
partition by range (to_days(created_at)) | |
( | |
partition p_0 values LESS THAN (to_days('2000-01-01')), | |
sql | |
partitions = (-4..4).collect do |i| | |
"partition p_#{(Time.zone.now+i.days).strftime("%Y%m%d") } values LESS THAN (to_days('#{(Time.zone.now+i.days).to_mysql_format}'))" | |
end | |
partition_sql << partitions.join(",\n") << ');' | |
options = {:options => partition_sql} | |
create_table(table_name, options, &block) | |
end | |
def native_database_types | |
NATIVE_DATABASE_TYPES[:auto] = "int(11) NOT NULL auto_increment".freeze | |
NATIVE_DATABASE_TYPES | |
end | |
end | |
class ActiveRecord::ConnectionAdapters::TableDefinition | |
original_to_sql = self.instance_method(:to_sql) | |
def composite_primary_key *args | |
@columns << CompositeKeyColumn.new(*args) | |
end | |
define_method(:to_sql) do | |
composite_key_count = @columns.count {|column| column.is_a? CompositeKeyColumn} | |
if composite_key_count > 0 | |
@columns.each do |column| | |
column.type = :auto if column.sql_type.include?('PRIMARY KEY') | |
end | |
end | |
original_to_sql.bind(self).call | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment