adapter_extensions 0.5.0 → 0.9.5.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +1 -0
- data/CHANGELOG +20 -15
- data/Gemfile +4 -0
- data/HOW_TO_RELEASE +13 -0
- data/Rakefile +5 -86
- data/adapter_extensions.gemspec +27 -0
- data/lib/adapter_extensions/connection_adapters/mysql_adapter.rb +31 -3
- data/lib/adapter_extensions/version.rb +1 -7
- data/lib/adapter_extensions.rb +0 -2
- data/test/abstract_adapter_test.rb +59 -0
- data/test/connection/mysql/connection.rb +27 -0
- data/test/connection/mysql/setup.sql +21 -0
- data/test/connection/postgresql/connection.rb +27 -0
- data/test/connection/postgresql/setup.sql +21 -0
- data/test/integration/adapter_test.rb +118 -0
- data/test/integration/empty.csv +0 -0
- data/test/integration/mysql_tests.rb +6 -0
- data/test/integration/people.csv +3 -0
- data/test/integration/people.txt +3 -0
- data/test/integration/people_with_empties.csv +3 -0
- data/test/integration/postgresql_tests.rb +6 -0
- data/test/test_helper.rb +11 -0
- metadata +73 -38
data/.gitignore
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
pkg
|
data/CHANGELOG
CHANGED
@@ -1,26 +1,31 @@
|
|
1
|
-
0.
|
2
|
-
*
|
1
|
+
0.9.5 - not yet released
|
2
|
+
* Add in REPLACE to LOAD DATA INFILE command (jayzes/kookster)
|
3
|
+
* Remove FasterCSV from dependencies (not used apparently)
|
3
4
|
|
4
|
-
0.
|
5
|
-
*
|
5
|
+
0.5 - January 14, 2009
|
6
|
+
* Updated dependencies for gem to current versions of ActiveRecord, ActiveSupport and Rake. May not be compatible with Rails versions less than 2.x.
|
6
7
|
|
7
|
-
0.
|
8
|
-
*
|
8
|
+
0.4 - September 17, 2007
|
9
|
+
* Added copy_table method that can copy the structure and data from one table to another. Currently implemented in MySQL (tested), PostgreSQL (tested) and SQL Server adapters (untested).
|
10
|
+
* Added support for SELECT..INTO for PostgreSQL.
|
9
11
|
|
10
|
-
0.
|
11
|
-
*
|
12
|
+
0.3.1 - May 4, 2007
|
13
|
+
* Added support for modifying SELECT statements to add an INSERT INTO.
|
12
14
|
|
13
15
|
0.3.0 - March 8, 2007
|
14
16
|
* PostgreSQL adapter included
|
15
17
|
* Added tests for bulk loading
|
16
18
|
* bulk_load method now handles table missing and file missing as error cases
|
17
19
|
|
18
|
-
0.
|
19
|
-
*
|
20
|
+
0.2.0 - March 6, 2007
|
21
|
+
* SQL Server adapter included (Seth Ladd)
|
20
22
|
|
21
|
-
0.
|
22
|
-
*
|
23
|
-
|
23
|
+
0.1.2 - March 5, 2007
|
24
|
+
* Bug fixes
|
25
|
+
|
26
|
+
0.1.1 - March 5, 2007
|
27
|
+
* Bug fixes
|
28
|
+
|
29
|
+
0.1.0 - March 5, 2007
|
30
|
+
* Initial release
|
24
31
|
|
25
|
-
0.5 -
|
26
|
-
* Updated dependencies for gem to current versions of ActiveRecord, ActiveSupport and Rake. May not be compatible with Rails versions less than 2.x.
|
data/Gemfile
ADDED
data/HOW_TO_RELEASE
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
* update lib/adapter_extensions/version
|
2
|
+
* push your changes
|
3
|
+
* then use bundler to build + git tag + push to rubygems
|
4
|
+
|
5
|
+
rake release
|
6
|
+
|
7
|
+
* if you remain stuck at "Pushed git commits and tags", the task may silently wait for your password. Check this if it's the case:
|
8
|
+
|
9
|
+
https://github.com/carlhuda/bundler/issues/980
|
10
|
+
|
11
|
+
* you can list changes using github:
|
12
|
+
|
13
|
+
https://github.com/activewarehouse/adapter_extensions/compare/0.9.5.rc1...master
|
data/Rakefile
CHANGED
@@ -1,23 +1,8 @@
|
|
1
|
+
require 'bundler'
|
1
2
|
require 'rake'
|
2
3
|
require 'rake/testtask'
|
3
|
-
require '
|
4
|
-
require '
|
5
|
-
require 'rake/gempackagetask'
|
6
|
-
require 'rake/contrib/rubyforgepublisher'
|
7
|
-
require 'date'
|
8
|
-
|
9
|
-
require File.join(File.dirname(__FILE__), 'lib/adapter_extensions', 'version')
|
10
|
-
|
11
|
-
PKG_BUILD = ENV['PKG_BUILD'] ? '.' + ENV['PKG_BUILD'] : ''
|
12
|
-
PKG_NAME = 'adapter_extensions'
|
13
|
-
PKG_VERSION = AdapterExtensions::VERSION::STRING + PKG_BUILD
|
14
|
-
PKG_FILE_NAME = "#{PKG_NAME}-#{PKG_VERSION}"
|
15
|
-
PKG_DESTINATION = ENV["PKG_DESTINATION"] || "../#{PKG_NAME}"
|
16
|
-
|
17
|
-
RELEASE_NAME = "REL #{PKG_VERSION}"
|
18
|
-
|
19
|
-
RUBY_FORGE_PROJECT = "activewarehouse"
|
20
|
-
RUBY_FORGE_USER = "aeden"
|
4
|
+
require 'rdoc'
|
5
|
+
require 'rdoc/task'
|
21
6
|
|
22
7
|
desc 'Default: run unit tests.'
|
23
8
|
task :default => :test
|
@@ -50,55 +35,6 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
|
|
50
35
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
36
|
end
|
52
37
|
|
53
|
-
PKG_FILES = FileList[
|
54
|
-
'CHANGELOG',
|
55
|
-
'README',
|
56
|
-
'LICENSE',
|
57
|
-
'Rakefile',
|
58
|
-
'doc/**/*',
|
59
|
-
'lib/**/*',
|
60
|
-
] - [ 'test' ]
|
61
|
-
|
62
|
-
spec = Gem::Specification.new do |s|
|
63
|
-
s.name = 'adapter_extensions'
|
64
|
-
s.version = PKG_VERSION
|
65
|
-
s.summary = "Extensions to Rails ActiveRecord adapters."
|
66
|
-
s.description = <<-EOF
|
67
|
-
Provides various extensions to the Rails ActiveRecord adapters.
|
68
|
-
EOF
|
69
|
-
|
70
|
-
s.add_dependency('rake', '>= 0.8.3')
|
71
|
-
s.add_dependency('activesupport', '>= 2.1.0')
|
72
|
-
s.add_dependency('activerecord', '>= 2.1.0')
|
73
|
-
s.add_dependency('fastercsv', '>= 1.0.0')
|
74
|
-
|
75
|
-
s.rdoc_options << '--exclude' << '.'
|
76
|
-
s.has_rdoc = false
|
77
|
-
|
78
|
-
s.files = PKG_FILES.to_a.delete_if {|f| f.include?('.svn')}
|
79
|
-
s.require_path = 'lib'
|
80
|
-
|
81
|
-
s.author = "Anthony Eden"
|
82
|
-
s.email = "anthonyeden@gmail.com"
|
83
|
-
s.homepage = "http://activewarehouse.rubyforge.org/adapter_extensions"
|
84
|
-
s.rubyforge_project = "activewarehouse"
|
85
|
-
end
|
86
|
-
|
87
|
-
Rake::GemPackageTask.new(spec) do |pkg|
|
88
|
-
pkg.gem_spec = spec
|
89
|
-
pkg.need_tar = true
|
90
|
-
pkg.need_zip = true
|
91
|
-
end
|
92
|
-
|
93
|
-
namespace :github do
|
94
|
-
desc "Update Github Gemspec"
|
95
|
-
task :update_gemspec do
|
96
|
-
File.open(File.join(File.dirname(__FILE__), "#{spec.name}.gemspec"), "w"){|f| f << spec.to_ruby}
|
97
|
-
end
|
98
|
-
end
|
99
|
-
|
100
|
-
|
101
|
-
|
102
38
|
desc "Generate code statistics"
|
103
39
|
task :lines do
|
104
40
|
lines, codelines, total_lines, total_codelines = 0, 0, 0, 0
|
@@ -124,7 +60,7 @@ task :lines do
|
|
124
60
|
puts "Total: Lines #{total_lines}, LOC #{total_codelines}"
|
125
61
|
end
|
126
62
|
|
127
|
-
desc "Publish the release files to RubyForge
|
63
|
+
desc "Publish the release files to RubyForge (UNTESTED CURRENTLY)"
|
128
64
|
task :release => [ :package ] do
|
129
65
|
`rubyforge login`
|
130
66
|
|
@@ -135,24 +71,7 @@ task :release => [ :package ] do
|
|
135
71
|
end
|
136
72
|
end
|
137
73
|
|
138
|
-
desc "Publish the API documentation"
|
74
|
+
desc "Publish the API documentation (UNTESTED CURRENTLY)"
|
139
75
|
task :pdoc => [:rdoc] do
|
140
76
|
Rake::SshDirPublisher.new("aeden@rubyforge.org", "/var/www/gforge-projects/activewarehouse/adapter_extensions/rdoc", "rdoc").upload
|
141
77
|
end
|
142
|
-
|
143
|
-
desc "Install the gem from a local generated package"
|
144
|
-
task :install => [:package] do
|
145
|
-
windows = RUBY_PLATFORM =~ /mswin/
|
146
|
-
sudo = windows ? '' : 'sudo'
|
147
|
-
gem = windows ? 'gem.bat' : 'gem'
|
148
|
-
`#{sudo} #{gem} install pkg/#{PKG_NAME}-#{PKG_VERSION}`
|
149
|
-
end
|
150
|
-
|
151
|
-
desc "Reinstall the gem from a local package copy"
|
152
|
-
task :reinstall => [:package] do
|
153
|
-
windows = RUBY_PLATFORM =~ /mswin/
|
154
|
-
sudo = windows ? '' : 'sudo'
|
155
|
-
gem = windows ? 'gem.bat' : 'gem'
|
156
|
-
`#{sudo} #{gem} uninstall #{PKG_NAME} -x`
|
157
|
-
`#{sudo} #{gem} install pkg/#{PKG_NAME}-#{PKG_VERSION}`
|
158
|
-
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
lib = File.expand_path('../lib/', __FILE__)
|
3
|
+
$:.unshift lib unless $:.include?(lib)
|
4
|
+
|
5
|
+
require 'adapter_extensions/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |s|
|
8
|
+
s.name = %q{adapter_extensions}
|
9
|
+
s.version = AdapterExtensions::VERSION
|
10
|
+
s.platform = Gem::Platform::RUBY
|
11
|
+
s.authors = ["Anthony Eden", "Thibaut Barrère"]
|
12
|
+
s.email = ["thibaut.barrere@gmail.com"]
|
13
|
+
s.homepage = "https://github.com/activewarehouse/adapter_extensions"
|
14
|
+
s.summary = %q{Extensions to Rails ActiveRecord adapters.}
|
15
|
+
s.description = %q{Provides various extensions to the Rails ActiveRecord adapters.}
|
16
|
+
s.rubyforge_project = %q{activewarehouse}
|
17
|
+
|
18
|
+
s.required_rubygems_version = ">= 1.3.6"
|
19
|
+
|
20
|
+
s.add_runtime_dependency('rake', '>= 0.8.3')
|
21
|
+
s.add_runtime_dependency('activesupport', '>= 2.1.0')
|
22
|
+
s.add_runtime_dependency('activerecord', '>= 2.1.0')
|
23
|
+
|
24
|
+
s.files = `git ls-files`.split("\n")
|
25
|
+
s.test_files = `git ls-files -- {test}/*`.split("\n")
|
26
|
+
s.require_path = "lib"
|
27
|
+
end
|
@@ -1,3 +1,7 @@
|
|
1
|
+
# lots of folks doing something like this now - take a look for other good ideas
|
2
|
+
# https://github.com/jsuchal/activerecord-fast-import/blob/master/lib/activerecord-fast-import.rb
|
3
|
+
# https://github.com/EmmanuelOga/load_data_infile/blob/master/lib/load_data_infile.rb
|
4
|
+
|
1
5
|
# Source code for the MysqlAdapter extensions.
|
2
6
|
module ActiveRecord #:nodoc:
|
3
7
|
module ConnectionAdapters #:nodoc:
|
@@ -20,6 +24,21 @@ module ActiveRecord #:nodoc:
|
|
20
24
|
execute "INSERT INTO #{new_table_name} SELECT * FROM #{old_table_name}"
|
21
25
|
end
|
22
26
|
end
|
27
|
+
|
28
|
+
def disable_keys(table)
|
29
|
+
execute("ALTER TABLE #{table} DISABLE KEYS")
|
30
|
+
end
|
31
|
+
|
32
|
+
def enable_keys(table)
|
33
|
+
execute("ALTER TABLE #{table} ENABLE KEYS")
|
34
|
+
end
|
35
|
+
|
36
|
+
def with_keys_disabled(table)
|
37
|
+
disable_keys(table)
|
38
|
+
yield
|
39
|
+
ensure
|
40
|
+
enable_keys(table)
|
41
|
+
end
|
23
42
|
|
24
43
|
protected
|
25
44
|
# Call +bulk_load+, as that method wraps this method.
|
@@ -33,6 +52,8 @@ module ActiveRecord #:nodoc:
|
|
33
52
|
# * <tt>:fields</tt> -- Hash of options for fields:
|
34
53
|
# * <tt>:delimited_by</tt> -- The field delimiter
|
35
54
|
# * <tt>:enclosed_by</tt> -- The field enclosure
|
55
|
+
# * <tt>:replace</tt> -- Add in REPLACE to LOAD DATA INFILE command
|
56
|
+
# * <tt>:disable_keys</tt> -- if set to true, disable keys, loads, then enables again
|
36
57
|
def do_bulk_load(file, table_name, options={})
|
37
58
|
return if File.size(file) == 0
|
38
59
|
|
@@ -47,15 +68,22 @@ module ActiveRecord #:nodoc:
|
|
47
68
|
@bulk_load_enabled = true
|
48
69
|
end
|
49
70
|
|
50
|
-
q = "LOAD DATA LOCAL INFILE '#{file}' INTO TABLE #{table_name}"
|
71
|
+
q = "LOAD DATA LOCAL INFILE '#{file}' #{options[:replace] ? 'REPLACE' : ''} INTO TABLE #{table_name}"
|
51
72
|
if options[:fields]
|
52
73
|
q << " FIELDS"
|
53
74
|
q << " TERMINATED BY '#{options[:fields][:delimited_by]}'" if options[:fields][:delimited_by]
|
54
75
|
q << " ENCLOSED BY '#{options[:fields][:enclosed_by]}'" if options[:fields][:enclosed_by]
|
55
76
|
end
|
56
77
|
q << " IGNORE #{options[:ignore]} LINES" if options[:ignore]
|
57
|
-
q << " (#{options[:columns].join(',')})" if options[:columns]
|
58
|
-
|
78
|
+
q << " (#{options[:columns].map { |c| quote_column_name(c.to_s) }.join(',')})" if options[:columns]
|
79
|
+
|
80
|
+
puts q
|
81
|
+
if options[:disable_keys]
|
82
|
+
with_keys_disabled(table_name) { execute(q) }
|
83
|
+
else
|
84
|
+
execute(q)
|
85
|
+
end
|
86
|
+
|
59
87
|
end
|
60
88
|
|
61
89
|
end
|
data/lib/adapter_extensions.rb
CHANGED
@@ -0,0 +1,59 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/test_helper'
|
2
|
+
|
3
|
+
class AbstractAdapterTest < Test::Unit::TestCase
|
4
|
+
include ActiveRecord::ConnectionAdapters
|
5
|
+
class MyAdapter < AbstractAdapter
|
6
|
+
attr_accessor :query
|
7
|
+
def initialize
|
8
|
+
super(nil)
|
9
|
+
end
|
10
|
+
def tables
|
11
|
+
['people']
|
12
|
+
end
|
13
|
+
def execute(query)
|
14
|
+
@query = query
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
attr_accessor :adapter
|
19
|
+
def setup
|
20
|
+
@adapter = MyAdapter.new
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_truncate
|
24
|
+
table_name = 'foo'
|
25
|
+
adapter.truncate(table_name)
|
26
|
+
assert_equal "TRUNCATE TABLE #{table_name}", adapter.query
|
27
|
+
end
|
28
|
+
|
29
|
+
def test_support_select_into_table_should_return_false
|
30
|
+
assert_equal false, adapter.support_select_into_table?
|
31
|
+
end
|
32
|
+
def test_add_select_into_table_should_raise_not_implemented_error
|
33
|
+
assert_raises(NotImplementedError) { adapter.add_select_into_table(:new_table_name, :query) }
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_bulk_load_with_non_existent_file
|
37
|
+
file = 'f'
|
38
|
+
assert_raises(ArgumentError, "#{file} does not exist") { adapter.bulk_load(file, :table_name)}
|
39
|
+
end
|
40
|
+
def test_bulk_load_with_non_existent_table
|
41
|
+
filename = File.dirname(__FILE__) + '/integration/people.csv'
|
42
|
+
#flexmock(File).should_receive(:exists?).and_return(true)
|
43
|
+
table_name = 'foo'
|
44
|
+
assert_raise(ArgumentError) do
|
45
|
+
adapter.bulk_load(filename, table_name)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_do_bulk_load_should_raise_not_implemented_error
|
50
|
+
filename = File.dirname(__FILE__) + '/integration/people.csv'
|
51
|
+
# I'd like to do the below but it doesn't seem to work the way I want
|
52
|
+
#flexmock(File).should_receive(:exists?).and_return(true)
|
53
|
+
table_name = 'people'
|
54
|
+
assert_raise(NotImplementedError) do
|
55
|
+
adapter.bulk_load(filename, table_name)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
print "Using native MySQL\n"
|
2
|
+
|
3
|
+
#require 'logger'
|
4
|
+
#ActiveRecord::Base.logger = Logger.new("debug.log")
|
5
|
+
|
6
|
+
ActiveRecord::Base.configurations = {
|
7
|
+
'adapter_extensions_unittest' => {
|
8
|
+
:adapter => :mysql,
|
9
|
+
:username => 'root',
|
10
|
+
:host => 'localhost',
|
11
|
+
:database => 'adapter_extensions_unittest',
|
12
|
+
:encoding => 'utf8',
|
13
|
+
:setup_file => 'setup.sql',
|
14
|
+
}
|
15
|
+
}
|
16
|
+
|
17
|
+
ActiveRecord::Base.establish_connection 'adapter_extensions_unittest'
|
18
|
+
|
19
|
+
puts "Resetting database"
|
20
|
+
conn = ActiveRecord::Base.connection
|
21
|
+
conn.recreate_database(conn.current_database)
|
22
|
+
conn.reconnect!
|
23
|
+
lines = open(
|
24
|
+
File.join(File.dirname(__FILE__),
|
25
|
+
ActiveRecord::Base.configurations['adapter_extensions_unittest'][:setup_file])
|
26
|
+
).readlines
|
27
|
+
lines.join.split(';').each { |line| conn.execute(line) }
|
@@ -0,0 +1,21 @@
|
|
1
|
+
drop table if exists people;
|
2
|
+
create table people (
|
3
|
+
first_name char(255),
|
4
|
+
last_name char(255),
|
5
|
+
ssn char(64)
|
6
|
+
);
|
7
|
+
drop table if exists places;
|
8
|
+
create table places (
|
9
|
+
address text,
|
10
|
+
city char(255),
|
11
|
+
state char(255),
|
12
|
+
country char(2)
|
13
|
+
);
|
14
|
+
|
15
|
+
drop table if exists truncate_test;
|
16
|
+
create table truncate_test (
|
17
|
+
x char(4)
|
18
|
+
);
|
19
|
+
insert into truncate_test (x) values ('a');
|
20
|
+
insert into truncate_test (x) values ('b');
|
21
|
+
insert into truncate_test (x) values ('c');
|
@@ -0,0 +1,27 @@
|
|
1
|
+
print "Using PostgreSQL\n"
|
2
|
+
|
3
|
+
ActiveRecord::Base.configurations = {
|
4
|
+
'adapter_extensions_unittest' => {
|
5
|
+
:adapter => :postgresql,
|
6
|
+
:username => 'postgres',
|
7
|
+
:password => 'postgres',
|
8
|
+
:host => 'localhost',
|
9
|
+
:database => 'adapter_extensions_unittest',
|
10
|
+
:encoding => 'utf8',
|
11
|
+
:setup_file => 'setup.sql',
|
12
|
+
}
|
13
|
+
}
|
14
|
+
|
15
|
+
ActiveRecord::Base.establish_connection 'adapter_extensions_unittest'
|
16
|
+
conn = ActiveRecord::Base.connection
|
17
|
+
lines = open(
|
18
|
+
File.join(File.dirname(__FILE__),
|
19
|
+
ActiveRecord::Base.configurations['adapter_extensions_unittest'][:setup_file])
|
20
|
+
).readlines
|
21
|
+
lines.join.split(';').each_with_index do |line, index|
|
22
|
+
begin
|
23
|
+
conn.execute(line)
|
24
|
+
rescue => e
|
25
|
+
#puts "failed to load line #{index}: #{e}"
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
create table people (
|
2
|
+
first_name char(255),
|
3
|
+
last_name char(255),
|
4
|
+
ssn char(64)
|
5
|
+
);
|
6
|
+
delete from people;
|
7
|
+
create table places (
|
8
|
+
address text,
|
9
|
+
city char(255),
|
10
|
+
state char(255),
|
11
|
+
country char(2)
|
12
|
+
);
|
13
|
+
delete from places;
|
14
|
+
|
15
|
+
create table truncate_test (
|
16
|
+
x char(4)
|
17
|
+
);
|
18
|
+
delete from truncate_test;
|
19
|
+
insert into truncate_test (x) values ('a');
|
20
|
+
insert into truncate_test (x) values ('b');
|
21
|
+
insert into truncate_test (x) values ('c');
|
@@ -0,0 +1,118 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../test_helper'
|
2
|
+
|
3
|
+
# Integration tests
|
4
|
+
class AdapterTest < Test::Unit::TestCase
|
5
|
+
|
6
|
+
require File.dirname(__FILE__) + "/#{ENV['DB']}_tests"
|
7
|
+
include "#{ENV['DB'].capitalize}Tests".constantize
|
8
|
+
|
9
|
+
def test_add_select_into_table
|
10
|
+
new_table_name = 'new_people'
|
11
|
+
sql_query = 'select * from people'
|
12
|
+
assert_equal "CREATE TABLE #{new_table_name} #{sql_query}",
|
13
|
+
connection.add_select_into_table(new_table_name, sql_query)
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_truncate
|
17
|
+
connection.delete("delete from truncate_test")
|
18
|
+
%w|a b c|.each do |value|
|
19
|
+
connection.execute("insert into truncate_test (x) values ('#{value}')")
|
20
|
+
end
|
21
|
+
|
22
|
+
assert_equal "3", connection.select_value("SELECT count(*) FROM truncate_test")
|
23
|
+
assert_nothing_raised { connection.truncate('truncate_test') }
|
24
|
+
assert_equal "0", connection.select_value("SELECT count(*) FROM truncate_test")
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_bulk_load
|
28
|
+
connection.truncate('people')
|
29
|
+
assert_equal "0", connection.select_value("SELECT count(*) FROM people")
|
30
|
+
assert_nothing_raised do
|
31
|
+
connection.bulk_load(File.join(File.dirname(__FILE__), 'people.txt'), 'people')
|
32
|
+
end
|
33
|
+
assert_equal "3", connection.select_value("SELECT count(*) FROM people")
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_bulk_load_csv
|
37
|
+
connection.truncate('people')
|
38
|
+
assert_nothing_raised do
|
39
|
+
options = {:fields => {:delimited_by => ','}}
|
40
|
+
connection.bulk_load(File.join(File.dirname(__FILE__), 'people.csv'), 'people', options)
|
41
|
+
end
|
42
|
+
assert_equal "3", connection.select_value("SELECT count(*) FROM people")
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_bulk_load_with_enclosed_by
|
46
|
+
connection.truncate('people')
|
47
|
+
assert_nothing_raised do
|
48
|
+
options = {:fields => {:delimited_by => ',', :enclosed_by => '"'}}
|
49
|
+
connection.bulk_load(File.join(File.dirname(__FILE__), 'people.csv'), 'people', options)
|
50
|
+
end
|
51
|
+
assert_equal "3", connection.select_value("SELECT count(*) FROM people")
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_bulk_load_with_null_string
|
55
|
+
connection.truncate('people')
|
56
|
+
assert_nothing_raised do
|
57
|
+
options = {:fields => {:delimited_by => ',', :null_string => ''}}
|
58
|
+
connection.bulk_load(File.join(File.dirname(__FILE__), 'people.csv'), 'people', options)
|
59
|
+
end
|
60
|
+
assert_equal "3", connection.select_value("SELECT count(*) FROM people")
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_bulk_load_interprets_empty_strings_as_empty_strings
|
64
|
+
connection.truncate('people')
|
65
|
+
options = {:fields => {:delimited_by => ','}}
|
66
|
+
connection.bulk_load(File.join(File.dirname(__FILE__), 'people_with_empties.csv'), 'people', options)
|
67
|
+
assert_equal "0", connection.select_value("SELECT count(*) FROM people WHERE first_name IS NULL")
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_bulk_load_interprets_empty_strings_as_nulls
|
71
|
+
connection.truncate('people')
|
72
|
+
options = {:fields => {:delimited_by => ',', :null_string => ''}}
|
73
|
+
connection.bulk_load(File.join(File.dirname(__FILE__), 'people_with_empties.csv'), 'people', options)
|
74
|
+
assert_equal "1", connection.select_value("SELECT count(*) FROM people WHERE first_name IS NULL"),
|
75
|
+
"NOTE: this is a known issue with MySQL - any other db should work correctly"
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_bulk_load_with_empty_file
|
79
|
+
connection.truncate('people')
|
80
|
+
assert_nothing_raised do
|
81
|
+
connection.bulk_load(File.dirname(__FILE__) + '/empty.csv', 'people')
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_add_select_into_table
|
86
|
+
sql_query = 'SELECT foo FROM bar'
|
87
|
+
new_table_name = 'new_table'
|
88
|
+
|
89
|
+
case ENV['DB']
|
90
|
+
when 'sqlserver', 'postgresql'
|
91
|
+
assert_equal 'SELECT foo INTO new_table FROM bar',
|
92
|
+
connection.add_select_into_table(new_table_name, sql_query)
|
93
|
+
when 'mysql'
|
94
|
+
assert_equal 'CREATE TABLE new_table SELECT foo FROM bar',
|
95
|
+
connection.add_select_into_table(new_table_name, sql_query)
|
96
|
+
else
|
97
|
+
puts "Don't know how to add select into table for #{ENV['DB']}"
|
98
|
+
end
|
99
|
+
end
|
100
|
+
|
101
|
+
def test_copy_table_should_copy_structure_and_data
|
102
|
+
table_name = 'people'
|
103
|
+
dest_table_name = "temp_#{table_name}"
|
104
|
+
|
105
|
+
begin connection.drop_table(dest_table_name); rescue; end;
|
106
|
+
connection.execute("DELETE FROM #{table_name}")
|
107
|
+
|
108
|
+
options = {:fields => {:delimited_by => ',', :enclosed_by => '"'}}
|
109
|
+
connection.bulk_load(File.join(File.dirname(__FILE__), 'people.csv'), 'people', options)
|
110
|
+
connection.copy_table(table_name, dest_table_name)
|
111
|
+
assert_equal 3, connection.select_value("SELECT count(*) FROM #{dest_table_name}").to_i
|
112
|
+
end
|
113
|
+
|
114
|
+
private
|
115
|
+
def connection
|
116
|
+
ActiveRecord::Base.connection
|
117
|
+
end
|
118
|
+
end
|
File without changes
|
data/test/test_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
$:.unshift(File.dirname(__FILE__))
|
2
|
+
|
3
|
+
require 'test/unit'
|
4
|
+
require 'rubygems'
|
5
|
+
require 'flexmock/test_unit'
|
6
|
+
require 'pp'
|
7
|
+
|
8
|
+
require File.dirname(__FILE__) + '/../lib/adapter_extensions'
|
9
|
+
|
10
|
+
db = ENV['DB'] ||= 'mysql'
|
11
|
+
require "connection/#{db}/connection"
|
metadata
CHANGED
@@ -1,59 +1,72 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: adapter_extensions
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
|
4
|
+
prerelease: true
|
5
|
+
segments:
|
6
|
+
- 0
|
7
|
+
- 9
|
8
|
+
- 5
|
9
|
+
- rc1
|
10
|
+
version: 0.9.5.rc1
|
5
11
|
platform: ruby
|
6
12
|
authors:
|
7
13
|
- Anthony Eden
|
14
|
+
- "Thibaut Barr\xC3\xA8re"
|
8
15
|
autorequire:
|
9
16
|
bindir: bin
|
10
17
|
cert_chain: []
|
11
18
|
|
12
|
-
date:
|
19
|
+
date: 2011-06-20 00:00:00 +02:00
|
13
20
|
default_executable:
|
14
21
|
dependencies:
|
15
22
|
- !ruby/object:Gem::Dependency
|
16
23
|
name: rake
|
17
|
-
|
18
|
-
|
19
|
-
|
24
|
+
prerelease: false
|
25
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
26
|
+
none: false
|
20
27
|
requirements:
|
21
28
|
- - ">="
|
22
29
|
- !ruby/object:Gem::Version
|
30
|
+
segments:
|
31
|
+
- 0
|
32
|
+
- 8
|
33
|
+
- 3
|
23
34
|
version: 0.8.3
|
24
|
-
|
35
|
+
type: :runtime
|
36
|
+
version_requirements: *id001
|
25
37
|
- !ruby/object:Gem::Dependency
|
26
38
|
name: activesupport
|
27
|
-
|
28
|
-
|
29
|
-
|
39
|
+
prerelease: false
|
40
|
+
requirement: &id002 !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
30
42
|
requirements:
|
31
43
|
- - ">="
|
32
44
|
- !ruby/object:Gem::Version
|
45
|
+
segments:
|
46
|
+
- 2
|
47
|
+
- 1
|
48
|
+
- 0
|
33
49
|
version: 2.1.0
|
34
|
-
|
50
|
+
type: :runtime
|
51
|
+
version_requirements: *id002
|
35
52
|
- !ruby/object:Gem::Dependency
|
36
53
|
name: activerecord
|
37
|
-
|
38
|
-
|
39
|
-
|
54
|
+
prerelease: false
|
55
|
+
requirement: &id003 !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
40
57
|
requirements:
|
41
58
|
- - ">="
|
42
59
|
- !ruby/object:Gem::Version
|
60
|
+
segments:
|
61
|
+
- 2
|
62
|
+
- 1
|
63
|
+
- 0
|
43
64
|
version: 2.1.0
|
44
|
-
version:
|
45
|
-
- !ruby/object:Gem::Dependency
|
46
|
-
name: fastercsv
|
47
65
|
type: :runtime
|
48
|
-
|
49
|
-
version_requirements: !ruby/object:Gem::Requirement
|
50
|
-
requirements:
|
51
|
-
- - ">="
|
52
|
-
- !ruby/object:Gem::Version
|
53
|
-
version: 1.0.0
|
54
|
-
version:
|
66
|
+
version_requirements: *id003
|
55
67
|
description: Provides various extensions to the Rails ActiveRecord adapters.
|
56
|
-
email:
|
68
|
+
email:
|
69
|
+
- thibaut.barrere@gmail.com
|
57
70
|
executables: []
|
58
71
|
|
59
72
|
extensions: []
|
@@ -61,44 +74,66 @@ extensions: []
|
|
61
74
|
extra_rdoc_files: []
|
62
75
|
|
63
76
|
files:
|
77
|
+
- .gitignore
|
64
78
|
- CHANGELOG
|
65
|
-
-
|
79
|
+
- Gemfile
|
80
|
+
- HOW_TO_RELEASE
|
66
81
|
- LICENSE
|
82
|
+
- README
|
67
83
|
- Rakefile
|
68
|
-
-
|
69
|
-
- lib/adapter_extensions
|
84
|
+
- adapter_extensions.gemspec
|
85
|
+
- lib/adapter_extensions.rb
|
70
86
|
- lib/adapter_extensions/connection_adapters/abstract_adapter.rb
|
71
87
|
- lib/adapter_extensions/connection_adapters/mysql_adapter.rb
|
72
88
|
- lib/adapter_extensions/connection_adapters/postgresql_adapter.rb
|
73
89
|
- lib/adapter_extensions/connection_adapters/sqlserver_adapter.rb
|
74
90
|
- lib/adapter_extensions/version.rb
|
75
|
-
-
|
76
|
-
|
77
|
-
|
91
|
+
- test/abstract_adapter_test.rb
|
92
|
+
- test/connection/mysql/connection.rb
|
93
|
+
- test/connection/mysql/setup.sql
|
94
|
+
- test/connection/postgresql/connection.rb
|
95
|
+
- test/connection/postgresql/setup.sql
|
96
|
+
- test/integration/adapter_test.rb
|
97
|
+
- test/integration/empty.csv
|
98
|
+
- test/integration/mysql_tests.rb
|
99
|
+
- test/integration/people.csv
|
100
|
+
- test/integration/people.txt
|
101
|
+
- test/integration/people_with_empties.csv
|
102
|
+
- test/integration/postgresql_tests.rb
|
103
|
+
- test/test_helper.rb
|
104
|
+
has_rdoc: true
|
105
|
+
homepage: https://github.com/activewarehouse/adapter_extensions
|
106
|
+
licenses: []
|
107
|
+
|
78
108
|
post_install_message:
|
79
|
-
rdoc_options:
|
80
|
-
|
81
|
-
- .
|
109
|
+
rdoc_options: []
|
110
|
+
|
82
111
|
require_paths:
|
83
112
|
- lib
|
84
113
|
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
none: false
|
85
115
|
requirements:
|
86
116
|
- - ">="
|
87
117
|
- !ruby/object:Gem::Version
|
118
|
+
segments:
|
119
|
+
- 0
|
88
120
|
version: "0"
|
89
|
-
version:
|
90
121
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
122
|
+
none: false
|
91
123
|
requirements:
|
92
124
|
- - ">="
|
93
125
|
- !ruby/object:Gem::Version
|
94
|
-
|
95
|
-
|
126
|
+
segments:
|
127
|
+
- 1
|
128
|
+
- 3
|
129
|
+
- 6
|
130
|
+
version: 1.3.6
|
96
131
|
requirements: []
|
97
132
|
|
98
133
|
rubyforge_project: activewarehouse
|
99
|
-
rubygems_version: 1.3.
|
134
|
+
rubygems_version: 1.3.7
|
100
135
|
signing_key:
|
101
|
-
specification_version:
|
136
|
+
specification_version: 3
|
102
137
|
summary: Extensions to Rails ActiveRecord adapters.
|
103
138
|
test_files: []
|
104
139
|
|