Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6040527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T06:31:36+00:00 2026-05-23T06:31:36+00:00

I want to create schema.sql instead of schema.rb. After googling around I found that

  • 0

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 ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T06:31:36+00:00Added an answer on May 23, 2026 at 6:31 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create XML Schema which can preserve formatted text Like, Line break
I want to create a schema in SQL Server database. There are also bunch
I want to create a Stored procedure and Job in Ms SQL server that
I have a sql schema dump from phpmyadmin. I want to create a new
I want to create a schema with with a name passed by variable. Example:
I want create a drop shadow around the canvas component in flex. Technically speaking
i want create image animation , i have 50 images with png format now
I am trying ebook reading app. In that I want create UIActionSheet by clicking
i have a table in sql that uses the adjacency model to create child/parent
I want to know if it's possible to create MySQL Schema from database diagram

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.