diff --git a/activerecord/CHANGELOG b/activerecord/CHANGELOG index fddfb5ef0b3cf5bf25a815b05ef822f11cf11b5e..dafc6a05e160aba616c4f8748ddc8ee12e9107b1 100644 --- a/activerecord/CHANGELOG +++ b/activerecord/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added automatic dropping/creating of test tables for running the unit tests on all databases #587 [adelle@bullet.net.au] + * Fixed that find_by_* would fail when column names had numbers #670 [demetrius] * Fixed the SQL Server adapter on a bunch of issues #667 [DeLynn] diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index 6a9ca8fcf10072c7b0d3d23fc2200867ad0ac356..5d2c764afed103099e64e0e54d34802a50a3745f 100755 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -356,6 +356,11 @@ def quote_column_name(name) name end + # Returns the human-readable name of the adapter. Use mixed case - one can always use downcase if needed. + def adapter_name() + 'Abstract' + end + # Returns a string of the CREATE TABLE SQL statements for recreating the entire structure of the database. def structure_dump() end diff --git a/activerecord/lib/active_record/connection_adapters/db2_adapter.rb b/activerecord/lib/active_record/connection_adapters/db2_adapter.rb index 867b17eaa0b971d26797bb0807a6e42d8ddcee84..225ade04416973f56a61ef549785c20aad7f094e 100644 --- a/activerecord/lib/active_record/connection_adapters/db2_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/db2_adapter.rb @@ -74,6 +74,10 @@ def rollback_db_transaction def quote_column_name(name) name; end + def adapter_name() + 'DB2' + end + def quote_string(s) s.gsub(/'/, "''") # ' (for ruby-mode) end diff --git a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb index 645e5f6dd4b21c35174e759fda082b8218c195d9..c637a50c5af862854836c96787706933f03df01e 100755 --- a/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/mysql_adapter.rb @@ -102,7 +102,11 @@ def rollback_db_transaction def quote_column_name(name) return "`#{name}`" end - + + def adapter_name() + 'MySQL' + end + def structure_dump select_all("SHOW TABLES").inject("") do |structure, table| structure += select_one("SHOW CREATE TABLE #{table.to_a.first.last}")["Create Table"] + ";\n\n" diff --git a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb index ef939d747bc9d41079979e7de194b7ec4d4e7f9f..67b26739cac8e6144c2343f14034b0a8926a22d8 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql_adapter.rb @@ -89,6 +89,10 @@ def quote_column_name(name) return "\"#{name}\"" end + def adapter_name() + 'PostgreSQL' + end + private def last_insert_id(table, column = "id") sequence_name = "#{table}_#{column || 'id'}_seq" diff --git a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb index b8a91929c70f1b917c7f44473c3664582aace213..d8f3e04498de34ed496d182f79fb425a4e600ea3 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb @@ -141,6 +141,10 @@ def quote_column_name(name) return "'#{name}'" end + def adapter_name() + 'SQLite' + end + protected def table_structure(table_name) execute "PRAGMA table_info(#{table_name})" diff --git a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb index 84293736e35e89b5f5681350439b814d78469675..7bddba9b5b5976f0e9bc58eb7a730a99d09214cf 100644 --- a/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/sqlserver_adapter.rb @@ -43,6 +43,10 @@ def self.sqlserver_connection(config) raise ArgumentError, "No database specified. Missing argument: database." end + def adapter_name() + 'SqlServer' + end + conn = DBI.connect("DBI:ADO:Provider=SQLOLEDB;Data Source=#{host};Initial Catalog=#{database};User Id=#{username};Password=#{password};") conn["AutoCommit"] = true diff --git a/activerecord/test/aaa_create_tables_test.rb b/activerecord/test/aaa_create_tables_test.rb new file mode 100644 index 0000000000000000000000000000000000000000..ba14199d312698551f5acb6c802ca18676d3500a --- /dev/null +++ b/activerecord/test/aaa_create_tables_test.rb @@ -0,0 +1,58 @@ +require 'abstract_unit' + +# The filename for this test begins with "aaa" so that +# it will be the first test. + +class SqlFile < File + #Define an iterator that iterates over the statements in a .sql file. + #statements are separated by a semicolon. + def initialize(path) + super(path) + end + + def each_statement() + statement = '' + each_line { |line| + #The last character of each line is a line-feed, so we will check the next-to-last character + #to see if it is a semicolon. A better way of doing this would be to look for a semicolon anywhere + #within the line in case multiple statements have been put on a single line. + #The last statement in the file must be followed by a line-feed. + if line.slice(-2,1)==';' then + statement = statement + line.slice(0,line.length-2) + "\n" + yield statement + statement = '' + else + statement = statement + line + end + } + end +end + +class CreateTablesTest < Test::Unit::TestCase + def setup + # This method is required by rake. + end + + def run_sql_file(connection, path) + sql_file = SqlFile.new(path) + sql_file.each_statement { |statement| + begin + #Skip errors. If there is a problem creating the tables then it will show up in other tests. + connection.execute(statement) + rescue ActiveRecord::StatementInvalid + end } + end + + def test_table_creation + adapter_name = ActiveRecord::Base.connection.adapter_name.downcase + run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/" + adapter_name + ".drop.sql" + run_sql_file ActiveRecord::Base.connection, "test/fixtures/db_definitions/" + adapter_name + ".sql" + + # Now do the same thing with the connection used by multiple_db_test.rb + adapter_name = Course.retrieve_connection.adapter_name.downcase + run_sql_file Course.retrieve_connection, "test/fixtures/db_definitions/" + adapter_name + "2.drop.sql" + run_sql_file Course.retrieve_connection, "test/fixtures/db_definitions/" + adapter_name + "2.sql" + + assert_equal 1,1 + end +end diff --git a/activerecord/test/fixtures/db_definitions/db2.drop.sql b/activerecord/test/fixtures/db_definitions/db2.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..1f611c8d5a5489298af47636d668d5c235d4d317 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/db2.drop.sql @@ -0,0 +1,18 @@ +DROP TABLE accounts; +DROP TABLE companies; +DROP TABLE topics; +DROP TABLE developers; +DROP TABLE projects; +DROP TABLE developers_projects; +DROP TABLE customers; +DROP TABLE movies; +DROP TABLE subscribers; +DROP TABLE booleantests; +DROP TABLE auto_id_tests; +DROP TABLE entrants; +DROP TABLE colnametests; +DROP TABLE mixins; +DROP TABLE people; +DROP TABLE binaries; +DROP TABLE computers; + diff --git a/activerecord/test/fixtures/db_definitions/db2.sql b/activerecord/test/fixtures/db_definitions/db2.sql index 033efcb0887c2dc53e879febd86f83b8cf571a11..46e326d5047ebb3ff7e8743a43c1a6964dfad66e 100644 --- a/activerecord/test/fixtures/db_definitions/db2.sql +++ b/activerecord/test/fixtures/db_definitions/db2.sql @@ -127,3 +127,4 @@ CREATE TABLE computers ( id int generated by default as identity (start with +10000), developer int NOT NULL ); + diff --git a/activerecord/test/fixtures/db_definitions/db22.drop.sql b/activerecord/test/fixtures/db_definitions/db22.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..df00ffd7c9a1370ad581c4a4d3b386f38ffbf4f9 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/db22.drop.sql @@ -0,0 +1,2 @@ +DROP TABLE courses; + diff --git a/activerecord/test/fixtures/db_definitions/db22.sql b/activerecord/test/fixtures/db_definitions/db22.sql index dc4f9ed364f137cb0b6cac9c98676c1fa94fc46a..9198cf5f6e55334e01d181c1d69b1287417388bd 100644 --- a/activerecord/test/fixtures/db_definitions/db22.sql +++ b/activerecord/test/fixtures/db_definitions/db22.sql @@ -2,3 +2,4 @@ CREATE TABLE courses ( id int NOT NULL PRIMARY KEY, name varchar(255) NOT NULL ); + diff --git a/activerecord/test/fixtures/db_definitions/mysql.drop.sql b/activerecord/test/fixtures/db_definitions/mysql.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..1f611c8d5a5489298af47636d668d5c235d4d317 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/mysql.drop.sql @@ -0,0 +1,18 @@ +DROP TABLE accounts; +DROP TABLE companies; +DROP TABLE topics; +DROP TABLE developers; +DROP TABLE projects; +DROP TABLE developers_projects; +DROP TABLE customers; +DROP TABLE movies; +DROP TABLE subscribers; +DROP TABLE booleantests; +DROP TABLE auto_id_tests; +DROP TABLE entrants; +DROP TABLE colnametests; +DROP TABLE mixins; +DROP TABLE people; +DROP TABLE binaries; +DROP TABLE computers; + diff --git a/activerecord/test/fixtures/db_definitions/mysql.sql b/activerecord/test/fixtures/db_definitions/mysql.sql index 0137cd91d5ca2add403d90e2f6f133ab770a77be..1064373f7f441855a206ba9b4117b59128a71919 100755 --- a/activerecord/test/fixtures/db_definitions/mysql.sql +++ b/activerecord/test/fixtures/db_definitions/mysql.sql @@ -126,4 +126,5 @@ CREATE TABLE `binaries` ( CREATE TABLE `computers` ( `id` INTEGER NOT NULL PRIMARY KEY, `developer` INTEGER NOT NULL -); \ No newline at end of file +); + diff --git a/activerecord/test/fixtures/db_definitions/mysql2.drop.sql b/activerecord/test/fixtures/db_definitions/mysql2.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..df00ffd7c9a1370ad581c4a4d3b386f38ffbf4f9 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/mysql2.drop.sql @@ -0,0 +1,2 @@ +DROP TABLE courses; + diff --git a/activerecord/test/fixtures/db_definitions/mysql2.sql b/activerecord/test/fixtures/db_definitions/mysql2.sql index 0a16a0a2f9a69eaa2fa3bf8a8442f939f842ebbf..551e0ecc7d94e3721426cec286b6fd42b5196fbc 100644 --- a/activerecord/test/fixtures/db_definitions/mysql2.sql +++ b/activerecord/test/fixtures/db_definitions/mysql2.sql @@ -2,3 +2,4 @@ CREATE TABLE `courses` ( `id` INTEGER NOT NULL PRIMARY KEY, `name` VARCHAR(255) NOT NULL ); + diff --git a/activerecord/test/fixtures/db_definitions/postgresql.drop.sql b/activerecord/test/fixtures/db_definitions/postgresql.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..1f611c8d5a5489298af47636d668d5c235d4d317 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/postgresql.drop.sql @@ -0,0 +1,18 @@ +DROP TABLE accounts; +DROP TABLE companies; +DROP TABLE topics; +DROP TABLE developers; +DROP TABLE projects; +DROP TABLE developers_projects; +DROP TABLE customers; +DROP TABLE movies; +DROP TABLE subscribers; +DROP TABLE booleantests; +DROP TABLE auto_id_tests; +DROP TABLE entrants; +DROP TABLE colnametests; +DROP TABLE mixins; +DROP TABLE people; +DROP TABLE binaries; +DROP TABLE computers; + diff --git a/activerecord/test/fixtures/db_definitions/postgresql.sql b/activerecord/test/fixtures/db_definitions/postgresql.sql index 7664c5e6e0c06d13a4b84cad97fce8f34468b5f2..f779027286d76dc5323f1a60ec727359ec727a5b 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql.sql @@ -144,4 +144,5 @@ CREATE TABLE binaries ( CREATE TABLE computers ( id serial, developer integer NOT NULL -); \ No newline at end of file +); + diff --git a/activerecord/test/fixtures/db_definitions/postgresql2.drop.sql b/activerecord/test/fixtures/db_definitions/postgresql2.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..df00ffd7c9a1370ad581c4a4d3b386f38ffbf4f9 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/postgresql2.drop.sql @@ -0,0 +1,2 @@ +DROP TABLE courses; + diff --git a/activerecord/test/fixtures/db_definitions/postgresql2.sql b/activerecord/test/fixtures/db_definitions/postgresql2.sql index b58a45eff74351d0523f1b68064331ceea817c41..c0d7f79b04d1f8465dfc31d3624447754bb85d6e 100644 --- a/activerecord/test/fixtures/db_definitions/postgresql2.sql +++ b/activerecord/test/fixtures/db_definitions/postgresql2.sql @@ -1,4 +1,5 @@ CREATE TABLE courses ( id serial, name text -); \ No newline at end of file +); + diff --git a/activerecord/test/fixtures/db_definitions/sqlite.drop.sql b/activerecord/test/fixtures/db_definitions/sqlite.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..1f611c8d5a5489298af47636d668d5c235d4d317 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/sqlite.drop.sql @@ -0,0 +1,18 @@ +DROP TABLE accounts; +DROP TABLE companies; +DROP TABLE topics; +DROP TABLE developers; +DROP TABLE projects; +DROP TABLE developers_projects; +DROP TABLE customers; +DROP TABLE movies; +DROP TABLE subscribers; +DROP TABLE booleantests; +DROP TABLE auto_id_tests; +DROP TABLE entrants; +DROP TABLE colnametests; +DROP TABLE mixins; +DROP TABLE people; +DROP TABLE binaries; +DROP TABLE computers; + diff --git a/activerecord/test/fixtures/db_definitions/sqlite.sql b/activerecord/test/fixtures/db_definitions/sqlite.sql index 1b9a5ea932d433dd3fcc4765e8f1d821df19e45b..988973b0ffbabf0eecb0e5e1d673d52bc6dae5fe 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite.sql @@ -113,4 +113,5 @@ CREATE TABLE 'binaries' ( CREATE TABLE 'computers' ( 'id' INTEGER NOT NULL PRIMARY KEY, 'developer' INTEGER NOT NULL -); \ No newline at end of file +); + diff --git a/activerecord/test/fixtures/db_definitions/sqlite2.drop.sql b/activerecord/test/fixtures/db_definitions/sqlite2.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..df00ffd7c9a1370ad581c4a4d3b386f38ffbf4f9 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/sqlite2.drop.sql @@ -0,0 +1,2 @@ +DROP TABLE courses; + diff --git a/activerecord/test/fixtures/db_definitions/sqlite2.sql b/activerecord/test/fixtures/db_definitions/sqlite2.sql index 19b123968ae215b0443e0e445e1c75322332bccc..5c0d231bbcc1b294e113c79bac2d04cf4188b482 100644 --- a/activerecord/test/fixtures/db_definitions/sqlite2.sql +++ b/activerecord/test/fixtures/db_definitions/sqlite2.sql @@ -2,3 +2,4 @@ CREATE TABLE 'courses' ( 'id' INTEGER NOT NULL PRIMARY KEY, 'name' VARCHAR(255) NOT NULL ); + diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..1f611c8d5a5489298af47636d668d5c235d4d317 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/sqlserver.drop.sql @@ -0,0 +1,18 @@ +DROP TABLE accounts; +DROP TABLE companies; +DROP TABLE topics; +DROP TABLE developers; +DROP TABLE projects; +DROP TABLE developers_projects; +DROP TABLE customers; +DROP TABLE movies; +DROP TABLE subscribers; +DROP TABLE booleantests; +DROP TABLE auto_id_tests; +DROP TABLE entrants; +DROP TABLE colnametests; +DROP TABLE mixins; +DROP TABLE people; +DROP TABLE binaries; +DROP TABLE computers; + diff --git a/activerecord/test/fixtures/db_definitions/sqlserver.sql b/activerecord/test/fixtures/db_definitions/sqlserver.sql index 95106a7b18a0e9dca8a65f0d847bd39dbaf9e079..743a5383fef66f50bb0dfc2799d57920933bc16f 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver.sql @@ -127,3 +127,4 @@ CREATE TABLE computers ( developer int NOT NULL, PRIMARY KEY (id) ); + diff --git a/activerecord/test/fixtures/db_definitions/sqlserver2.drop.sql b/activerecord/test/fixtures/db_definitions/sqlserver2.drop.sql new file mode 100644 index 0000000000000000000000000000000000000000..df00ffd7c9a1370ad581c4a4d3b386f38ffbf4f9 --- /dev/null +++ b/activerecord/test/fixtures/db_definitions/sqlserver2.drop.sql @@ -0,0 +1,2 @@ +DROP TABLE courses; + diff --git a/activerecord/test/fixtures/db_definitions/sqlserver2.sql b/activerecord/test/fixtures/db_definitions/sqlserver2.sql index dc4f9ed364f137cb0b6cac9c98676c1fa94fc46a..9198cf5f6e55334e01d181c1d69b1287417388bd 100644 --- a/activerecord/test/fixtures/db_definitions/sqlserver2.sql +++ b/activerecord/test/fixtures/db_definitions/sqlserver2.sql @@ -2,3 +2,4 @@ CREATE TABLE courses ( id int NOT NULL PRIMARY KEY, name varchar(255) NOT NULL ); +