Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migration to update id from int to bigint #433

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions temba/request_logs/migrations/0018_http_id_bigint.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
from django.db import migrations

SQL = """
ALTER TABLE public.request_logs_httplog ADD COLUMN new_id BIGINT;

CREATE FUNCTION set_new_id() RETURNS TRIGGER AS
$BODY$
BEGIN
NEW.new_id := NEW.id;
RETURN NEW;
END
$BODY$ LANGUAGE PLPGSQL;

CREATE TRIGGER set_new_id_trigger
BEFORE INSERT OR UPDATE ON public.request_logs_httplog
FOR EACH ROW EXECUTE PROCEDURE set_new_id();

DO $$
DECLARE
batch_size INTEGER := 10000; -- Define o tamanho do batch
min_id BIGINT; -- Armazena o menor valor de id no batch atual
max_id BIGINT; -- Armazena o maior valor de id na tabela
rows_updated INTEGER; -- Contador de linhas atualizadas em cada batch
BEGIN

SELECT MIN(id) INTO min_id FROM public.request_logs_httplog;

SELECT MAX(id) INTO max_id FROM public.request_logs_httplog;

WHILE min_id <= max_id LOOP

UPDATE public.request_logs_httplog
SET new_id = id
WHERE id >= min_id AND id < min_id + batch_size
AND new_id IS DISTINCT FROM id;

GET DIAGNOSTICS rows_updated = ROW_COUNT;

IF rows_updated = 0 THEN
EXIT;
END IF;

min_id := min_id + batch_size;

PERFORM pg_sleep(0.1);
END LOOP;
END $$;

CREATE UNIQUE INDEX IF NOT EXISTS new_id_unique ON public.request_logs_httplog(new_id);

ALTER TABLE public.request_logs_httplog ADD CONSTRAINT new_id_not_null CHECK (new_id IS NOT NULL) NOT VALID;

ALTER TABLE public.request_logs_httplog VALIDATE CONSTRAINT new_id_not_null;

BEGIN TRANSACTION;

LOCK TABLE public.request_logs_httplog IN EXCLUSIVE MODE;

ALTER TABLE public.request_logs_httplog
DROP CONSTRAINT request_logs_httplog_pkey,
ADD CONSTRAINT request_logs_httplog_pkey PRIMARY KEY USING INDEX new_id_unique;

ALTER SEQUENCE public.request_logs_httplog_id_seq OWNED BY public.request_logs_httplog.new_id;

ALTER TABLE public.request_logs_httplog ALTER COLUMN new_id SET DEFAULT nextval('public.request_logs_httplog_id_seq');

ALTER TABLE public.request_logs_httplog DROP COLUMN id;

ALTER TABLE public.request_logs_httplog RENAME COLUMN new_id TO id;

DROP TRIGGER IF EXISTS set_new_id_trigger ON public.request_logs_httplog;
DROP FUNCTION IF EXISTS set_new_id();

COMMIT;
"""


class Migration(migrations.Migration):
dependencies = [
("request_logs", "0017_httplog_contact"),
]

operations = [migrations.RunSQL(SQL)]
Loading