Neil wrote:
I've been learning/developing with MySQL, and I've also taken up using
Git. Firstly, am I right in presuming that I could rely on Git to
version a database if I'm using sqlite3, i.e. in a file within the
Rails app folder?
Git must be physically capable of storing a binary file. However...
The only way you should ever consider versioning a Rails database is with
migrations. You should be able to delete any database, reconstitute it with
rake db:migrate, and pass all tests at any time.
At my day-job, our CruiseControl.rb installation does that each time we
integrate.
If so, can I easily export the MySQL database and all the dev data and
import it straight in to sqlite3 without too much difficulty? Someone
suggested edge Rails has dbconsole, but I don't really want to deal
with moving to edge Rails on my first proper app. Do I have any other
options for this?
The data in your production database should be sacred, and stored on the
actual production server (with its own backups and such).
Your development and test environments should use test/fixtures/*.yml files.
Use this rake task to build a set of fixtures, if you don't have one yet -
and remember to delete all the records you don't need from your development
database first.
namespace :test do
desc 'Create YAML test fixtures from data in an existing database.
Defaults to development database. Set RAILS_ENV to override.'
task :extract_fixtures => :environment do
sql = "SELECT * FROM %s"
skip_tables = ["schema_info"]
ActiveRecord::Base.establish_connection
(ActiveRecord::Base.connection.tables - skip_tables).each do
table_name|
i = "000"
File.open("#{RAILS_ROOT}/tmp/#{table_name}.yml", 'w') do |file|
data = ActiveRecord::Base.connection.select_all(sql % table_name)
file.write data.inject({}) { |hash, record|
hash["#{table_name}_#{i.succ!}"] = record
hash
}.to_yaml
end
end
end
end
Don't be fooled by imitations. Some versions of test:extract_fixtures will
overwrite your test/fixtures folder. That is absurd, because you should
already have fixtures in there.