fluent-plugin-sql
fluent-plugin-sql copied to clipboard
Update in_sql.rb
Adjusts according to suggestions in #43
This fixed our issues as well so I thought we'd PR it.
@Sjolus Thanks! Could you follow DCO instructions and add the appropriate test for this case?
@kenhys Looks like I've successfully done the signoff push required by DCO (My first commit was just an edit in the web ui, now pushed using SSH key)
In regards to setting up a test case I'm afraid I don't have the ruby skills to set it up. I can describe the scenario if that would help any future developer design a test but I'm a copy and paste solution man, not a ruby developer. Sorry! Feel free to close the PR if this is required for your practice.
It changes the behavior to the previous version, how about making it customizable? (something like this)
diff --git a/lib/fluent/plugin/in_sql.rb b/lib/fluent/plugin/in_sql.rb
index babec57..beaf4a3 100644
--- a/lib/fluent/plugin/in_sql.rb
+++ b/lib/fluent/plugin/in_sql.rb
@@ -51,6 +51,9 @@ module Fluent::Plugin
desc 'limit of number of rows for each SQL(optional)'
config_param :select_limit, :time, default: 500
+ desc 'fetch record in specified timezone'
+ config_param :time_zone, :enum, list: [:utc, :local], default: :utc
+
class TableElement
include Fluent::Configurable
@@ -192,6 +195,7 @@ module Fluent::Plugin
socket: @socket,
schema_search_path: @schema_search_path,
}
+ ActiveRecord::Base.default_timezone = @time_zone
# creates subclass of ActiveRecord::Base so that it can have different
# database configuration from ActiveRecord::Base.
And test like this:
diff --git a/test/plugin/test_in_sql_with_custom_time.rb b/test/plugin/test_in_sql_with_custom_time.rb
index 5bb7e95..e784685 100644
--- a/test/plugin/test_in_sql_with_custom_time.rb
+++ b/test/plugin/test_in_sql_with_custom_time.rb
@@ -7,6 +7,7 @@ class SqlInputCustomTimeTest < Test::Unit::TestCase
end
def teardown
+ Message.destroy_all
end
CONFIG = %[
@@ -108,6 +109,29 @@ class SqlInputCustomTimeTest < Test::Unit::TestCase
end
end
+ sub_test_case "timezone" do
+ def test_utc
+ d = create_driver(CONFIG + "select_interval 1 time_zone utc")
+ Message.create!(message: "message 1", updated_at: '2021-07-01 15:00:16.100758000 +0900')
+ d.end_if do
+ d.record_count >= 1
+ end
+ d.run(timeout: 5)
+ assert_equal("2021-07-01 06:00:16.100758+0000", d.events.first.last["updated_at"])
+ end
+
+ def test_local
+ d = create_driver(CONFIG + "select_interval 1 time_zone local")
+ stub(ActiveRecord::Base).default_timezone { 'Tokyo' }
+ Message.create!(message: "message 1", updated_at: '2021-07-01 15:00:16.100758000 +0900')
+ d.end_if do
+ d.record_count >= 1
+ end
+ d.run(timeout: 5)
+ assert_equal("2021-07-01 15:00:16.100758+0900", d.events.first.last["updated_at"])
+ end
+ end
+
class Message < ActiveRecord::Base
self.table_name = "messages_custom_time"
end
This patch solves my problem, time fields were losing time zone information and mis-converted to GMT
Adjusts according to suggestions in #43
This fixed our issues as well so I thought we'd PR it.
This fixes my issue too, time fields where mis-translated in GMT, loosing our time zone information