activerecord-redshift-adapter 0.9.0 → 0.9.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,4 @@
1
- activerecord-redshift-adapter
2
- =============================
1
+ # activerecord-redshift-adapter
3
2
 
4
3
  adapter for aws redshift for rails 3
5
4
 
@@ -9,9 +8,8 @@ barely tested (I'm working on a project that needs this -- this works as much as
9
8
 
10
9
  good luck
11
10
 
12
- example database.yml
13
- ====================
14
-
11
+ ## example database.yml
12
+ ```yml
15
13
  common: &common
16
14
  adapter: postgresql
17
15
  username: postgres
@@ -32,3 +30,20 @@ redshift_development:
32
30
  <<: *common
33
31
  <<: *redshiftdb
34
32
  database: databasename
33
+ ```
34
+
35
+ ## options
36
+ <table>
37
+ <tr>
38
+ <th>option</th>
39
+ <th>description</th>
40
+ </tr>
41
+ <tr>
42
+ <th>schema_search_path</th>
43
+ <td>set schema_search_path. use default value if not given.</td>
44
+ </tr>
45
+ <tr>
46
+ <th>read_timezone</th>
47
+ <td>force timezone for datetime when select values. ActiveRecord default timezone will set if not given.</td>
48
+ </tr>
49
+ </table>
@@ -32,8 +32,10 @@ module ActiveRecord
32
32
  module ConnectionAdapters
33
33
  # Redshift-specific extensions to column definitions in a table.
34
34
  class RedshiftColumn < Column #:nodoc:
35
+ attr_accessor :timezone
35
36
  # Instantiates a new Redshift column definition in a table.
36
- def initialize(name, default, sql_type = nil, null = true)
37
+ def initialize(name, default, sql_type = nil, null = true, timezone = nil)
38
+ self.timezone = timezone
37
39
  super(name, self.class.extract_value_from_default(default), sql_type, null)
38
40
  end
39
41
 
@@ -41,6 +43,25 @@ module ActiveRecord
41
43
  name == other.name && default == other.default && sql_type == other.sql_type && null == other.null
42
44
  end
43
45
 
46
+ def type_cast(value)
47
+ return nil if value.nil?
48
+ return coder.load(value) if encoded?
49
+
50
+ if timezone && [:datetime, :timestamp].include?(type)
51
+ self.class.string_to_time_with_timezone(string, timezone)
52
+ else
53
+ super
54
+ end
55
+ end
56
+
57
+ def type_cast_code(var_name)
58
+ if timezone && [:datetime, :timestamp].include?(type)
59
+ "#{self.class.name}.string_to_time_with_timezone(#{var_name},'#{timezone}')"
60
+ else
61
+ super
62
+ end
63
+ end
64
+
44
65
  # :stopdoc:
45
66
  class << self
46
67
  attr_accessor :money_precision
@@ -54,6 +75,14 @@ module ActiveRecord
54
75
  super
55
76
  end
56
77
  end
78
+
79
+ def string_to_time_with_timezone(string, timezone)
80
+ if string =~ self::Format::ISO_DATETIME
81
+ Time.parse("#{string} #{timezone}")
82
+ else
83
+ string_to_time(string)
84
+ end
85
+ end
57
86
  end
58
87
  # :startdoc:
59
88
 
@@ -815,7 +844,7 @@ module ActiveRecord
815
844
  def columns(table_name, name = nil)
816
845
  # Limit, precision, and scale are all handled by the superclass.
817
846
  column_definitions(table_name).collect do |column_name, type, default, notnull|
818
- RedshiftColumn.new(column_name, default, type, notnull == 'f')
847
+ RedshiftColumn.new(column_name, default, type, notnull == 'f', @config[:read_timezone])
819
848
  end
820
849
  end
821
850
 
@@ -1,4 +1,4 @@
1
1
  module ActiverecordRedshiftAdapter
2
2
  # the current version of this gem
3
- VERSION = "0.9.0"
3
+ VERSION = "0.9.1"
4
4
  end
@@ -1,8 +1,37 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe ActiveRecord::Base do
4
- it "opens a connection" do
5
- connection = ActiveRecord::Base.redshift_connection(TEST_CONNECTION_HASH)
6
- connection.active?.should be_true
4
+ let(:connection) { ActiveRecord::Base.redshift_connection(TEST_CONNECTION_HASH) }
5
+
6
+ describe '.redshift_connection' do
7
+ it "opens a connection" do
8
+ expect(connection).to be_active
9
+ end
10
+ end
11
+
12
+ describe 'set UTC timezone for datetime' do
13
+ class TimezoneTest < ActiveRecord::Base
14
+ default_timezone = :jst
15
+ establish_connection(TEST_CONNECTION_HASH.merge('adapter' => 'redshift', 'read_timezone' => 'UTC'))
16
+ end
17
+
18
+ before do
19
+ connection.query <<-SQL
20
+ CREATE TABLE public.timezone_tests ( "id" INTEGER NULL, "created_at" TIMESTAMP NULL );
21
+ INSERT INTO public.timezone_tests VALUES (1, '2013-07-01 12:00:00');
22
+ SQL
23
+ end
24
+
25
+ after do
26
+ connection.query <<-SQL
27
+ DROP TABLE public.timezone_tests;
28
+ SQL
29
+ end
30
+
31
+ it 'returns timestamp as UTC' do
32
+ data = TimezoneTest.all.first
33
+ expect(data.created_at.zone).to eq 'UTC'
34
+ expect(data.created_at).to eq Time.parse '2013-07-01 12:00:00 UTC'
35
+ end
7
36
  end
8
37
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-redshift-adapter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -68,7 +68,7 @@ files:
68
68
  - .gitignore
69
69
  - Gemfile
70
70
  - LICENSE
71
- - README
71
+ - README.md
72
72
  - Rakefile
73
73
  - activerecord-redshift-adapter.gemspec
74
74
  - lib/active_record/connection_adapters/redshift_adapter.rb