Skip to content

Commit

Permalink
Merge [5514] from trunk.
Browse files Browse the repository at this point in the history
git-svn-id: http://svn-commit.rubyonrails.org/rails/branches/1-2-pre-release@5515 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information
jeremy committed Nov 13, 2006
1 parent 35240ba commit 86bd0da
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
2 changes: 2 additions & 0 deletions activerecord/CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
*SVN*

* Oracle: automatically detect the primary key. #6594 [vesaria, Michael Schoen]

* Oracle: to increase performance, prefetch 100 rows and enable similar cursor sharing. Both are configurable in database.yml. #6607 [philbogle@gmail.com, Michael Schoen]

* Cache inheritance_column. #6592 [Stefan Kaes]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,8 @@ def indexes(table_name, name = nil) #:nodoc:

def columns(table_name, name = nil) #:nodoc:
(owner, table_name) = @connection.describe(table_name)
raise "Could not describe #{table_name}. Does it exist?" unless owner and table_name

table_cols = %Q{
table_cols = <<-SQL
select column_name as name, data_type as sql_type, data_default, nullable,
decode(data_type, 'NUMBER', data_precision,
'FLOAT', data_precision,
Expand All @@ -327,7 +326,7 @@ def columns(table_name, name = nil) #:nodoc:
where owner = '#{owner}'
and table_name = '#{table_name}'
order by column_id
}
SQL

select_all(table_cols, name).map do |row|
limit, scale = row['limit'], row['scale']
Expand Down Expand Up @@ -386,6 +385,25 @@ def remove_column(table_name, column_name) #:nodoc:
execute "ALTER TABLE #{table_name} DROP COLUMN #{column_name}"
end

# Find a table's primary key and sequence.
# *Note*: Only primary key is implemented - sequence will be nil.
def pk_and_sequence_for(table_name)
(owner, table_name) = @connection.describe(table_name)

pks = select_values(<<-SQL, 'Primary Key')
select cc.column_name
from all_constraints c, all_cons_columns cc
where c.owner = '#{owner}'
and c.table_name = '#{table_name}'
and c.constraint_type = 'P'
and cc.owner = c.owner
and cc.constraint_name = c.constraint_name
SQL

# only support single column keys
pks.size == 1 ? [oracle_downcase(pks.first), nil] : nil
end

def structure_dump #:nodoc:
s = select_all("select sequence_name from user_sequences").inject("") do |structure, seq|
structure << "create sequence #{seq.to_a.first.last};\n\n"
Expand Down Expand Up @@ -531,7 +549,7 @@ def define_a_column(i)
def describe(name)
@desc ||= @@env.alloc(OCIDescribe)
@desc.attrSet(OCI_ATTR_DESC_PUBLIC, -1) if VERSION >= '0.1.14'
@desc.describeAny(@svc, name.to_s, OCI_PTYPE_UNK) rescue return nil
@desc.describeAny(@svc, name.to_s, OCI_PTYPE_UNK) rescue raise %Q{"DESC #{name}" failed; does it exist?}
info = @desc.attrGet(OCI_ATTR_PARAM)

case info.attrGet(OCI_ATTR_PTYPE)
Expand All @@ -543,6 +561,7 @@ def describe(name)
schema = info.attrGet(OCI_ATTR_SCHEMA_NAME)
name = info.attrGet(OCI_ATTR_NAME)
describe(schema + '.' + name)
else raise %Q{"DESC #{name}" failed; not a table or view.}
end
end

Expand Down
2 changes: 1 addition & 1 deletion activerecord/test/schema_dumper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def test_no_dump_errors
def test_schema_dump_includes_not_null_columns
stream = StringIO.new

ActiveRecord::SchemaDumper.ignore_tables = [/^[^s]/]
ActiveRecord::SchemaDumper.ignore_tables = [/^[^r]/]
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
output = stream.string
assert_match %r{:null => false}, output
Expand Down

0 comments on commit 86bd0da

Please sign in to comment.