| LEFT | RIGHT |
| (no file at all) | |
| 1 define discourse::postactiontype( | |
| 2 $id, | |
| 3 $key = $title, | |
| 4 $is_flag = false, | |
| 5 $icon = undef, | |
| 6 $position = 0, | |
| 7 $ensure = 'present' | |
| 8 ) { | |
| 9 # Attempt some escaping | |
| 10 $escaped_id = regsubst($id, '\D', '', 'G') | |
| 11 $escaped_key = regsubst($key, '[\'\\]', '\\\1', 'G') | |
| 12 if $is_flag { | |
| 13 $escaped_flag = 'true' | |
| 14 } | |
| 15 else { | |
| 16 $escaped_flag = 'false' | |
| 17 } | |
| 18 if $icon { | |
| 19 $dummy = regsubst($icon, '[\'\\]', '\\\1', 'G') | |
| 20 $escaped_icon = "'${dummy}'" | |
| 21 } | |
| 22 else { | |
| 23 $escaped_icon = "null" | |
| 24 } | |
| 25 $escaped_position = regsubst($position, '\D', '', 'G') | |
| 26 | |
| 27 case $ensure { | |
| 28 default: { | |
| 29 err("unknown ensure value ${ensure}") | |
| 30 } | |
| 31 present: { | |
| 32 # This is apparently how you do a conditional INSERT in PostgreSQL - sorry | |
| 33 $update_sql = "UPDATE post_action_types SET name_key = '$escaped_key', is_
flag = $escaped_flag, icon = $escaped_icon, position = $escaped_position WHERE i
d = $escaped_id RETURNING 1" | |
| 34 $columns = "id, name_key, is_flag, icon, position, created_at, updated_at" | |
| 35 $values = "SELECT $escaped_id, '$escaped_key', $escaped_flag, $escaped_ico
n, $escaped_position, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP" | |
| 36 | |
| 37 postgresql_psql {"WITH upd AS ($update_sql) INSERT INTO post_action_types
($columns) $values WHERE NOT EXISTS (SELECT * FROM upd)": | |
| 38 db => 'discourse', | |
| 39 psql_user => 'discourse' | |
| 40 } | |
| 41 } | |
| 42 absent: { | |
| 43 postgresql_psql {"DELETE FROM post_action_types WHERE id = $escaped_id": | |
| 44 db => 'discourse', | |
| 45 psql_user => 'discourse' | |
| 46 } | |
| 47 } | |
| 48 } | |
| 49 } | |
| LEFT | RIGHT |