@@ -173,6 +173,8 @@ def init_change_tracking(self, table_config, last_known_sync_version):
173173 # c) whether a full refresh is needed - this is a derivative of the validity of the last known sync version
174174 # because if the last known sync version is no longer valid, then our target data is in-an-invalid-state /
175175 # 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.
176178 #
177179 # the following help us determining the above:
178180 # 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):
183185 # c) min_valid_version: the minimum version that is valid for use in obtaining change tracking information from
184186 # the specified table.
185187 # 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).
186190
187191 get_change_tracking_info_sql = f"" \
188192 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):
193197 f" CASE WHEN @last_known_sync_version >= @min_valid_version THEN 1 ELSE 0 END; \n " \
194198 f"DECLARE @last_sync_version BIGINT; \n " \
195199 f"DECLARE @force_full_load BIT; \n " \
200+ f"DECLARE @data_changed_since_last_sync BIT; \n " \
196201 f" \n " \
197202 f"IF @last_known_sync_version_is_valid = 1 \n " \
198203 f"BEGIN \n " \
@@ -205,17 +210,32 @@ def init_change_tracking(self, table_config, last_known_sync_version):
205210 f" SET @last_sync_version = 0; \n " \
206211 f"END \n " \
207212 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 "
211230
212231 self .logger .debug (f"Getting ChangeTracking info for { table_config ['schema' ]} .{ table_config ['name' ]} .\n "
213232 f"{ get_change_tracking_info_sql } " )
214233
215234 result = self .database_engine .execute (text (get_change_tracking_info_sql ))
216235 row = result .fetchone ()
217236
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" ])
219239
220240 @staticmethod
221241 def build_where_clause (batch_key_tracker , table_alias ):
0 commit comments