--
-- This file is part of TALER
-- Copyright (C) 2026 Taler Systems SA
--
-- TALER is free software; you can redistribute it and/or modify it under the
-- terms of the GNU General Public License as published by the Free Software
-- Foundation; either version 3, or (at your option) any later version.
--
-- TALER is distributed in the hope that it will be useful, but WITHOUT ANY
-- WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-- A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License along with
-- TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>
--

-- Track how far a worker has come *within* a shard, so that the work it
-- committed can be recorded long before the shard as a whole is finished, and
-- so that a worker picking the shard up later resumes instead of restarting.
ALTER TABLE work_shards
  ADD COLUMN progress_row INT8;

-- Existing rows have no recorded progress. Starting them over is always safe
-- (every job using this table is idempotent, it just costs extra work), so
-- point them at the beginning of their shard.
UPDATE work_shards
   SET progress_row=start_row;

ALTER TABLE work_shards
  ALTER COLUMN progress_row SET NOT NULL;

COMMENT ON COLUMN work_shards.progress_row
  IS 'rows in [start_row,progress_row) of this shard have been processed and committed; equal to start_row while nothing has been done yet and to end_row once the shard is complete. Only ever moves forward, so a second worker that concurrently grabbed the same shard cannot rewind it.';
