|
| 1 | +# Generated by Django 2.2.10 on 2020-02-17 09:44 |
| 2 | + |
| 3 | +from django.db import migrations |
| 4 | + |
| 5 | + |
| 6 | +def non_null_count(*values): |
| 7 | + none_values = [i for i in values if i == None] |
| 8 | + |
| 9 | + return len(none_values) |
| 10 | + |
| 11 | + |
| 12 | +DB_FUNCTIONS = { |
| 13 | + "postgresql": { |
| 14 | + "forward": lambda conn, cursor: cursor.execute( |
| 15 | + """ |
| 16 | + CREATE OR REPLACE FUNCTION public.non_null_count(VARIADIC arg_array ANYARRAY) |
| 17 | + RETURNS BIGINT AS |
| 18 | + $$ |
| 19 | + SELECT COUNT(x) FROM UNNEST($1) AS x |
| 20 | + $$ LANGUAGE SQL IMMUTABLE; |
| 21 | + """ |
| 22 | + ), |
| 23 | + "reverse": lambda conn, cursor: cursor.execute( |
| 24 | + """ |
| 25 | + DROP FUNCTION IF EXISTS public.non_null_count(VARIADIC arg_array ANYARRAY); |
| 26 | + """ |
| 27 | + ), |
| 28 | + }, |
| 29 | + "sqlite": { |
| 30 | + "forward": lambda conn, cursor: conn.create_function( |
| 31 | + "non_null_count", -1, non_null_count |
| 32 | + ), |
| 33 | + "reverse": lambda conn, cursor: conn.create_function( |
| 34 | + "non_null_count", -1, None |
| 35 | + ), |
| 36 | + }, |
| 37 | + "mysql": { |
| 38 | + "forward": lambda conn, cursor: cursor.execute( |
| 39 | + """ |
| 40 | + CREATE FUNCTION non_null_count (params JSON) |
| 41 | + RETURNS INT |
| 42 | + DETERMINISTIC |
| 43 | + READS SQL DATA |
| 44 | + BEGIN |
| 45 | + DECLARE n INT DEFAULT JSON_LENGTH(params); |
| 46 | + DECLARE i INT DEFAULT 0; |
| 47 | + DECLARE current BOOLEAN DEFAULT false; |
| 48 | + DECLARE val INT DEFAULT 0; |
| 49 | +
|
| 50 | + WHILE i < n DO |
| 51 | + SET current = if(JSON_TYPE(JSON_EXTRACT(params, concat('$[', i , ']'))) != 'NULL', true, false); |
| 52 | + IF current THEN |
| 53 | + SET val = val + 1; |
| 54 | + END IF; |
| 55 | + SET i = i + 1; |
| 56 | + END WHILE; |
| 57 | + RETURN val; |
| 58 | + END; |
| 59 | + CREATE TRIGGER demo_book_validate before INSERT ON demo_book |
| 60 | + FOR each row |
| 61 | + BEGIN |
| 62 | + if non_null_count(JSON_ARRAY(new.amount_off, new.percentage)) = 0 |
| 63 | + THEN |
| 64 | + signal SQLSTATE '45000' SET message_text = 'Both amount_off and percentage cannot |
| 65 | + be null'; |
| 66 | + END if; |
| 67 | + END; |
| 68 | +
|
| 69 | +
|
| 70 | + CREATE TRIGGER demo_book_validate_2 before UPDATE ON demo_book |
| 71 | + FOR each row |
| 72 | + BEGIN |
| 73 | + if non_null_count(JSON_ARRAY(new.amount_off, new.percentage)) = 0 |
| 74 | + THEN |
| 75 | + signal SQLSTATE '45000' SET message_text = 'Both amount_off and percentage cannot |
| 76 | + be null'; |
| 77 | + END if; |
| 78 | + END; |
| 79 | + """ |
| 80 | + ), |
| 81 | + "reverse": lambda conn, cursor: cursor.execute( |
| 82 | + """ |
| 83 | + DROP FUNCTION non_null_count; |
| 84 | + DROP TRIGGER demo_book_validate; |
| 85 | + DROP TRIGGER demo_book_validate_2; |
| 86 | + """ |
| 87 | + ), |
| 88 | + }, |
| 89 | +} |
| 90 | + |
| 91 | + |
| 92 | +def forwards_func(apps, schema_editor): |
| 93 | + conn = schema_editor.connection |
| 94 | + vendor = conn.vendor |
| 95 | + |
| 96 | + with conn.cursor() as cursor: |
| 97 | + func = DB_FUNCTIONS[vendor]["forward"] |
| 98 | + |
| 99 | + func(conn.connection, cursor) |
| 100 | + |
| 101 | + |
| 102 | +def reverse_func(apps, schema_editor): |
| 103 | + conn = schema_editor.connection |
| 104 | + db_alias = conn.db_alias |
| 105 | + |
| 106 | + with conn.cursor() as cursor: |
| 107 | + func = DB_FUNCTIONS[db_alias]["reverse"] |
| 108 | + |
| 109 | + func(conn, cursor) |
| 110 | + |
| 111 | + |
| 112 | +class Migration(migrations.Migration): |
| 113 | + dependencies = [ |
| 114 | + ("demo", "0001_initial"), |
| 115 | + ] |
| 116 | + |
| 117 | + operations = [migrations.RunPython(forwards_func, reverse_func)] |
0 commit comments