Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From any controller, you can define the following method.</p> <pre><code> def copy_template_database template_name = "customerdb1" # Database to copy from new_name = "temp" #database to create &amp; copy to #connect to template database to copy. Note that this will override any previous #connections for all Models that inherit from ActiveRecord::Base ActiveRecord::Base.establish_connection({:adapter =&gt; "mysql", :database =&gt; template_name, :host =&gt; "olddev", :username =&gt; "root", :password =&gt; "password" }) sql_connection = ActiveRecord::Base.connection sql_connection.execute("CREATE DATABASE #{new_name} CHARACTER SET latin1 COLLATE latin1_general_ci") tables = sql_connection.select_all("Show Tables") #the results are an array of hashes, ie: # [{"table_from_customerdb1" =&gt; "customers"},{"table_from_customerdb1" =&gt; "employees},...] table_names = Array.new tables.each { |hash| hash.each_value { |name| table_names &lt;&lt; name }} table_names.each { |name| sql_connection.execute("CREATE TABLE #{new_name}.#{name} LIKE #{template_name}.#{name}") sql_connection.execute("INSERT INTO #{new_name}.#{name} SELECT * FROM #{template_name}.#{name}") } #This statement is optional. It connects ActiveRecord to the new database ActiveRecord::Base.establish_connection({:adapter =&gt; "mysql", :database =&gt; new_name, :host =&gt; "olddev", :username =&gt; "root", :password =&gt; "password" }) end </code></pre> <p>Note that I do not know for sure if this will keep foriegn key integrity. I think it depends a lot on how the template Database is created.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload