OLD | NEW |
(Empty) | |
| 1 define discourse::sitesetting( |
| 2 $setting = $title, |
| 3 $value = undef, |
| 4 $type = 1, |
| 5 $ensure = 'present' |
| 6 ) { |
| 7 # Attempt some escaping |
| 8 $escaped_value = regsubst($value, '[\'\\]', '\\\1', 'G') |
| 9 $escaped_setting = regsubst($setting, '[\'\\]', '\\\1', 'G') |
| 10 $escaped_type = regsubst($type, '\D', '', 'G') |
| 11 |
| 12 case $ensure { |
| 13 default: { |
| 14 err("unknown ensure value ${ensure}") |
| 15 } |
| 16 present: { |
| 17 # This is apparently how you do a conditional INSERT in PostgreSQL - sorry |
| 18 $update_sql = "UPDATE site_settings SET value = '$escaped_value', data_typ
e = $escaped_type WHERE name = '$escaped_setting' RETURNING 1" |
| 19 $columns = "name, data_type, value, created_at, updated_at" |
| 20 $values = "SELECT '$escaped_setting', $escaped_type, '$escaped_value', CUR
RENT_TIMESTAMP, CURRENT_TIMESTAMP" |
| 21 |
| 22 postgresql_psql {"WITH upd AS ($update_sql) INSERT INTO site_settings ($co
lumns) $values WHERE NOT EXISTS (SELECT * FROM upd)": |
| 23 db => 'discourse', |
| 24 psql_user => 'discourse', |
| 25 unless => 'SELECT false' |
| 26 } |
| 27 } |
| 28 absent: { |
| 29 postgresql_psql {"DELETE FROM site_settings WHERE name = '$escaped_setting
'": |
| 30 db => 'discourse', |
| 31 psql_user => 'discourse', |
| 32 unless => 'SELECT false' |
| 33 } |
| 34 } |
| 35 } |
| 36 } |
OLD | NEW |