-- Copyright (C) 2026 Eric Seigne <eric.seigne@cap-rel.fr>
--
-- This program 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 of the License, or
-- (at your option) any later version.
--
-- This program 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 this program. If not, see <https://www.gnu.org/licenses/>.

-- Evolution of existing installations towards the entity-aware schema of
-- llx_facturx_apimport (fresh installations get it from llx_facturx_apimport.sql
-- and its .key.sql): the single-entity unique index on facturxid is replaced by
-- a (facturxid, entity) pair and the reference becomes unique per entity, which
-- closes the numbering race of APImport::getNextValue(). run_sql() replays this
-- file at every activation: statements failing because the object already
-- exists are expected and tolerated.

ALTER TABLE llx_facturx_apimport MODIFY entity integer DEFAULT 1 NOT NULL;
ALTER TABLE llx_facturx_apimport DROP INDEX facturxid;
ALTER TABLE llx_facturx_apimport ADD UNIQUE INDEX uk_facturx_apimport_facturxid (facturxid, entity);
ALTER TABLE llx_facturx_apimport ADD UNIQUE INDEX uk_facturx_apimport_ref (ref, entity);
ALTER TABLE llx_facturx_apimport ADD INDEX idx_facturx_apimport_entity (entity);
