Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ActiveRecord::SchemaDumper for dumping a DB schema to a pure-ruby…
… file, making it easier to consolidate large migration lists and port database schemas between databases. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@2312 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
- Loading branch information
Showing
11 changed files
with
292 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,23 @@ | |||
module ActiveRecord | |||
|
|||
class Schema < Migration #:nodoc: | |||
private_class_method :new | |||
|
|||
def self.define(info={}, &block) | |||
instance_eval(&block) | |||
|
|||
unless info.empty? | |||
initialize_schema_information | |||
cols = columns('schema_info') | |||
|
|||
info = info.map do |k,v| | |||
v = quote(v, cols.detect { |c| c.name == k.to_s }) | |||
"#{k} = #{v}" | |||
end | |||
|
|||
update "UPDATE schema_info SET #{info.join(", ")}" | |||
end | |||
end | |||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,87 @@ | |||
module ActiveRecord | |||
|
|||
# This class is used to dump the database schema for some connection to some | |||
# output format (i.e., ActiveRecord::Schema). | |||
class SchemaDumper | |||
private_class_method :new | |||
|
|||
def self.dump(connection=ActiveRecord::Base.connection, stream=STDOUT) | |||
new(connection).dump(stream) | |||
stream | |||
end | |||
|
|||
def dump(stream) | |||
header(stream) | |||
tables(stream) | |||
trailer(stream) | |||
stream | |||
end | |||
|
|||
private | |||
|
|||
def initialize(connection) | |||
@connection = connection | |||
@types = @connection.native_database_types | |||
@info = @connection.select_one("SELECT * FROM schema_info") rescue nil | |||
end | |||
|
|||
def header(stream) | |||
define_params = @info ? ":version => #{@info['version']}" : "" | |||
|
|||
stream.puts <<HEADER | |||
# This file is autogenerated. Instead of editing this file, please use the | |||
# migrations feature of ActiveRecord to incrementally modify your database, and | |||
# then regenerate this schema definition. | |||
require 'active_record/schema' | |||
ActiveRecord::Schema.define(#{define_params}) do | |||
HEADER | |||
end | |||
|
|||
def trailer(stream) | |||
stream.puts "end" | |||
end | |||
|
|||
def tables(stream) | |||
@connection.tables.sort.each do |tbl| | |||
next if tbl == "schema_info" | |||
table(tbl, stream) | |||
end | |||
end | |||
|
|||
def table(table, stream) | |||
columns = @connection.columns(table) | |||
|
|||
stream.print " create_table #{table.inspect}" | |||
stream.print ", :id => false" if !columns.detect { |c| c.name == "id" } | |||
stream.puts " do |t|" | |||
|
|||
columns.each do |column| | |||
next if column.name == "id" | |||
stream.print " t.column #{column.name.inspect}, #{column.type.inspect}" | |||
stream.print ", :limit => #{column.limit.inspect}" if column.limit != @types[column.type][:limit] | |||
stream.print ", :default => #{column.default.inspect}" if column.default | |||
stream.print ", :null => false" if !column.null | |||
stream.puts | |||
end | |||
|
|||
stream.puts " end" | |||
stream.puts | |||
|
|||
indexes(table, stream) | |||
end | |||
|
|||
def indexes(table, stream) | |||
indexes = @connection.indexes(table) | |||
indexes.each do |index| | |||
stream.print " add_index #{index.table.inspect}, #{index.columns.inspect}, :name => #{index.name.inspect}" | |||
stream.print ", :unique => true" if index.unique | |||
stream.puts | |||
end | |||
stream.puts unless indexes.empty? | |||
end | |||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,38 @@ | |||
require 'abstract_unit' | |||
|
|||
class AdapterTest < Test::Unit::TestCase | |||
def setup | |||
@connection = ActiveRecord::Base.connection | |||
end | |||
|
|||
def test_tables | |||
if @connection.respond_to?(:tables) | |||
tables = @connection.tables | |||
assert tables.include?("accounts") | |||
assert tables.include?("authors") | |||
assert tables.include?("tasks") | |||
assert tables.include?("topics") | |||
else | |||
warn "#{@connection.class} does not respond to #tables" | |||
end | |||
end | |||
|
|||
def test_indexes | |||
if @connection.respond_to?(:indexes) | |||
indexes = @connection.indexes("accounts") | |||
assert indexes.empty? | |||
|
|||
@connection.add_index :accounts, :firm_id | |||
indexes = @connection.indexes("accounts") | |||
assert_equal "accounts", indexes.first.table | |||
assert_equal "accounts_firm_id_index", indexes.first.name | |||
assert !indexes.first.unique | |||
assert_equal ["firm_id"], indexes.first.columns | |||
else | |||
warn "#{@connection.class} does not respond to #indexes" | |||
end | |||
|
|||
ensure | |||
@connection.remove_index :accounts, :firm_id rescue nil | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,31 @@ | |||
require 'abstract_unit' | |||
require "#{File.dirname(__FILE__)}/../lib/active_record/schema" | |||
|
|||
if ActiveRecord::Base.connection.supports_migrations? | |||
|
|||
class ActiveRecordSchemaTest < Test::Unit::TestCase | |||
def setup | |||
@connection = ActiveRecord::Base.connection | |||
end | |||
|
|||
def teardown | |||
@connection.drop_table :fruits rescue nil | |||
end | |||
|
|||
def test_schema_define | |||
ActiveRecord::Schema.define(:version => 7) do | |||
create_table :fruits do |t| | |||
t.column :color, :string | |||
t.column :size, :string | |||
t.column :texture, :string | |||
t.column :flavor, :string | |||
end | |||
end | |||
|
|||
assert_nothing_raised { @connection.select_all "SELECT * FROM fruits" } | |||
assert_nothing_raised { @connection.select_all "SELECT * FROM schema_info" } | |||
assert_equal 7, @connection.select_one("SELECT version FROM schema_info")['version'].to_i | |||
end | |||
end | |||
|
|||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.