Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord Schema Dump without rails

Tags:

ruby

firebird

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.

like image 399
Chris Valentine Avatar asked Aug 15 '14 15:08

Chris Valentine


Video Answer


1 Answers

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
like image 119
Patrick Oscity Avatar answered Sep 24 '22 00:09

Patrick Oscity