Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 ?

like image 360
Pravin Avatar asked Jan 15 '11 07:01

Pravin


People also ask

How do I create a schema in SQL query?

Right-click the Security folder, point to New, and select Schema. In the Schema - New dialog box, on the General page, enter a name for the new schema in the Schema name box. In the Schema owner box, enter the name of a database user or role to own the schema.

How do I create a staging schema in SQL Server?

Creating a new SCHEMA in databaseGo to Database > expand Security Folder > Next, expand Schemas Folder there you will see a schema newly created schema Staging. Using SQL Query: Following Statement returns all the list of available schema in database Prod_Db. You can see, Staging schema is also there in result.

Is create schema same as database?

As the query is written to create the database, similarly the query can be written to create the schema. Logical structure can be used by the schema to store data while memory component can be used by the database to store data. Also, a schema is collection of tables while a database is a collection of schema.

Does SQL Server support schemas?

By default, SQL Server uses [dbo] schema for all objects in a database. We can query SCHEMA_NAME() to get the default schema for the connected user.


2 Answers

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.

like image 186
Arsen7 Avatar answered Sep 17 '22 13:09

Arsen7


I'm using rails 2.3.5 but this may apply to 3.0 as well:

rake db:structure:dump does the trick for me.

like image 41
Wojtek Kruszewski Avatar answered Sep 20 '22 13:09

Wojtek Kruszewski