Skip to content

Instantly share code, notes, and snippets.

@saxix
Created July 15, 2020 14:11
Show Gist options
  • Save saxix/bfdeef94261d5bfcdbebf3abc485b65d to your computer and use it in GitHub Desktop.
Save saxix/bfdeef94261d5bfcdbebf3abc485b65d to your computer and use it in GitHub Desktop.
# Generated by Django 2.2.8 on 2020-07-10 13:12
from django.db import migrations
def reverse(apps, schema_editor):
pass
class Migration(migrations.Migration):
dependencies = [
("household", "0011_migration"),
]
operations = [
migrations.RunSQL(
"ALTER TABLE household_household ADD unicef_id_index SERIAL"
),
migrations.RunSQL(
"ALTER TABLE household_individual ADD unicef_id_index SERIAL"
),
migrations.RunSQL(
"""
CREATE OR REPLACE FUNCTION create_hh_unicef_id() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
NEW.unicef_id := format('HH-%s-%s',NEW.country,LPAD(format('%s',NEW.unicef_id_index),8,'0'));
return NEW;
end
$$;
CREATE TRIGGER create_hh_unicef_id BEFORE INSERT ON household_household FOR EACH ROW EXECUTE PROCEDURE create_hh_unicef_id();
""",
),
migrations.RunSQL(
"""
CREATE OR REPLACE FUNCTION create_ii_unicef_id() RETURNS trigger
LANGUAGE plpgsql
AS $$
begin
NEW.unicef_id := format('IND-%s-%s',TO_CHAR(NEW.first_registration_date, 'yy'), LPAD(format('%s',NEW.unicef_id_index),8,'0'));
return NEW;
end
$$;
CREATE TRIGGER create_ii_unicef_id BEFORE INSERT ON household_individual FOR EACH ROW EXECUTE PROCEDURE create_ii_unicef_id();
""",
),
migrations.RunSQL(
"UPDATE household_household SET unicef_id = format('HH-%s-%s',country ,LPAD(format('%s',unicef_id_index),8,'0'))"
),
migrations.RunSQL(
"UPDATE household_individual SET unicef_id = format('IND-%s-%s',TO_CHAR(first_registration_date, 'yy'), LPAD(format('%s',unicef_id_index),8,'0'))"
),
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment