X-Git-Url: http://git.equinoxoli.org/?p=migration-tools.git;a=blobdiff_plain;f=sql%2Fbase%2Fbase.sql;h=ca9a568574bfa57214da0d468c8fb4ef34ea856a;hp=b828102f02e7f7f08ae92616456cd96e212809f0;hb=2db3025cacf2d60d016ae0759ff2ac39f36a3d92;hpb=56cd79ed8adadd1cfe909cce559726cb7e329f3f diff --git a/sql/base/base.sql b/sql/base/base.sql index b828102..ca9a568 100644 --- a/sql/base/base.sql +++ b/sql/base/base.sql @@ -688,7 +688,7 @@ CREATE OR REPLACE FUNCTION migration_tools.attempt_money (TEXT,TEXT) RETURNS NUM output NUMERIC(8,2); BEGIN FOR output IN - EXECUTE 'SELECT ' || quote_literal(REPLACE(attempt_value,'$','')) || '::NUMERIC(8,2) AS a;' + EXECUTE 'SELECT ' || quote_literal(REPLACE(REPLACE(attempt_value,'$',''),',','')) || '::NUMERIC(8,2) AS a;' LOOP RETURN output; END LOOP; @@ -709,7 +709,7 @@ CREATE OR REPLACE FUNCTION migration_tools.attempt_money6 (TEXT,TEXT) RETURNS NU output NUMERIC(6,2); BEGIN FOR output IN - EXECUTE 'SELECT ' || quote_literal(REPLACE(attempt_value,'$','')) || '::NUMERIC(6,2) AS a;' + EXECUTE 'SELECT ' || quote_literal(REPLACE(REPLACE(attempt_value,'$',''),',','')) || '::NUMERIC(6,2) AS a;' LOOP RETURN output; END LOOP; @@ -1934,3 +1934,89 @@ BEGIN END $func$ LANGUAGE PLPGSQL; + +-- example: SELECT * FROM migration_tools.duplicate_template(5,'{3,4}'); +CREATE OR REPLACE FUNCTION migration_tools.duplicate_template (INTEGER, INTEGER[]) RETURNS VOID AS $$ + DECLARE + target_event_def ALIAS FOR $1; + orgs ALIAS FOR $2; + BEGIN + DROP TABLE IF EXISTS new_atevdefs; + CREATE TEMP TABLE new_atevdefs (atevdef INTEGER); + FOR i IN array_lower(orgs,1) .. array_upper(orgs,1) LOOP + INSERT INTO action_trigger.event_definition ( + active + ,owner + ,name + ,hook + ,validator + ,reactor + ,cleanup_success + ,cleanup_failure + ,delay + ,max_delay + ,usr_field + ,opt_in_setting + ,delay_field + ,group_field + ,template + ,granularity + ,repeat_delay + ) SELECT + 'f' + ,orgs[i] + ,name || ' (clone of '||target_event_def||')' + ,hook + ,validator + ,reactor + ,cleanup_success + ,cleanup_failure + ,delay + ,max_delay + ,usr_field + ,opt_in_setting + ,delay_field + ,group_field + ,template + ,granularity + ,repeat_delay + FROM + action_trigger.event_definition + WHERE + id = target_event_def + ; + RAISE INFO 'created atevdef with id = %', currval('action_trigger.event_definition_id_seq'); + INSERT INTO new_atevdefs SELECT currval('action_trigger.event_definition_id_seq'); + INSERT INTO action_trigger.environment ( + event_def + ,path + ,collector + ,label + ) SELECT + currval('action_trigger.event_definition_id_seq') + ,path + ,collector + ,label + FROM + action_trigger.environment + WHERE + event_def = target_event_def + ; + INSERT INTO action_trigger.event_params ( + event_def + ,param + ,value + ) SELECT + currval('action_trigger.event_definition_id_seq') + ,param + ,value + FROM + action_trigger.event_params + WHERE + event_def = target_event_def + ; + END LOOP; + RAISE INFO '-- UPDATE action_trigger.event_definition SET active = CASE WHEN id = % THEN FALSE ELSE TRUE END WHERE id in (%,%);', target_event_def, target_event_def, (SELECT array_to_string(array_agg(atevdef),',') from new_atevdefs); + END; +$$ LANGUAGE PLPGSQL STRICT VOLATILE; +