lib/seed-fu/seeder.rb in seed-fu-2.3.1 vs lib/seed-fu/seeder.rb in seed-fu-2.3.2
- old
+ new
@@ -84,17 +84,17 @@
Hash[@constraints.map { |c| [c, data[c.to_sym]] }]
end
def update_id_sequence
if @model_class.connection.adapter_name == "PostgreSQL"
+ return if @model_class.primary_key.nil? || @model_class.sequence_name.nil?
+
quoted_id = @model_class.connection.quote_column_name(@model_class.primary_key)
- quoted_sequence = "'" + @model_class.sequence_name + "'"
- @model_class.connection.execute(
- "SELECT pg_catalog.setval(" +
- "#{quoted_sequence}," +
- "(SELECT MAX(#{quoted_id}) FROM #{@model_class.quoted_table_name}) + 1" +
- ");"
- )
+ sequence = @model_class.sequence_name
+
+ @model_class.connection.execute <<-EOS
+ SELECT setval('#{sequence}', (SELECT GREATEST(MAX(#{quoted_id})+(SELECT increment_by FROM #{sequence}), (SELECT min_value FROM #{sequence})) FROM #{@model_class.quoted_table_name}), false)
+ EOS
end
end
end
end