rsconn 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +24 -0
- data/README.md +29 -0
- data/Rakefile +5 -0
- data/lib/rsconn/jdbc_url.rb +18 -0
- data/lib/rsconn/logger.rb +31 -0
- data/lib/rsconn/postgres.rb +176 -0
- data/lib/rsconn/typecast_result.rb +65 -0
- data/lib/rsconn/version.rb +3 -0
- data/lib/rsconn.rb +25 -0
- data/rsconn.gemspec +31 -0
- data/spec/custom_matchers.rb +11 -0
- data/spec/dev_redshift.rb +16 -0
- data/spec/jdbc_url_spec.rb +29 -0
- data/spec/redshift_spec.rb +74 -0
- data/spec/typecast_result_spec.rb +121 -0
- metadata +177 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1c30c89eb0d8df0325b2e030f9456e007b73bd91
|
4
|
+
data.tar.gz: f92be0b7307b051a9ae335b9b23d2d5428bd730a
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 974295630b65b75e56c3c46540fa4201b158dbd2b1abd1e0f78fecf0d45bd06e4adff49bb1c9ed636e00e875c9d51535cd821622028540e0d336fffa27cffa5c
|
7
|
+
data.tar.gz: 17a74642754c11403981bb81105c1fa1a6877914fead77a2c0c71f39a3107cdff04cbd35d3997693cb340a4b0424fcd3fff0aa52c2d4f57989795f3f31ea7a70
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
This is free and unencumbered software released into the public domain.
|
2
|
+
|
3
|
+
Anyone is free to copy, modify, publish, use, compile, sell, or
|
4
|
+
distribute this software, either in source code form or as a compiled
|
5
|
+
binary, for any purpose, commercial or non-commercial, and by any
|
6
|
+
means.
|
7
|
+
|
8
|
+
In jurisdictions that recognize copyright laws, the author or authors
|
9
|
+
of this software dedicate any and all copyright interest in the
|
10
|
+
software to the public domain. We make this dedication for the benefit
|
11
|
+
of the public at large and to the detriment of our heirs and
|
12
|
+
successors. We intend this dedication to be an overt act of
|
13
|
+
relinquishment in perpetuity of all present and future rights to this
|
14
|
+
software under copyright law.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
19
|
+
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
|
20
|
+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
|
21
|
+
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
22
|
+
OTHER DEALINGS IN THE SOFTWARE.
|
23
|
+
|
24
|
+
For more information, please refer to <http://unlicense.org/>
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# rsconn
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'rsconn'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rsconn
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
TODO: Write usage instructions here
|
22
|
+
|
23
|
+
## Contributing
|
24
|
+
|
25
|
+
1. Fork it ( https://github.com/deanmorin/rsconn/fork )
|
26
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
27
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
28
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
29
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
module Rsconn
|
2
|
+
class JdbcUrl
|
3
|
+
attr_reader :url, :host, :port, :database
|
4
|
+
|
5
|
+
def initialize(url)
|
6
|
+
@url = url
|
7
|
+
@host = @url.split('//').last.split(':').first
|
8
|
+
@port = @url.split(':').last.split('/').first.to_i
|
9
|
+
@database = @url.split('/').last
|
10
|
+
|
11
|
+
db_type = @url.split(':')[1]
|
12
|
+
|
13
|
+
if db_type != 'postgresql'
|
14
|
+
fail ArgumentError, 'Only works with a "postgresql" JDBC URL'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
require 'singleton'
|
2
|
+
require 'time'
|
3
|
+
|
4
|
+
|
5
|
+
module Rsconn
|
6
|
+
class Logger
|
7
|
+
|
8
|
+
def initialize(options={})
|
9
|
+
@secrets = options.fetch(:secrets, [])
|
10
|
+
@secrets << ENV['AWS_SECRET_ACCESS_KEY'] if ENV['AWS_SECRET_ACCESS_KEY']
|
11
|
+
end
|
12
|
+
|
13
|
+
def log(msg)
|
14
|
+
puts "#{now_utc} | #{hide_secrets(msg)}"
|
15
|
+
end
|
16
|
+
|
17
|
+
private
|
18
|
+
|
19
|
+
def hide_secrets(msg)
|
20
|
+
@secrets.inject(msg) {|message, secret| hide_secret(message, secret) }
|
21
|
+
end
|
22
|
+
|
23
|
+
def hide_secret(msg, secret)
|
24
|
+
msg.sub(secret, '*' * secret.length)
|
25
|
+
end
|
26
|
+
|
27
|
+
def now_utc
|
28
|
+
DateTime.now.new_offset(0).strftime('%Y-%m-%d %H:%M:%S')
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,176 @@
|
|
1
|
+
require 'pg'
|
2
|
+
require 'rsconn/jdbc_url'
|
3
|
+
require 'rsconn/logger'
|
4
|
+
|
5
|
+
|
6
|
+
module Rsconn
|
7
|
+
class PG::Result
|
8
|
+
def length
|
9
|
+
ntuples
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
class Postgres
|
15
|
+
attr_reader :conn
|
16
|
+
|
17
|
+
def initialize(jdbc_url, user, password, options={})
|
18
|
+
fail_if_invalid_connection_credentials(jdbc_url, user, password)
|
19
|
+
|
20
|
+
@abort_on_error = options.fetch(:abort_on_error, true)
|
21
|
+
@max_retries = options.fetch(:max_retries, 3)
|
22
|
+
@quiet = options.fetch(:quiet, false)
|
23
|
+
@jdbc_url = JdbcUrl.new(jdbc_url)
|
24
|
+
@user = user
|
25
|
+
@password = password
|
26
|
+
@error_occurred = false
|
27
|
+
@retry_count = 0
|
28
|
+
|
29
|
+
secrets = options.fetch(:secrets, [])
|
30
|
+
@logger = Logger.new(:secrets => secrets)
|
31
|
+
|
32
|
+
init_connection
|
33
|
+
end
|
34
|
+
|
35
|
+
def execute(sql, options={})
|
36
|
+
quiet = options.fetch(:quiet, @quiet)
|
37
|
+
|
38
|
+
log "\n#{sql}\n" unless quiet
|
39
|
+
result = with_error_handling { @conn.exec(sql) }
|
40
|
+
|
41
|
+
cmd = result.cmd_status.split.first
|
42
|
+
affected_rows = result.cmd_tuples
|
43
|
+
|
44
|
+
if %w(INSERT UPDATE DELETE MOVE FETCH).include?(cmd)
|
45
|
+
log "Affected #{affected_rows} row(s)." unless quiet
|
46
|
+
end
|
47
|
+
|
48
|
+
affected_rows
|
49
|
+
end
|
50
|
+
|
51
|
+
def query(sql, options={})
|
52
|
+
quiet = options.fetch(:quiet, @quiet)
|
53
|
+
|
54
|
+
log "\n#{sql}\n" unless quiet
|
55
|
+
result = with_error_handling { @conn.exec(sql) }
|
56
|
+
TypecastResult.new(result).result
|
57
|
+
end
|
58
|
+
|
59
|
+
def execute_script(filename, options={})
|
60
|
+
File.open(filename) do |fh|
|
61
|
+
sql = fh.read
|
62
|
+
sql = remove_comments(sql)
|
63
|
+
sql = substitute_variables(sql)
|
64
|
+
execute_each_statement(sql, options)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
def error_occurred?
|
69
|
+
@error_occurred
|
70
|
+
end
|
71
|
+
|
72
|
+
def clear_error_state
|
73
|
+
@error_occurred = false
|
74
|
+
end
|
75
|
+
|
76
|
+
def drop_table_if_exists(table, options={})
|
77
|
+
cascade = options.delete(:cascade) ? ' CASCADE' : ''
|
78
|
+
execute("DROP TABLE IF EXISTS #{table}#{cascade};", options)
|
79
|
+
end
|
80
|
+
|
81
|
+
def drop_view_if_exists(view, options={})
|
82
|
+
cascade = options.delete(:cascade) ? ' CASCADE' : ''
|
83
|
+
execute("DROP VIEW IF EXISTS #{view}#{cascade};", options)
|
84
|
+
end
|
85
|
+
|
86
|
+
def table_exists?(schema, table)
|
87
|
+
sql = <<-SQL
|
88
|
+
SELECT count(*) FROM pg_tables
|
89
|
+
WHERE schemaname = '#{schema}' AND tablename = '#{table}'
|
90
|
+
;
|
91
|
+
SQL
|
92
|
+
query(sql, :quiet => true).first['count'] == 1
|
93
|
+
end
|
94
|
+
|
95
|
+
private
|
96
|
+
|
97
|
+
def fail_if_invalid_connection_credentials(jdbc_url, user, password)
|
98
|
+
fail ArgumentError, 'jdbc_url needs to be a string' unless jdbc_url.is_a?(String)
|
99
|
+
fail ArgumentError, 'user needs to be a string' unless user.is_a?(String)
|
100
|
+
fail ArgumentError, 'password needs to be a string' unless password.is_a?(String)
|
101
|
+
end
|
102
|
+
|
103
|
+
def init_connection
|
104
|
+
with_error_handling do
|
105
|
+
@conn = PGconn.open(
|
106
|
+
:host => @jdbc_url.host,
|
107
|
+
:port => @jdbc_url.port,
|
108
|
+
:dbname => @jdbc_url.database,
|
109
|
+
:user => @user,
|
110
|
+
:password => @password,
|
111
|
+
)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
# errors_to_ignore should only be used where no return value is expected
|
116
|
+
def with_error_handling
|
117
|
+
return_value = yield
|
118
|
+
@retry_count = 0
|
119
|
+
return_value
|
120
|
+
|
121
|
+
rescue PG::Error => e
|
122
|
+
if recoverable_error?(e) && @retry_count < @max_retries
|
123
|
+
@retry_count += 1
|
124
|
+
sleep_time = sleep_time_for_error(e)
|
125
|
+
log "Failed with recoverable error (#{e.class}): #{e}"
|
126
|
+
log "Retry attempt #{@retry_count} will occur after #{sleep_time} seconds"
|
127
|
+
sleep sleep_time
|
128
|
+
retry
|
129
|
+
|
130
|
+
else
|
131
|
+
@retry_count = 0
|
132
|
+
@error_occurred = true
|
133
|
+
log "An error occurred (#{e.class}): #{e}"
|
134
|
+
log e.backtrace.join("\n")
|
135
|
+
|
136
|
+
raise e if @abort_on_error
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
def recoverable_error?(err_msg)
|
141
|
+
false
|
142
|
+
end
|
143
|
+
|
144
|
+
def sleep_time_for_error(err_msg)
|
145
|
+
60
|
146
|
+
end
|
147
|
+
|
148
|
+
def sql_exception_class
|
149
|
+
fail NotImplementedError, 'sql_exception_class'
|
150
|
+
end
|
151
|
+
|
152
|
+
def remove_comments(sql)
|
153
|
+
lines = sql.split("\n")
|
154
|
+
stripped_lines = lines.reject {|line| starts_with_double_dash?(line) }
|
155
|
+
stripped_lines.join("\n")
|
156
|
+
end
|
157
|
+
|
158
|
+
def starts_with_double_dash?(line)
|
159
|
+
line =~ /\A\s*--/
|
160
|
+
end
|
161
|
+
|
162
|
+
def substitute_variables(sql)
|
163
|
+
sql
|
164
|
+
end
|
165
|
+
|
166
|
+
def execute_each_statement(sql, options={})
|
167
|
+
sql.split(/;\s*$/).each do |statement|
|
168
|
+
execute("#{statement};", options)
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
def log(msg)
|
173
|
+
@logger.log(msg)
|
174
|
+
end
|
175
|
+
end
|
176
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
module Rsconn
|
2
|
+
class TypecastResult
|
3
|
+
attr_reader :result
|
4
|
+
|
5
|
+
TYPE_BOOLEAN = 16
|
6
|
+
TYPE_BIGINT = 20
|
7
|
+
TYPE_SMALLINT = 21
|
8
|
+
TYPE_INTEGER = 23
|
9
|
+
TYPE_OID = 26
|
10
|
+
TYPE_REAL = 700
|
11
|
+
TYPE_DOUBLE_PRECISION = 701
|
12
|
+
TYPE_MONEY = 790
|
13
|
+
TYPE_NUMERIC = 1700
|
14
|
+
TYPE_DATE = 1082
|
15
|
+
TYPE_TIMESTAMP_WITHOUT_TIME_ZONE = 1114
|
16
|
+
TYPE_TIMESTAMP_WITH_TIME_ZONE = 1184
|
17
|
+
|
18
|
+
def initialize(uncast_result)
|
19
|
+
@uncast_result = uncast_result
|
20
|
+
@result = uncast_result.map {|row| typecast_row(row) }
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def typecast_row(row)
|
26
|
+
(0...@uncast_result.nfields).each do |index|
|
27
|
+
column = @uncast_result.fname(index)
|
28
|
+
type = @uncast_result.ftype(index)
|
29
|
+
value = row[column]
|
30
|
+
|
31
|
+
row[column] = typecast(type, value)
|
32
|
+
end
|
33
|
+
row
|
34
|
+
end
|
35
|
+
|
36
|
+
def typecast(type, value)
|
37
|
+
case type
|
38
|
+
|
39
|
+
when TYPE_BOOLEAN
|
40
|
+
if value == 't'
|
41
|
+
true
|
42
|
+
elsif value == 'f'
|
43
|
+
false
|
44
|
+
else
|
45
|
+
fail 'unexpected boolean string'
|
46
|
+
end
|
47
|
+
|
48
|
+
when TYPE_BIGINT, TYPE_SMALLINT, TYPE_INTEGER, TYPE_OID
|
49
|
+
value.to_i
|
50
|
+
|
51
|
+
when TYPE_REAL, TYPE_DOUBLE_PRECISION
|
52
|
+
value.to_f
|
53
|
+
|
54
|
+
when TYPE_MONEY, TYPE_NUMERIC
|
55
|
+
BigDecimal.new(value)
|
56
|
+
|
57
|
+
when TYPE_DATE, TYPE_TIMESTAMP_WITHOUT_TIME_ZONE, TYPE_TIMESTAMP_WITH_TIME_ZONE
|
58
|
+
DateTime.strptime(value, '%Y-%m-%d %H:%M:%S')
|
59
|
+
|
60
|
+
else
|
61
|
+
value
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
data/lib/rsconn.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'rsconn/postgres'
|
2
|
+
|
3
|
+
|
4
|
+
module Rsconn
|
5
|
+
class Redshift < Postgres
|
6
|
+
attr_reader :query_group, :query_slot_count
|
7
|
+
|
8
|
+
def initialize(jdbc_url, user, password, options={})
|
9
|
+
super
|
10
|
+
@query_group = options.fetch(:query_group, nil)
|
11
|
+
@query_slot_count = options.fetch(:query_slot_count, nil)
|
12
|
+
|
13
|
+
set_query_group(@query_group) if @query_group
|
14
|
+
set_query_slot_count(@query_slot_count) if @query_slot_count
|
15
|
+
end
|
16
|
+
|
17
|
+
def set_query_group(query_group)
|
18
|
+
execute("SET query_group TO #{query_group};")
|
19
|
+
end
|
20
|
+
|
21
|
+
def set_query_slot_count(count)
|
22
|
+
execute("SET wlm_query_slot_count TO #{count};")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
data/rsconn.gemspec
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rsconn/version'
|
5
|
+
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'rsconn'
|
9
|
+
spec.version = Rsconn::VERSION
|
10
|
+
spec.authors = ['Dean Morin']
|
11
|
+
spec.email = ['morin.dean@gmail.com']
|
12
|
+
spec.summary = 'Redshift wrapper.'
|
13
|
+
spec.description = <<-EOS
|
14
|
+
Convience wrapper for using Redshift.
|
15
|
+
EOS
|
16
|
+
spec.homepage = 'http://github.com/deanmorin/rsconn'
|
17
|
+
spec.license = 'The Unlicense'
|
18
|
+
|
19
|
+
spec.files = `git ls-files -z`.split("\x0")
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
21
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
22
|
+
spec.require_paths = ['lib']
|
23
|
+
|
24
|
+
spec.add_dependency 'pg', '~> 0.17'
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.6'
|
26
|
+
spec.add_development_dependency 'pry', '~> 0.10', '>= 0.10.1'
|
27
|
+
spec.add_development_dependency 'pry-nav', '~> 0.2', '>= 0.2.4'
|
28
|
+
spec.add_development_dependency 'rake', '~> 10'
|
29
|
+
spec.add_development_dependency 'rspec', '~> 3'
|
30
|
+
spec.add_development_dependency 'rspec-its', '~> 1'
|
31
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
module Rsconn
|
2
|
+
RSpec::Matchers.define :only_include_class do |klass|
|
3
|
+
match do |actual|
|
4
|
+
actual.result.all? {|row| row.keys.all? {|key| row[key].class == klass } }
|
5
|
+
end
|
6
|
+
|
7
|
+
failure_message do |actual|
|
8
|
+
"expected every value's class to be '#{klass}'"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'rspec/its'
|
2
|
+
require 'rsconn/jdbc_url'
|
3
|
+
|
4
|
+
|
5
|
+
module Rsconn
|
6
|
+
describe JdbcUrl do
|
7
|
+
HOST = 'host.redshift.amazonaws.com'
|
8
|
+
PORT = 5439
|
9
|
+
DATABASE = 'dbname'
|
10
|
+
JDBC_URL = "jdbc:postgresql://#{HOST}:#{PORT}/#{DATABASE}"
|
11
|
+
MYSQL_URL = "jdbc:mysql://#{HOST}:#{PORT}/#{DATABASE}"
|
12
|
+
|
13
|
+
describe '#new' do
|
14
|
+
|
15
|
+
context 'when a good url is given' do
|
16
|
+
subject { JdbcUrl.new(JDBC_URL) }
|
17
|
+
its(:url) { should eq(JDBC_URL) }
|
18
|
+
its(:host) { should eq(HOST) }
|
19
|
+
its(:port) { should eq(PORT) }
|
20
|
+
its(:database) { should eq(DATABASE) }
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'when a mysql url is given' do
|
24
|
+
subject { JdbcUrl.new(MYSQL_URL) }
|
25
|
+
it { expect { subject }.to raise_error(ArgumentError) }
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
require_relative 'dev_redshift'
|
2
|
+
|
3
|
+
|
4
|
+
module Rsconn
|
5
|
+
describe 'Redshift' do
|
6
|
+
|
7
|
+
before(:all) do
|
8
|
+
@db_conn = DevRedshift.new
|
9
|
+
end
|
10
|
+
|
11
|
+
after(:all) do
|
12
|
+
@db_conn.drop_table_if_exists('test.tmp_drop_if_exists')
|
13
|
+
@db_conn.drop_table_if_exists('test.tmp_test_table')
|
14
|
+
@db_conn.drop_view_if_exists('test.tmp_test_table_vw')
|
15
|
+
end
|
16
|
+
|
17
|
+
describe '#drop_table_if_exists' do
|
18
|
+
|
19
|
+
it 'ignores a missing table' do
|
20
|
+
@db_conn.drop_table_if_exists('test.i_dont_exist')
|
21
|
+
expect(@db_conn.error_occurred?).to be false
|
22
|
+
end
|
23
|
+
|
24
|
+
it 'drops an existing table' do
|
25
|
+
@db_conn.drop_table_if_exists('test.tmp_drop_if_exists')
|
26
|
+
@db_conn.execute('CREATE TABLE test.tmp_drop_if_exists (tmp boolean)')
|
27
|
+
@db_conn.drop_table_if_exists('test.tmp_drop_if_exists')
|
28
|
+
expect(@db_conn.error_occurred?).to be false
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'cascades when :cascade option is true' do
|
32
|
+
@db_conn.execute('CREATE TABLE test.tmp_test_table (tmp boolean)')
|
33
|
+
@db_conn.execute('CREATE VIEW test.tmp_test_table_vw AS SELECT * ' +
|
34
|
+
'FROM test.tmp_test_table')
|
35
|
+
@db_conn.drop_table_if_exists('test.tmp_test_table', :cascade => true)
|
36
|
+
sql = "SELECT count(*) FROM pg_views WHERE viewname = 'tmp_test_table_vw'"
|
37
|
+
|
38
|
+
count = @db_conn.query(sql).first['count']
|
39
|
+
expect(count).to eq(0)
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
describe '#drop_view_if_exists' do
|
44
|
+
|
45
|
+
it 'ignores a missing view' do
|
46
|
+
@db_conn.drop_view_if_exists('test.i_dont_exist_vw')
|
47
|
+
expect(@db_conn.error_occurred?).to be false
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'drops an existing view' do
|
51
|
+
@db_conn.drop_table_if_exists('test.tmp_test_table', :cascade => true)
|
52
|
+
@db_conn.execute('CREATE TABLE test.tmp_test_table (tmp boolean)')
|
53
|
+
@db_conn.execute('CREATE VIEW test.tmp_drop_if_exists_vw AS SELECT * ' +
|
54
|
+
'FROM test.tmp_test_table')
|
55
|
+
@db_conn.drop_view_if_exists('test.tmp_drop_if_exists_vw')
|
56
|
+
@db_conn.execute('DROP TABLE test.tmp_test_table')
|
57
|
+
expect(@db_conn.error_occurred?).to be false
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe '#get_field_by_type' do
|
62
|
+
|
63
|
+
it 'converts booleans correctly' do
|
64
|
+
@db_conn.execute('CREATE TABLE test.tmp_test_table (tmp boolean)')
|
65
|
+
@db_conn.execute('INSERT INTO test.tmp_test_table VALUES (true), (false)')
|
66
|
+
rows = @db_conn.query('SELECT * FROM test.tmp_test_table ORDER BY tmp')
|
67
|
+
@db_conn.drop_table_if_exists('test.tmp_test_table')
|
68
|
+
|
69
|
+
expect(rows[0]['tmp']).to be false
|
70
|
+
expect(rows[1]['tmp']).to be true
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
require 'bigdecimal'
|
2
|
+
require 'rspec/its'
|
3
|
+
require 'rsconn/typecast_result'
|
4
|
+
require_relative 'custom_matchers'
|
5
|
+
|
6
|
+
|
7
|
+
module Rsconn
|
8
|
+
class ResultStub < Array
|
9
|
+
|
10
|
+
def initialize
|
11
|
+
super(1)
|
12
|
+
end
|
13
|
+
|
14
|
+
def fname(index)
|
15
|
+
self.first.keys[index]
|
16
|
+
end
|
17
|
+
|
18
|
+
def ftype(index)
|
19
|
+
@ftypes[index]
|
20
|
+
end
|
21
|
+
|
22
|
+
def nfields
|
23
|
+
self.first.keys.length
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
class BooleanResultStub < ResultStub
|
28
|
+
def initialize
|
29
|
+
self[0] = { 'true' => 't', 'false' => 'f' }
|
30
|
+
@ftypes = [TypecastResult::TYPE_BOOLEAN, TypecastResult::TYPE_BOOLEAN]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
class BadBooleanResultStub < ResultStub
|
35
|
+
def initialize
|
36
|
+
self[0] = { 'bad' => 'true' }
|
37
|
+
@ftypes = [TypecastResult::TYPE_BOOLEAN]
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class IntegerResultStub < ResultStub
|
42
|
+
def initialize
|
43
|
+
self[0] = { 'c1' => '1', 'c2' => '2', 'c3' => '3', 'c4' => '4' }
|
44
|
+
@ftypes = [
|
45
|
+
TypecastResult::TYPE_BIGINT, TypecastResult::TYPE_SMALLINT,
|
46
|
+
TypecastResult::TYPE_INTEGER, TypecastResult::TYPE_OID
|
47
|
+
]
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
class FloatResultStub < ResultStub
|
52
|
+
def initialize
|
53
|
+
self[0] = { 'c1' => '-3', 'c2' => '4.111111111111111111' }
|
54
|
+
@ftypes = [TypecastResult::TYPE_REAL, TypecastResult::TYPE_DOUBLE_PRECISION]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
class BigDecimalResultStub < ResultStub
|
59
|
+
def initialize
|
60
|
+
self[0] = { 'c1' => '-3', 'c2' => '4.11111111111111111111111111111111111111111111111111' }
|
61
|
+
@ftypes = [TypecastResult::TYPE_MONEY, TypecastResult::TYPE_NUMERIC]
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
class DateTimeResultStub < ResultStub
|
66
|
+
def initialize
|
67
|
+
self[0] = {
|
68
|
+
'c1' => '1900-01-01 00:00:00',
|
69
|
+
'c2' => '3000-01-01 01:00:00',
|
70
|
+
'c3' => '4000-01-01 23:32:11'
|
71
|
+
}
|
72
|
+
@ftypes = [
|
73
|
+
TypecastResult::TYPE_DATE,
|
74
|
+
TypecastResult::TYPE_TIMESTAMP_WITHOUT_TIME_ZONE,
|
75
|
+
TypecastResult::TYPE_TIMESTAMP_WITH_TIME_ZONE
|
76
|
+
]
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
|
81
|
+
describe TypecastResult do
|
82
|
+
|
83
|
+
describe '#new' do
|
84
|
+
|
85
|
+
context "when there's booleans in the results" do
|
86
|
+
subject { TypecastResult.new(BooleanResultStub.new) }
|
87
|
+
let(:expected) { [{ 'true' => true, 'false' => false }] }
|
88
|
+
its(:result) { is_expected.to eq(expected) }
|
89
|
+
end
|
90
|
+
|
91
|
+
context "when there's an bad boolean value in the results" do
|
92
|
+
subject { TypecastResult.new(BadBooleanResultStub.new) }
|
93
|
+
it { expect { subject }.to raise_error(RuntimeError) }
|
94
|
+
end
|
95
|
+
|
96
|
+
context "when there's integers in the results" do
|
97
|
+
subject { TypecastResult.new(IntegerResultStub.new) }
|
98
|
+
let(:expected) { [{ 'c1' => 1, 'c2' => 2, 'c3' => 3, 'c4' => 4 }] }
|
99
|
+
its(:result) { is_expected.to eq(expected) }
|
100
|
+
it { is_expected.to only_include_class(Fixnum) }
|
101
|
+
end
|
102
|
+
|
103
|
+
context "when there's floating point numbers in the results" do
|
104
|
+
subject { TypecastResult.new(FloatResultStub.new) }
|
105
|
+
let(:expected) { [{ 'c1' => -3, 'c2' => 4.111111111111111111.to_f }] }
|
106
|
+
its(:result) { is_expected.to eq(expected) }
|
107
|
+
it { is_expected.to only_include_class(Float) }
|
108
|
+
end
|
109
|
+
|
110
|
+
context "when there's large or non-arbitrary precision numbers in the results" do
|
111
|
+
subject { TypecastResult.new(BigDecimalResultStub.new) }
|
112
|
+
it { is_expected.to only_include_class(BigDecimal) }
|
113
|
+
end
|
114
|
+
|
115
|
+
context "when there's timestamps in the results" do
|
116
|
+
subject { TypecastResult.new(DateTimeResultStub.new) }
|
117
|
+
it { is_expected.to only_include_class(DateTime) }
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
metadata
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rsconn
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Dean Morin
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-11-13 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: pg
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.17'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.6'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.6'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pry
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.10'
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: 0.10.1
|
51
|
+
type: :development
|
52
|
+
prerelease: false
|
53
|
+
version_requirements: !ruby/object:Gem::Requirement
|
54
|
+
requirements:
|
55
|
+
- - "~>"
|
56
|
+
- !ruby/object:Gem::Version
|
57
|
+
version: '0.10'
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: 0.10.1
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: pry-nav
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '0.2'
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: 0.2.4
|
71
|
+
type: :development
|
72
|
+
prerelease: false
|
73
|
+
version_requirements: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - "~>"
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0.2'
|
78
|
+
- - ">="
|
79
|
+
- !ruby/object:Gem::Version
|
80
|
+
version: 0.2.4
|
81
|
+
- !ruby/object:Gem::Dependency
|
82
|
+
name: rake
|
83
|
+
requirement: !ruby/object:Gem::Requirement
|
84
|
+
requirements:
|
85
|
+
- - "~>"
|
86
|
+
- !ruby/object:Gem::Version
|
87
|
+
version: '10'
|
88
|
+
type: :development
|
89
|
+
prerelease: false
|
90
|
+
version_requirements: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - "~>"
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '10'
|
95
|
+
- !ruby/object:Gem::Dependency
|
96
|
+
name: rspec
|
97
|
+
requirement: !ruby/object:Gem::Requirement
|
98
|
+
requirements:
|
99
|
+
- - "~>"
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '3'
|
102
|
+
type: :development
|
103
|
+
prerelease: false
|
104
|
+
version_requirements: !ruby/object:Gem::Requirement
|
105
|
+
requirements:
|
106
|
+
- - "~>"
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '3'
|
109
|
+
- !ruby/object:Gem::Dependency
|
110
|
+
name: rspec-its
|
111
|
+
requirement: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - "~>"
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '1'
|
116
|
+
type: :development
|
117
|
+
prerelease: false
|
118
|
+
version_requirements: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '1'
|
123
|
+
description: |2
|
124
|
+
Convience wrapper for using Redshift.
|
125
|
+
email:
|
126
|
+
- morin.dean@gmail.com
|
127
|
+
executables: []
|
128
|
+
extensions: []
|
129
|
+
extra_rdoc_files: []
|
130
|
+
files:
|
131
|
+
- ".gitignore"
|
132
|
+
- Gemfile
|
133
|
+
- LICENSE.txt
|
134
|
+
- README.md
|
135
|
+
- Rakefile
|
136
|
+
- lib/rsconn.rb
|
137
|
+
- lib/rsconn/jdbc_url.rb
|
138
|
+
- lib/rsconn/logger.rb
|
139
|
+
- lib/rsconn/postgres.rb
|
140
|
+
- lib/rsconn/typecast_result.rb
|
141
|
+
- lib/rsconn/version.rb
|
142
|
+
- rsconn.gemspec
|
143
|
+
- spec/custom_matchers.rb
|
144
|
+
- spec/dev_redshift.rb
|
145
|
+
- spec/jdbc_url_spec.rb
|
146
|
+
- spec/redshift_spec.rb
|
147
|
+
- spec/typecast_result_spec.rb
|
148
|
+
homepage: http://github.com/deanmorin/rsconn
|
149
|
+
licenses:
|
150
|
+
- The Unlicense
|
151
|
+
metadata: {}
|
152
|
+
post_install_message:
|
153
|
+
rdoc_options: []
|
154
|
+
require_paths:
|
155
|
+
- lib
|
156
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
157
|
+
requirements:
|
158
|
+
- - ">="
|
159
|
+
- !ruby/object:Gem::Version
|
160
|
+
version: '0'
|
161
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
162
|
+
requirements:
|
163
|
+
- - ">="
|
164
|
+
- !ruby/object:Gem::Version
|
165
|
+
version: '0'
|
166
|
+
requirements: []
|
167
|
+
rubyforge_project:
|
168
|
+
rubygems_version: 2.2.2
|
169
|
+
signing_key:
|
170
|
+
specification_version: 4
|
171
|
+
summary: Redshift wrapper.
|
172
|
+
test_files:
|
173
|
+
- spec/custom_matchers.rb
|
174
|
+
- spec/dev_redshift.rb
|
175
|
+
- spec/jdbc_url_spec.rb
|
176
|
+
- spec/redshift_spec.rb
|
177
|
+
- spec/typecast_result_spec.rb
|