-
-
Notifications
You must be signed in to change notification settings - Fork 218
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #34751 from dimagi/dm/sql-repeatrecord
Couch to SQL Repeat Records: post-PR-3 migrations
- Loading branch information
Showing
4 changed files
with
109 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
corehq/motech/repeaters/migrations/0011_remove_obsolete_entities.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
""", | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters