lib/partitioned/partitioned_base/sql_adapter.rb in partitioned-1.3.5 vs lib/partitioned/partitioned_base/sql_adapter.rb in partitioned-2.0.0

- old
+ new

@@ -55,17 +55,18 @@ # # Does a specific child partition exist. # def partition_exists?(*partition_key_values) - return find(:first, - :from => "pg_tables", - :select => "count(*) as count", - :conditions => ["schemaname = ? and tablename = ?", - configurator.schema_name, - configurator.part_name(*partition_key_values) - ]).count.to_i == 1 + query = <<-SQL + SELECT COUNT(*) AS count + FROM pg_tables + WHERE schemaname = '#{configurator.schema_name}' + AND tablename = '#{configurator.part_name(*partition_key_values)}' + LIMIT 1 + SQL + return find_by_sql(query).first.count.to_i == 1 end # # Returns an array of partition table names from last to first limited to # the number of entries requested by its first parameter. @@ -88,16 +89,19 @@ # $1 = the name of schema (foos_partitions) # $2 = the order by clause that would make the greatest table name listed first # $3 = the parameter 'how_many' # def last_n_partition_names(how_many = 1) - return find(:all, - :from => "pg_tables", - :select => :tablename, - :conditions => ["schemaname = ?", configurator.schema_name], - :order => last_n_partitions_order_by_clause, - :limit => how_many).map(&:tablename) + order_by_clause = last_n_partitions_order_by_clause + query = <<-SQL + SELECT tablename + FROM pg_tables + WHERE schemaname = '#{configurator.schema_name}' + #{order_by_clause.nil? ? "" : "ORDER BY " + order_by_clause} + LIMIT 1 + SQL + return find_by_sql(query).map(&:tablename) end # # Override this or order the tables from last (greatest value? greatest date?) to first. # @@ -160,11 +164,23 @@ def create_partition_table(*partition_key_values) create_table(configurator.table_name(*partition_key_values), { :id => false, :options => "INHERITS (#{configurator.parent_table_name(*partition_key_values)})" }) do |t| - constraint = configurator.check_constraint(*partition_key_values) - t.check_constraint constraint if constraint + end + add_check_constraint(*partition_key_values) + end + + # + # Add the check constraint to the table (if applicable) + # + def add_check_constraint(*partition_key_values) + constraint = configurator.check_constraint(*partition_key_values) + if constraint + sql = <<-SQL + ALTER TABLE #{configurator.table_name(*partition_key_values)} ADD CHECK (#{constraint}); + SQL + execute(sql) end end # # Remove a specific single child table.