Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Oracle: create_table takes a :sequence_name option to override the 't…

…ablename_seq' default. Closes #7000.

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5933 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit e3103441114b1c5e7b67b71efa9c0b44e03645e6 1 parent 9ccb12a
@jeremy jeremy authored
View
2  activerecord/CHANGELOG
@@ -1,5 +1,7 @@
*SVN*
+* Oracle: create_table takes a :sequence_name option to override the 'tablename_seq' default. #7000 [Michael Schoen]
+
* MySQL: retain SSL settings on reconnect. #6976 [randyv2]
* Apply scoping during initialize instead of create. Fixes setting of foreign key when using find_or_initialize_by with scoping. [Cody Fauser]
View
4 activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -94,7 +94,7 @@ def create_table(name, options = {})
yield table_definition
if options[:force]
- drop_table(name) rescue nil
+ drop_table(name, options) rescue nil
end
create_sql = "CREATE#{' TEMPORARY' if options[:temporary]} TABLE "
@@ -112,7 +112,7 @@ def rename_table(name, new_name)
end
# Drops a table from the database.
- def drop_table(name)
+ def drop_table(name, options = {})
execute "DROP TABLE #{name}"
end
View
2  activerecord/lib/active_record/connection_adapters/frontbase_adapter.rb
@@ -778,7 +778,7 @@ def rename_table(name, new_name)
end
# Drops a table from the database.
- def drop_table(name)
+ def drop_table(name, options = {})
execute "DROP TABLE #{name} RESTRICT"
rescue ActiveRecord::StatementInvalid => e
raise e unless e.message.match(/Referenced TABLE - \w* - does not exist/)
View
8 activerecord/lib/active_record/connection_adapters/oracle_adapter.rb
@@ -351,7 +351,8 @@ def columns(table_name, name = nil) #:nodoc:
def create_table(name, options = {}) #:nodoc:
super(name, options)
- execute "CREATE SEQUENCE #{name}_seq START WITH 10000" unless options[:id] == false
+ seq_name = options[:sequence_name] || "#{name}_seq"
+ execute "CREATE SEQUENCE #{seq_name} START WITH 10000" unless options[:id] == false
end
def rename_table(name, new_name) #:nodoc:
@@ -359,9 +360,10 @@ def rename_table(name, new_name) #:nodoc:
execute "RENAME #{name}_seq TO #{new_name}_seq" rescue nil
end
- def drop_table(name) #:nodoc:
+ def drop_table(name, options = {}) #:nodoc:
super(name)
- execute "DROP SEQUENCE #{name}_seq" rescue nil
+ seq_name = options[:sequence_name] || "#{name}_seq"
+ execute "DROP SEQUENCE #{seq_name}" rescue nil
end
def remove_index(table_name, options = {}) #:nodoc:
View
2  activerecord/test/defaults_test.rb
@@ -46,7 +46,7 @@ def test_mysql_integer_not_null_defaults
end
end
- if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter, :FirebirdAdapter, :OpenBaseAdapter)
+ if current_adapter?(:PostgreSQLAdapter, :SQLServerAdapter, :FirebirdAdapter, :OpenBaseAdapter, :OracleAdapter)
def test_default_integers
default = Default.new
assert_instance_of Fixnum, default.positive_integer
View
17 activerecord/test/fixtures/db_definitions/oracle.sql
@@ -137,6 +137,23 @@ create table booleantests (
);
create sequence booleantests_seq minvalue 10000;
+CREATE TABLE defaults (
+ id integer not null,
+ modified_date date default sysdate,
+ modified_date_function date default sysdate,
+ fixed_date date default to_date('2004-01-01', 'YYYY-MM-DD'),
+ modified_time date default sysdate,
+ modified_time_function date default sysdate,
+ fixed_time date default TO_DATE('2004-01-01 00:00:00', 'YYYY-MM-DD HH24:MI:SS'),
+ char1 varchar2(1) default 'Y',
+ char2 varchar2(50) default 'a varchar field',
+ char3 clob default 'a text field',
+ positive_integer integer default 1,
+ negative_integer integer default -1,
+ decimal_number number(3,2) default 2.78
+);
+create sequence defaults_seq minvalue 10000;
+
create table auto_id_tests (
auto_id integer not null,
value integer default null,
View
39 activerecord/test/migration_test.rb
@@ -700,6 +700,43 @@ def test_migrator_with_missing_version_numbers
assert_equal 2, ActiveRecord::Migrator.current_version
end
-
+ def test_create_table_with_custom_sequence_name
+ return unless current_adapter? :OracleAdapter
+
+ # table name is 29 chars, the standard sequence name will
+ # be 33 chars and fail
+ assert_raises(ActiveRecord::StatementInvalid) do
+ begin
+ Person.connection.create_table :table_with_name_thats_just_ok do |t|
+ t.column :foo, :string, :null => false
+ end
+ ensure
+ Person.connection.drop_table :table_with_name_thats_just_ok rescue nil
+ end
+ end
+
+ # should be all good w/ a custom sequence name
+ assert_nothing_raised do
+ begin
+ Person.connection.create_table :table_with_name_thats_just_ok,
+ :sequence_name => 'suitably_short_seq' do |t|
+ t.column :foo, :string, :null => false
+ end
+
+ Person.connection.execute("select suitably_short_seq.nextval from dual")
+
+ ensure
+ Person.connection.drop_table :table_with_name_thats_just_ok,
+ :sequence_name => 'suitably_short_seq' rescue nil
+ end
+ end
+
+ # confirm the custom sequence got dropped
+ assert_raises(ActiveRecord::StatementInvalid) do
+ Person.connection.execute("select suitably_short_seq.nextval from dual")
+ end
+ end
+
end
end
+
Please sign in to comment.
Something went wrong with that request. Please try again.