schema.sql not creating even after setting schema_format = :sql

Pravin picture Pravin · Jan 15, 2011 · Viewed 9k times · Source

I want to create schema.sql instead of schema.rb. After googling around I found that it can be done by setting sql schema format in application.rb. So I set following in application.rb

config.active_record.schema_format = :sql

But if I set schema_format to :sql, schema.rb/schema.sql is not created at all. If I comment the line above it creates schema.rb but I need schema.sql. I am assuming that it will have database structure dumped in it and I know that the database structure can be dumped using

rake db:structure:dump 

But I want it to be done automatically when database is migrated.

Is there anything I am missing or assuming wrong ?

Answer

Arsen7 picture Arsen7 · Jun 20, 2011

Five months after the original question the problem still exists. The answer is that you did everything correctly, but there is a bug in Rails.

Even in the guides it looks like all you need is to change the format from :ruby to :sql, but the migrate task is defined like this (activerecord/lib/active_record/railties/databases.rake line 155):

task :migrate => [:environment, :load_config] do
  ActiveRecord::Migration.verbose = ENV["VERBOSE"] ? ENV["VERBOSE"] == "true" : true
  ActiveRecord::Migrator.migrate(ActiveRecord::Migrator.migrations_paths, ENV["VERSION"] ? ENV["VERSION"].to_i : nil)
  db_namespace["schema:dump"].invoke if ActiveRecord::Base.schema_format == :ruby
end

As you can see, nothing happens unless the schema_format equals :ruby. Automatic dumping of the schema in SQL format was working in Rails 1.x. Something has changed in Rails 2, and has not been fixed.

The problem is that even if you manage to create the schema in SQL format, there is no task to load this into the database, and the task rake db:setup will ignore your database structure.

The bug has been noticed recently: https://github.com/rails/rails/issues/715 (and issues/715), and there is a patch at https://gist.github.com/971720

You may want to wait until the patch is applied to Rails (the edge version still has this bug), or apply the patch yourself (you may need to do it manually, since line numbers have changed a little).


Workaround:

With bundler it's relatively hard to patch the libraries (upgrades are so easy, that they are done very often and the paths are polluted with strange numbers - at least if you use edge rails ;-), so, instead of patching the file directly, you may want to create two files in your lib/tasks folder:

lib/tasks/schema_format.rake:

import File.expand_path(File.dirname(__FILE__)+"/schema_format.rb")

# Loads the *_structure.sql file into current environment's database.
# This is a slightly modified copy of the 'test:clone_structure' task.
def db_load_structure(filename)
  abcs = ActiveRecord::Base.configurations
  case abcs[Rails.env]['adapter']
  when /mysql/
    ActiveRecord::Base.establish_connection(Rails.env)
    ActiveRecord::Base.connection.execute('SET foreign_key_checks = 0')
    IO.readlines(filename).join.split("\n\n").each do |table|
      ActiveRecord::Base.connection.execute(table)
    end
  when /postgresql/
    ENV['PGHOST']     = abcs[Rails.env]['host'] if abcs[Rails.env]['host']
    ENV['PGPORT']     = abcs[Rails.env]['port'].to_s if abcs[Rails.env]['port']
    ENV['PGPASSWORD'] = abcs[Rails.env]['password'].to_s if abcs[Rails.env]['password']
    `psql -U "#{abcs[Rails.env]['username']}" -f #{filename} #{abcs[Rails.env]['database']} #{abcs[Rails.env]['template']}`
  when /sqlite/
    dbfile = abcs[Rails.env]['database'] || abcs[Rails.env]['dbfile']
    `sqlite3 #{dbfile} < #{filename}`
  when 'sqlserver'
    `osql -E -S #{abcs[Rails.env]['host']} -d #{abcs[Rails.env]['database']} -i #{filename}`
    # There was a relative path. Is that important? : db\\#{Rails.env}_structure.sql`
  when 'oci', 'oracle'
    ActiveRecord::Base.establish_connection(Rails.env)
    IO.readlines(filename).join.split(";\n\n").each do |ddl|
      ActiveRecord::Base.connection.execute(ddl)
    end
  when 'firebird'
    set_firebird_env(abcs[Rails.env])
    db_string = firebird_db_string(abcs[Rails.env])
    sh "isql -i #{filename} #{db_string}"
  else
    raise "Task not supported by '#{abcs[Rails.env]['adapter']}'"
  end
end

namespace :db do
  namespace :structure do
    desc "Load development_structure.sql file into the current environment's database"
    task :load => :environment do
      file_env = 'development' # From which environment you want the structure?
                               # You may use a parameter or define different tasks.
      db_load_structure "#{Rails.root}/db/#{file_env}_structure.sql"
    end
  end
end

and lib/tasks/schema_format.rb:

def dump_structure_if_sql
  Rake::Task['db:structure:dump'].invoke if ActiveRecord::Base.schema_format == :sql
end
Rake::Task['db:migrate'     ].enhance do dump_structure_if_sql end
Rake::Task['db:migrate:up'  ].enhance do dump_structure_if_sql end
Rake::Task['db:migrate:down'].enhance do dump_structure_if_sql end
Rake::Task['db:rollback'    ].enhance do dump_structure_if_sql end
Rake::Task['db:forward'     ].enhance do dump_structure_if_sql end

Rake::Task['db:structure:dump'].enhance do
  # If not reenabled, then in db:migrate:redo task the dump would be called only once,
  # and would contain only the state after the down-migration.
  Rake::Task['db:structure:dump'].reenable
end 

# The 'db:setup' task needs to be rewritten.
Rake::Task['db:setup'].clear.enhance(['environment']) do # see the .clear method invoked?
  Rake::Task['db:create'].invoke
  Rake::Task['db:schema:load'].invoke if ActiveRecord::Base.schema_format == :ruby
  Rake::Task['db:structure:load'].invoke if ActiveRecord::Base.schema_format == :sql
  Rake::Task['db:seed'].invoke
end 

Having these files, you have monkeypatched rake tasks, and you still can easily upgrade Rails. Of course, you should monitor the changes introduced in the file activerecord/lib/active_record/railties/databases.rake and decide whether the modifications are still necessary.