@@ -173,6 +173,8 @@ def init_change_tracking(self, table_config, last_known_sync_version):
173
173
# c) whether a full refresh is needed - this is a derivative of the validity of the last known sync version
174
174
# because if the last known sync version is no longer valid, then our target data is in-an-invalid-state /
175
175
# out-of-sync and a full refresh must be forced to sync the data.
176
+ # d) whether any data has changed for the table synce the last sync - this is a derivative of the results of
177
+ # the CHANGETABLE() call, which we perform later and join to the table to load in the changed rows.
176
178
#
177
179
# the following help us determining the above:
178
180
# a) sync_version: the current version of change tracking at source database.
@@ -183,6 +185,8 @@ def init_change_tracking(self, table_config, last_known_sync_version):
183
185
# c) min_valid_version: the minimum version that is valid for use in obtaining change tracking information from
184
186
# the specified table.
185
187
# it's value IS sourced from CHANGE_TRACKING_MIN_VALID_VERSION(OBJECT_ID(..)).
188
+ # d) data_changed_since_last_sync: whether or not data has changed in the table since last_known_sync_version
189
+ # it's value is sourced from CHANGETABLE(table, last_known_sync_version).
186
190
187
191
get_change_tracking_info_sql = f"" \
188
192
f"DECLARE @sync_version BIGINT = CHANGE_TRACKING_CURRENT_VERSION(); \n " \
@@ -193,6 +197,7 @@ def init_change_tracking(self, table_config, last_known_sync_version):
193
197
f" CASE WHEN @last_known_sync_version >= @min_valid_version THEN 1 ELSE 0 END; \n " \
194
198
f"DECLARE @last_sync_version BIGINT; \n " \
195
199
f"DECLARE @force_full_load BIT; \n " \
200
+ f"DECLARE @data_changed_since_last_sync BIT; \n " \
196
201
f" \n " \
197
202
f"IF @last_known_sync_version_is_valid = 1 \n " \
198
203
f"BEGIN \n " \
@@ -205,17 +210,32 @@ def init_change_tracking(self, table_config, last_known_sync_version):
205
210
f" SET @last_sync_version = 0; \n " \
206
211
f"END \n " \
207
212
f" \n " \
208
- f"SELECT @sync_version AS sync_version \n " \
209
- f", @last_sync_version AS last_sync_version \n " \
210
- f", @force_full_load AS force_full_load; \n "
213
+ f"IF EXISTS ( " \
214
+ f" SELECT 1" \
215
+ f" FROM CHANGETABLE(CHANGES " \
216
+ f" { table_config ['schema' ]} .{ table_config ['name' ]} , { last_known_sync_version } ) " \
217
+ f" as c )" \
218
+ f"BEGIN \n " \
219
+ f" SET @data_changed_since_last_sync = 1; \n " \
220
+ f"END \n " \
221
+ f"ELSE \n " \
222
+ f"BEGIN \n " \
223
+ f" SET @data_changed_since_last_sync = 0; \n " \
224
+ f"END \n " \
225
+ f" \n " \
226
+ f"SELECT @sync_version AS sync_version \n " \
227
+ f", @last_sync_version AS last_sync_version \n " \
228
+ f", @force_full_load AS force_full_load \n " \
229
+ f", @data_changed_since_last_sync AS data_changed_since_last_sync; \n "
211
230
212
231
self .logger .debug (f"Getting ChangeTracking info for { table_config ['schema' ]} .{ table_config ['name' ]} .\n "
213
232
f"{ get_change_tracking_info_sql } " )
214
233
215
234
result = self .database_engine .execute (text (get_change_tracking_info_sql ))
216
235
row = result .fetchone ()
217
236
218
- return ChangeTrackingInfo (row ["last_sync_version" ], row ["sync_version" ], row ["force_full_load" ])
237
+ return ChangeTrackingInfo ( row ["last_sync_version" ], row ["sync_version" ],
238
+ row ["force_full_load" ], row ["data_changed_since_last_sync" ])
219
239
220
240
@staticmethod
221
241
def build_where_clause (batch_key_tracker , table_alias ):
0 commit comments