<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: sequences code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Thu, 24 Jul 2008 04:38:08 GMT</pubDate>
    <description>DZone Snippets: sequences code</description>
    <item>
      <title>Adjust Database Sequences</title>
      <link>http://snippets.dzone.com/posts/show/3021</link>
      <description>If loading data through fixtures or other means with hard-coded IDs, database sequences may need to be adjusted.  This is written for Oracle.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :db do&lt;br /&gt;  task :adjust_sequences =&gt; :environment do&lt;br /&gt;    ActiveRecord::Base.connection.tables.each do |table|&lt;br /&gt;      begin&lt;br /&gt;        count = ActiveRecord::Base.count_by_sql("SELECT MAX(id) FROM #{table}")&lt;br /&gt;        seq = "#{table}_seq"&lt;br /&gt;        ActiveRecord::Base.connection.execute("DROP SEQUENCE #{seq}")&lt;br /&gt;        ActiveRecord::Base.connection.execute("CREATE SEQUENCE #{seq} START WITH #{count+1}")&lt;br /&gt;      rescue =&gt; ex&lt;br /&gt;        puts "Failed for #{table} with #{ex.class}"&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 20 Nov 2006 07:22:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3021</guid>
      <author>dcmanges (Dan Manges)</author>
    </item>
  </channel>
</rss>
