5

In rails you can setup a rails app, assign the right db driver (I need firebird/fb) and then do a rake db:schema:dump pretty much out of the box.

I'm trying to do a version control for my database schema. How can I just make a ruby script that requires activerecord and fb libraries and achieve the same thing. I dont' need an entire rails app. All I want is a consistent script to extract the schema.

2 Answers 2

9

Looking at the source of the db:schema:dump task, the following code should get you started:

require 'active_record'
require 'active_record/schema_dumper'
require 'activerecord-fb-adapter'

filename = './schema.rb'

ActiveRecord::Base.establish_connection(
  adapter: 'fb',
  database: 'db/development.fdb',
  username: 'SYSDBA',
  password: 'masterkey',
  host: 'localhost',
  encoding: 'UTF-8',
  create: true
)

File.open(filename, "w:utf-8") do |file|
  ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
end
3
  • thanks i'm still new to ruby, will try this out ASAP :) Aug 15, 2014 at 15:48
  • wouldnt work on mac os x, but i think its a mac error, pretty sure this is the sound answer. thanks Aug 15, 2014 at 18:09
  • This turned out to be an even simpler solution. firebirdnews.org/… Aug 15, 2014 at 18:40
0

I just happen to have been working on something similar except for the firebird/fb part.

require "yaml"
require "active_record"

include ActiveRecord
include ActiveRecord::Tasks

ActiveRecord::Migrator.migrations_path='./db/migrate'

DatabaseTasks.db_dir = './db'

db_config_file = "./config/database.yml"
db_config = YAML.load_file(db_config_file)
db_type = 'development'
db_object = db_config[db_type]

@sldbtask = SQLiteDatabaseTasks.new(db_object, './')

unless File.exist?(db_object['database'])
  @sldbtask.create
  @sldbtask.connection
  # try different migation versions
  migration_version = 0
  ActiveRecord::Migrator.run(:up, ActiveRecord::Migrator.migrations_path, migration_version)
end 

unless File.exist?('./db/schema.rb')
  #DatabaseTasks.check_schema_file('./db/schema.rb')
  File.open('./db/schema.rb', "w:utf-8") do |file|
    @sldbtask.establish_connection(db_object)
    ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, file)
  end
end

Of course my adapter is different but it could be modified and it's versatile with a Rails app so you could drop the code part into the root directory. But of course you can rake for that but this just shows that it works. Tou might nave problems with the migration version though. I haven't thoroughly tested it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.