<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: mysql code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sun, 27 Jul 2008 02:36:29 GMT</pubDate>
    <description>DZone Snippets: mysql code</description>
    <item>
      <title>Restore a single table from a large MySQL backup</title>
      <link>http://snippets.dzone.com/posts/show/4819</link>
      <description>Say, for some reason, you need to restore the entire contents of a single table from a HUGE mysqldump generated backup containing several tables. For example:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;create table `baz`;&lt;br /&gt;&lt;br /&gt;GIGS OF SQL YOU DON'T WANT;&lt;br /&gt;&lt;br /&gt;create table `foo`;&lt;br /&gt;&lt;br /&gt;A COUPLE THOUSAND LINES YOU DO WANT;&lt;br /&gt;&lt;br /&gt;create table `bar`;&lt;br /&gt;&lt;br /&gt;MORE SQL YOU DON'T WANT;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;With a little dash 'o ruby, you can extract just the part you want:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ ruby -ne '@found=true if $_ =~ /^CREATE TABLE `foo`/i; next unless @found; exit if $_ =~ /^CREATE TABLE (?!`foo`)/i; puts $_;' giant_sql_dump.sql &gt; foo.sql&lt;br /&gt;$ cat foo.sql&lt;br /&gt;create table `foo`;&lt;br /&gt;&lt;br /&gt;A COUPLE THOUSAND LINES YOU DO WANT;&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;You can then easily restore that entire table:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;$ mysql mydatabase -e 'drop table foo'&lt;br /&gt;$ mysql mydatabase &lt; foo.sql&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 28 Nov 2007 14:10:03 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4819</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
    <item>
      <title>Synchronizing Rails DB Contents via Fixtures</title>
      <link>http://snippets.dzone.com/posts/show/3393</link>
      <description>The following rake task will dump the contents of the current environment's database to YAML fixtures. Stick the following in lib/tasks/fixtures.rake:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;namespace :db do&lt;br /&gt;  namespace :fixtures do&lt;br /&gt;    &lt;br /&gt;    desc 'Create YAML test fixtures from data in an existing database.  &lt;br /&gt;    Defaults to development database.  Set RAILS_ENV to override.'&lt;br /&gt;    task :dump =&gt; :environment do&lt;br /&gt;      sql  = "SELECT * FROM %s"&lt;br /&gt;      skip_tables = ["schema_info"]&lt;br /&gt;      ActiveRecord::Base.establish_connection(:development)&lt;br /&gt;      (ActiveRecord::Base.connection.tables - skip_tables).each do |table_name|&lt;br /&gt;        i = "000"&lt;br /&gt;        File.open("#{RAILS_ROOT}/test/fixtures/#{table_name}.yml", 'w') do |file|&lt;br /&gt;          data = ActiveRecord::Base.connection.select_all(sql % table_name)&lt;br /&gt;          file.write data.inject({}) { |hash, record|&lt;br /&gt;            hash["#{table_name}_#{i.succ!}"] = record&lt;br /&gt;            hash&lt;br /&gt;          }.to_yaml&lt;br /&gt;        end&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After making changes to the database that you'd like to dump to fixtures:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:fixtures:dump&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;After checking out updated fixtures from SVN:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;rake db:migrate&lt;br /&gt;rake db:fixtures:load&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 31 Jan 2007 01:04:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3393</guid>
      <author>jnewland (Jesse Newland)</author>
    </item>
  </channel>
</rss>
