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