Skip to content

Commit d9f928a

Browse files
committed
Add support for "uuid" type in tracking_column_type
* Added "uuid" type to tracking_column_type settings * Updated relevant documentation * Added tests for "uuid" type * Ensured compatibility with existing types
1 parent 587c54a commit d9f928a

File tree

4 files changed

+51
-19
lines changed

4 files changed

+51
-19
lines changed

docs/input-jdbc.asciidoc

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ Here is the list:
127127

128128
|==========================================================
129129
|sql_last_value | The value used to calculate which rows to query. Before any query is run,
130-
this is set to Thursday, 1 January 1970, or 0 if `use_column_value` is true and
130+
this is set to Thursday, 1 January 1970, 0, or 00000000-0000-0000-0000-000000000000 if `use_column_value` is true and
131131
`tracking_column` is set. It is updated accordingly after subsequent queries are run.
132132
|offset, size| Values used with manual paging mode to explicitly implement the paging.
133133
Supported only if <<plugins-{type}s-{plugin}-jdbc_paging_enabled>> is enabled and
@@ -240,7 +240,7 @@ This plugin supports the following configuration options plus the <<plugins-{typ
240240
| <<plugins-{type}s-{plugin}-statement_filepath>> |a valid filesystem path|No
241241
| <<plugins-{type}s-{plugin}-target>> | {logstash-ref}/field-references-deepdive.html[field reference] | No
242242
| <<plugins-{type}s-{plugin}-tracking_column>> |<<string,string>>|No
243-
| <<plugins-{type}s-{plugin}-tracking_column_type>> |<<string,string>>, one of `["numeric", "timestamp"]`|No
243+
| <<plugins-{type}s-{plugin}-tracking_column_type>> |<<string,string>>, one of `["numeric", "timestamp", "uuid"]`|No
244244
| <<plugins-{type}s-{plugin}-use_column_value>> |<<boolean,boolean>>|No
245245
| <<plugins-{type}s-{plugin}-use_prepared_statements>> |<<boolean,boolean>>|No
246246
|=======================================================================
@@ -645,10 +645,24 @@ The column whose value is to be tracked if `use_column_value` is set to `true`
645645
[id="plugins-{type}s-{plugin}-tracking_column_type"]
646646
===== `tracking_column_type`
647647

648-
* Value can be any of: `numeric`, `timestamp`
648+
* Value can be any of: `numeric`, `timestamp`, `uuid`
649649
* Default value is `"numeric"`
650650

651-
Type of tracking column. Currently only "numeric" and "timestamp"
651+
Type of tracking column. Currently only "numeric", "timestamp" and "uuid"
652+
653+
NOTE: Example of uuid type usage in PostgreSQL.
654+
[source,ruby]
655+
---------------------------------------------------------------------------------------------------
656+
input {
657+
jdbc {
658+
statement => "SELECT id, mycolumn1, mycolumn2 FROM my_table WHERE id > :sql_last_value::uuid"
659+
use_column_value => true
660+
tracking_column => "id"
661+
tracking_column_type => "uuid"
662+
# ... other configuration bits
663+
}
664+
}
665+
---------------------------------------------------------------------------------------------------
652666

653667
[id="plugins-{type}s-{plugin}-use_column_value"]
654668
===== `use_column_value`

lib/logstash/inputs/jdbc.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ module LogStash module Inputs class Jdbc < LogStash::Inputs::Base
191191
# If tracking column value rather than timestamp, the column whose value is to be tracked
192192
config :tracking_column, :validate => :string
193193

194-
# Type of tracking column. Currently only "numeric" and "timestamp"
195-
config :tracking_column_type, :validate => ['numeric', 'timestamp'], :default => 'numeric'
194+
# Type of tracking column. Currently only "numeric", "timestamp" and "uuid"
195+
config :tracking_column_type, :validate => ['numeric', 'timestamp', 'uuid'], :default => 'numeric'
196196

197197
# Whether the previous run state should be preserved
198198
config :clean_run, :validate => :boolean, :default => false

lib/logstash/plugin_mixins/jdbc/value_tracking.rb

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,21 @@ def self.build_last_value_tracker(plugin)
1212
handler.clean
1313
end
1414

15-
if plugin.use_column_value && plugin.tracking_column_type == "numeric"
16-
# use this irrespective of the jdbc_default_timezone setting
17-
NumericValueTracker.new(handler)
18-
else
19-
if plugin.jdbc_default_timezone.nil?
20-
# no TZ stuff for Sequel, use Time
21-
TimeValueTracker.new(handler)
15+
if plugin.use_column_value
16+
case plugin.tracking_column_type
17+
when "numeric"
18+
# use this irrespective of the jdbc_default_timezone setting
19+
NumericValueTracker.new(handler)
20+
when "uuid"
21+
UuidValueTracker.new(handler)
2222
else
23-
# Sequel does timezone handling on DateTime only
24-
DateTimeValueTracker.new(handler)
23+
if plugin.jdbc_default_timezone.nil?
24+
# no TZ stuff for Sequel, use Time
25+
TimeValueTracker.new(handler)
26+
else
27+
# Sequel does timezone handling on DateTime only
28+
DateTimeValueTracker.new(handler)
29+
end
2530
end
2631
end
2732
end
@@ -34,7 +39,7 @@ def initialize(handler)
3439
end
3540

3641
if Psych::VERSION&.split('.')&.first.to_i >= 4
37-
YAML_PERMITTED_CLASSES = [::DateTime, ::Time, ::BigDecimal].freeze
42+
YAML_PERMITTED_CLASSES = [::DateTime, ::Time, ::BigDecimal, ::String].freeze
3843
def self.load_yaml(source)
3944
Psych::safe_load(source, permitted_classes: YAML_PERMITTED_CLASSES)
4045
end
@@ -81,6 +86,17 @@ def set_value(value)
8186
end
8287
end
8388

89+
class UuidValueTracker < ValueTracking
90+
def set_initial
91+
common_set_initial(:uuid, "00000000-0000-0000-0000-000000000000")
92+
end
93+
94+
def set_value(value)
95+
return unless value.is_a?(String)
96+
@value = value
97+
end
98+
end
99+
84100
class DateTimeValueTracker < ValueTracking
85101
def set_initial
86102
common_set_initial(:to_datetime, DateTime.new(1970))

spec/inputs/jdbc_spec.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@
3535
db.create_table :test_table do
3636
DateTime :created_at
3737
BigDecimal :big_num
38+
String :uuid
3839
Integer :num
3940
String :string
4041
DateTime :custom_time
4142
end
42-
db << "CREATE TABLE types_table (num INTEGER, string VARCHAR(255), started_at DATE, custom_time TIMESTAMP, ranking DECIMAL(16,6))"
43+
db << "CREATE TABLE types_table (num INTEGER, string VARCHAR(255), started_at DATE, custom_time TIMESTAMP, ranking DECIMAL(16,6), uuid VARCHAR(36))"
4344
db << "CREATE TABLE test1_table (num INTEGER, string VARCHAR(255), custom_time TIMESTAMP, created_at TIMESTAMP)"
4445
end
4546
end
@@ -1522,7 +1523,7 @@
15221523
end
15231524

15241525
before do
1525-
db << "INSERT INTO types_table (num, string, started_at, custom_time, ranking) VALUES (1, 'A test', '1999-12-31', '1999-12-31 23:59:59', 95.67)"
1526+
db << "INSERT INTO types_table (num, string, started_at, custom_time, ranking, uuid) VALUES (1, 'A test', '1999-12-31', '1999-12-31 23:59:59', 95.67, '018f15f3-6cfd-7a1b-b70f-d97ed8a73128')"
15261527

15271528
plugin.register
15281529
end
@@ -1539,6 +1540,7 @@
15391540
expect(event.get("started_at")).to be_a_logstash_timestamp_equivalent_to("1999-12-31T00:00:00.000Z")
15401541
expect(event.get("custom_time")).to be_a_logstash_timestamp_equivalent_to("1999-12-31T23:59:59.000Z")
15411542
expect(event.get("ranking").to_f).to eq(95.67)
1543+
expect(event.get("uuid")).to eq("018f15f3-6cfd-7a1b-b70f-d97ed8a73128")
15421544
end
15431545
end
15441546

@@ -1552,7 +1554,7 @@
15521554
end
15531555

15541556
before(:each) do
1555-
db << "INSERT INTO types_table (num, string, started_at, custom_time, ranking) VALUES (1, 'A test', '1999-12-31', '2021-11-07 01:23:45', 95.67)"
1557+
db << "INSERT INTO types_table (num, string, started_at, custom_time, ranking, uuid) VALUES (1, 'A test', '1999-12-31', '2021-11-07 01:23:45', 95.67, '018f15f3-6cfd-7a1b-b70f-d97ed8a73128')"
15561558
plugin.register
15571559
end
15581560

0 commit comments

Comments
 (0)