mirror of
https://github.com/github/rails.git
synced 2026-04-26 03:00:59 -04:00
Added automatic dropping/creating of test tables for running the unit tests on all databases #587 [adelle@bullet.net.au]
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@719 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
@@ -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]
|
||||
|
||||
@@ -356,6 +356,11 @@ module ActiveRecord
|
||||
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
|
||||
|
||||
|
||||
@@ -74,6 +74,10 @@ begin
|
||||
|
||||
def quote_column_name(name) name; end
|
||||
|
||||
def adapter_name()
|
||||
'DB2'
|
||||
end
|
||||
|
||||
def quote_string(s)
|
||||
s.gsub(/'/, "''") # ' (for ruby-mode)
|
||||
end
|
||||
|
||||
@@ -102,7 +102,11 @@ module ActiveRecord
|
||||
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"
|
||||
|
||||
@@ -89,6 +89,10 @@ module ActiveRecord
|
||||
return "\"#{name}\""
|
||||
end
|
||||
|
||||
def adapter_name()
|
||||
'PostgreSQL'
|
||||
end
|
||||
|
||||
private
|
||||
def last_insert_id(table, column = "id")
|
||||
sequence_name = "#{table}_#{column || 'id'}_seq"
|
||||
|
||||
@@ -141,6 +141,10 @@ module ActiveRecord
|
||||
return "'#{name}'"
|
||||
end
|
||||
|
||||
def adapter_name()
|
||||
'SQLite'
|
||||
end
|
||||
|
||||
protected
|
||||
def table_structure(table_name)
|
||||
execute "PRAGMA table_info(#{table_name})"
|
||||
|
||||
@@ -43,6 +43,10 @@ module ActiveRecord
|
||||
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
|
||||
|
||||
|
||||
58
activerecord/test/aaa_create_tables_test.rb
Normal file
58
activerecord/test/aaa_create_tables_test.rb
Normal file
@@ -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
|
||||
18
activerecord/test/fixtures/db_definitions/db2.drop.sql
vendored
Normal file
18
activerecord/test/fixtures/db_definitions/db2.drop.sql
vendored
Normal file
@@ -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;
|
||||
|
||||
@@ -127,3 +127,4 @@ CREATE TABLE computers (
|
||||
id int generated by default as identity (start with +10000),
|
||||
developer int NOT NULL
|
||||
);
|
||||
|
||||
|
||||
2
activerecord/test/fixtures/db_definitions/db22.drop.sql
vendored
Normal file
2
activerecord/test/fixtures/db_definitions/db22.drop.sql
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
DROP TABLE courses;
|
||||
|
||||
@@ -2,3 +2,4 @@ CREATE TABLE courses (
|
||||
id int NOT NULL PRIMARY KEY,
|
||||
name varchar(255) NOT NULL
|
||||
);
|
||||
|
||||
|
||||
18
activerecord/test/fixtures/db_definitions/mysql.drop.sql
vendored
Normal file
18
activerecord/test/fixtures/db_definitions/mysql.drop.sql
vendored
Normal file
@@ -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;
|
||||
|
||||
@@ -126,4 +126,5 @@ CREATE TABLE `binaries` (
|
||||
CREATE TABLE `computers` (
|
||||
`id` INTEGER NOT NULL PRIMARY KEY,
|
||||
`developer` INTEGER NOT NULL
|
||||
);
|
||||
);
|
||||
|
||||
|
||||
2
activerecord/test/fixtures/db_definitions/mysql2.drop.sql
vendored
Normal file
2
activerecord/test/fixtures/db_definitions/mysql2.drop.sql
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
DROP TABLE courses;
|
||||
|
||||
@@ -2,3 +2,4 @@ CREATE TABLE `courses` (
|
||||
`id` INTEGER NOT NULL PRIMARY KEY,
|
||||
`name` VARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
|
||||
18
activerecord/test/fixtures/db_definitions/postgresql.drop.sql
vendored
Normal file
18
activerecord/test/fixtures/db_definitions/postgresql.drop.sql
vendored
Normal file
@@ -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;
|
||||
|
||||
@@ -144,4 +144,5 @@ CREATE TABLE binaries (
|
||||
CREATE TABLE computers (
|
||||
id serial,
|
||||
developer integer NOT NULL
|
||||
);
|
||||
);
|
||||
|
||||
|
||||
2
activerecord/test/fixtures/db_definitions/postgresql2.drop.sql
vendored
Normal file
2
activerecord/test/fixtures/db_definitions/postgresql2.drop.sql
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
DROP TABLE courses;
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
CREATE TABLE courses (
|
||||
id serial,
|
||||
name text
|
||||
);
|
||||
);
|
||||
|
||||
|
||||
18
activerecord/test/fixtures/db_definitions/sqlite.drop.sql
vendored
Normal file
18
activerecord/test/fixtures/db_definitions/sqlite.drop.sql
vendored
Normal file
@@ -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;
|
||||
|
||||
@@ -113,4 +113,5 @@ CREATE TABLE 'binaries' (
|
||||
CREATE TABLE 'computers' (
|
||||
'id' INTEGER NOT NULL PRIMARY KEY,
|
||||
'developer' INTEGER NOT NULL
|
||||
);
|
||||
);
|
||||
|
||||
|
||||
2
activerecord/test/fixtures/db_definitions/sqlite2.drop.sql
vendored
Normal file
2
activerecord/test/fixtures/db_definitions/sqlite2.drop.sql
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
DROP TABLE courses;
|
||||
|
||||
@@ -2,3 +2,4 @@ CREATE TABLE 'courses' (
|
||||
'id' INTEGER NOT NULL PRIMARY KEY,
|
||||
'name' VARCHAR(255) NOT NULL
|
||||
);
|
||||
|
||||
|
||||
18
activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
vendored
Normal file
18
activerecord/test/fixtures/db_definitions/sqlserver.drop.sql
vendored
Normal file
@@ -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;
|
||||
|
||||
@@ -127,3 +127,4 @@ CREATE TABLE computers (
|
||||
developer int NOT NULL,
|
||||
PRIMARY KEY (id)
|
||||
);
|
||||
|
||||
|
||||
2
activerecord/test/fixtures/db_definitions/sqlserver2.drop.sql
vendored
Normal file
2
activerecord/test/fixtures/db_definitions/sqlserver2.drop.sql
vendored
Normal file
@@ -0,0 +1,2 @@
|
||||
DROP TABLE courses;
|
||||
|
||||
@@ -2,3 +2,4 @@ CREATE TABLE courses (
|
||||
id int NOT NULL PRIMARY KEY,
|
||||
name varchar(255) NOT NULL
|
||||
);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user