Ian Vaughan
Ian Vaughan

Reputation: 21381

Creating a table from a schema? Using Ruby Sequel gem

I can dump the current Database#schema, but I'd like to create a new table (in a different database) with that schema, but I dont see how this can be done?

DB = Sequel.connect('sqlite:///database_from.db')
schema = DB.schema :table
# schema => [[:id, {:auto_increment=>true, :allow_null=>false, :default=>nil, :primary_key=>true, :db_type=>"smallint(5) unsigned", :type=>:integer, :ruby_default=>nil}], [:field, {:allow_null=>true, :default=>nil, :primary_key=>false, :db_type=>"smallint(5) unsigned", :type=>:integer, :ruby_default=>nil}]]

DB2 = Sequel.connect('sqlite:///database_to.db')
DB2.create_table('table name', schema) #< allowing this would be cool!

Upvotes: 4

Views: 1896

Answers (1)

Ian Vaughan
Ian Vaughan

Reputation: 21381

One way it can be done via Migrations

  1. Dump the copy from database :-

    sequel -d mysql://root@localhost/database1 > db/001_test.rb

  2. Edit to only include the table required.

  3. Run the migration into the new db :-

    sequel -m db/ mysql://root@localhost/database2

Upvotes: 2

Related Questions