Skip to content

Commit

Permalink
Merge pull request #34751 from dimagi/dm/sql-repeatrecord
Browse files Browse the repository at this point in the history
Couch to SQL Repeat Records: post-PR-3 migrations
  • Loading branch information
millerdev authored Jun 20, 2024
2 parents c9292f1 + a858ee8 commit 7ef55ca
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
SELECT setval('repeaters_repeater_id_seq', NEW.value, true);
"""
UNDO_REPEATER_SEQ = """
DROP VIEW repeaters_repeater_id_seq_view;
DROP VIEW IF EXISTS repeaters_repeater_id_seq_view;
"""

REPEATERS_APP_LABEL = "repeaters"
Expand Down
9 changes: 8 additions & 1 deletion corehq/motech/repeaters/migrations/0002_repeaters_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,14 @@ class Migration(migrations.Migration):

operations = [
# Cannot be applied until the repeaters database has been initialized
# and migrated. The migration should be applied with:
# and migrated.
#
# After creating the new "repeaters" database and adding it to
# DATABASES and LOCAL_CUSTOM_DB_ROUTING, a "repeaters_fdw_user"
# must also be created on the postgres cluster.
# See https://github.com/dimagi/commcare-cloud/pull/5989
#
# Then the migration should be applied with:
#
# ./manage.py migrate --database=repeaters # initialize repeaters db
# ./manage.py migrate # create repeaters FDW
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
# Generated by Django 3.2.25 on 2024-04-02 14:38

from django.conf import settings
from django.db import migrations
from django.db.utils import DEFAULT_DB_ALIAS


REPEATERS_APP_LABEL = "repeaters"


class RepeatersFdwMigration(migrations.RunSQL):

def database_forwards(self, app_label, schema_editor, from_state, to_state):
if self.should_apply(schema_editor):
self._run_sql(schema_editor, self.sql.format(**self.context))

def database_backwards(self, app_label, schema_editor, from_state, to_state):
if self.should_apply(schema_editor):
self._run_sql(schema_editor, self.reverse_sql.format(**self.context))

def should_apply(self, schema_editor):
db_alias = schema_editor.connection.alias
return db_alias == DEFAULT_DB_ALIAS and repeaters_db() != DEFAULT_DB_ALIAS

@property
def context(self):
db = repeaters_db()
assert db != DEFAULT_DB_ALIAS
return settings.DATABASES[db]


def repeaters_db():
return settings.CUSTOM_DB_ROUTING.get(REPEATERS_APP_LABEL, DEFAULT_DB_ALIAS)


class Migration(migrations.Migration):

dependencies = [
('repeaters', '0010_rm_couch_artifacts'),
]

operations = [
RepeatersFdwMigration(
# NOTE does not restore dropped entities on reverse.
"""
DROP SEQUENCE IF EXISTS "repeaters_repeater_id_seq";
DROP SERVER IF EXISTS repeaters_fdw CASCADE;
DROP FUNCTION IF EXISTS repeaters_repeater_id_seq_nextval;
""",
reverse_sql=""
),
migrations.RunSQL(
sql="""
DROP INDEX IF EXISTS "unique_couch_id";
ALTER TABLE "repeaters_repeatrecord" DROP COLUMN "couch_id" CASCADE;
ALTER TABLE "repeaters_repeater"
DROP CONSTRAINT "repeaters_repeater_id_key",
DROP CONSTRAINT "id_eq",
DROP COLUMN "id" CASCADE,
DROP COLUMN "repeater_id";
ALTER TABLE "repeaters_repeatrecord" DROP COLUMN "repeater_id";
DROP TRIGGER repeaters_repeater_default_id ON repeaters_repeater;
DROP FUNCTION set_default_repeaters_repeater_id();
""",
# NOTE reverse works only on databases with no repeaters (test databases)
reverse_sql="""
ALTER TABLE "repeaters_repeatrecord" ADD COLUMN "couch_id" varchar(36) NULL;
CREATE UNIQUE INDEX "unique_couch_id" ON "repeaters_repeatrecord" ("couch_id")
WHERE "couch_id" IS NOT NULL;
CREATE FUNCTION set_default_repeaters_repeater_id()
RETURNS trigger LANGUAGE plpgsql AS $BODY$
BEGIN
IF NEW.id_ IS NULL THEN
NEW.id_ = NEW.repeater_id::uuid;
ELSIF NEW.repeater_id IS NULL THEN
NEW.repeater_id = REPLACE(NEW.id_::varchar, '-', '');
END IF;
RETURN NEW;
END
$BODY$;
CREATE TRIGGER repeaters_repeater_default_id BEFORE INSERT ON repeaters_repeater
FOR EACH ROW EXECUTE FUNCTION set_default_repeaters_repeater_id();
ALTER TABLE "repeaters_repeatrecord" ADD COLUMN "repeater_id" integer;
CREATE INDEX repeaters_repeatrecord_repeater_id_01b51f9d
ON repeaters_repeatrecord USING btree (repeater_id);
ALTER TABLE "repeaters_repeater"
ADD COLUMN "repeater_id" varchar(36) NOT NULL,
ADD COLUMN "id" serial NOT NULL,
ADD CONSTRAINT id_eq CHECK ("id_" = "repeater_id"::uuid),
ADD CONSTRAINT "repeaters_repeater_id_key" UNIQUE ("id");
ALTER TABLE ONLY repeaters_repeater
ADD CONSTRAINT repeaters_repeater_repeater_id_9ab445dc_uniq UNIQUE (repeater_id);
CREATE INDEX repeaters_repeater_repeater_id_9ab445dc_like
ON repeaters_repeater USING btree (repeater_id varchar_pattern_ops);
""",
),
]
1 change: 1 addition & 0 deletions migrations.lock
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,7 @@ repeaters
0008_sqlrepeatrecords
0009_add_domain_to_indexes
0010_rm_couch_artifacts
0011_remove_obsolete_entities
reports
0001_initial
0002_auto_20171121_1803
Expand Down

0 comments on commit 7ef55ca

Please sign in to comment.