
CREATE SCHEMA IF NOT EXISTS ops;

CREATE TABLE IF NOT EXISTS ops.pos_idempotency_requests (
    LIKE public.pos_idempotency_requests INCLUDING ALL
);

TRUNCATE TABLE ops.pos_idempotency_requests;

INSERT INTO ops.pos_idempotency_requests
SELECT
    *
FROM public.pos_idempotency_requests;

CREATE SEQUENCE IF NOT EXISTS ops.pos_idempotency_requests_id_seq
    AS bigint
    OWNED BY ops.pos_idempotency_requests.id;

ALTER TABLE ops.pos_idempotency_requests
    ALTER COLUMN id SET DEFAULT nextval('ops.pos_idempotency_requests_id_seq'::regclass);

SELECT setval(
    'ops.pos_idempotency_requests_id_seq',
    COALESCE((SELECT MAX(id) FROM ops.pos_idempotency_requests), 1),
    COALESCE((SELECT MAX(id) FROM ops.pos_idempotency_requests), 0) > 0
);

CREATE TABLE IF NOT EXISTS ops.error_log (
    LIKE public.error_log INCLUDING ALL
);

TRUNCATE TABLE ops.error_log;

INSERT INTO ops.error_log
SELECT
    *
FROM public.error_log;

CREATE SEQUENCE IF NOT EXISTS ops.error_log_id_error_seq
    AS integer
    OWNED BY ops.error_log.id_error;

ALTER TABLE ops.error_log
    ALTER COLUMN id_error SET DEFAULT nextval('ops.error_log_id_error_seq'::regclass);

SELECT setval(
    'ops.error_log_id_error_seq',
    COALESCE((SELECT MAX(id_error) FROM ops.error_log), 1),
    COALESCE((SELECT MAX(id_error) FROM ops.error_log), 0) > 0
);

CREATE TABLE IF NOT EXISTS ops.webhook_ingress_events (
    LIKE public.webhook_ingress_events INCLUDING ALL
);

TRUNCATE TABLE ops.webhook_ingress_events;

INSERT INTO ops.webhook_ingress_events
SELECT
    *
FROM public.webhook_ingress_events;

CREATE SEQUENCE IF NOT EXISTS ops.webhook_ingress_events_id_seq
    AS bigint
    OWNED BY ops.webhook_ingress_events.id;

ALTER TABLE ops.webhook_ingress_events
    ALTER COLUMN id SET DEFAULT nextval('ops.webhook_ingress_events_id_seq'::regclass);

SELECT setval(
    'ops.webhook_ingress_events_id_seq',
    COALESCE((SELECT MAX(id) FROM ops.webhook_ingress_events), 1),
    COALESCE((SELECT MAX(id) FROM ops.webhook_ingress_events), 0) > 0
);

CREATE TABLE IF NOT EXISTS ops.webhook_ingress_dlq (
    LIKE public.webhook_ingress_dlq INCLUDING ALL
);

TRUNCATE TABLE ops.webhook_ingress_dlq;

INSERT INTO ops.webhook_ingress_dlq
SELECT
    *
FROM public.webhook_ingress_dlq;

CREATE SEQUENCE IF NOT EXISTS ops.webhook_ingress_dlq_id_seq
    AS bigint
    OWNED BY ops.webhook_ingress_dlq.id;

ALTER TABLE ops.webhook_ingress_dlq
    ALTER COLUMN id SET DEFAULT nextval('ops.webhook_ingress_dlq_id_seq'::regclass);

SELECT setval(
    'ops.webhook_ingress_dlq_id_seq',
    COALESCE((SELECT MAX(id) FROM ops.webhook_ingress_dlq), 1),
    COALESCE((SELECT MAX(id) FROM ops.webhook_ingress_dlq), 0) > 0
);

ANALYZE ops.pos_idempotency_requests;
ANALYZE ops.error_log;
ANALYZE ops.webhook_ingress_events;
ANALYZE ops.webhook_ingress_dlq;
