Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: modules/discourse/manifests/postactiontype.pp

Issue 9431166: Make sure to populate post_action_types table (Closed)
Patch Set: Created Feb. 27, 2013, 5:01 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « modules/discourse/manifests/init.pp ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: modules/discourse/manifests/postactiontype.pp
===================================================================
new file mode 100644
--- /dev/null
+++ b/modules/discourse/manifests/postactiontype.pp
@@ -0,0 +1,51 @@
+define discourse::postactiontype(
+ $id,
+ $key = $title,
+ $is_flag = false,
+ $icon = undef,
+ $position = 0,
+ $ensure = 'present'
+) {
+ # Attempt some escaping
+ $escaped_id = regsubst($id, '\D', '', 'G')
+ $escaped_key = regsubst($key, '[\'\\]', '\\\1', 'G')
+ if $is_flag {
+ $escaped_flag = 'true'
+ }
+ else {
+ $escaped_flag = 'false'
+ }
+ if $icon {
+ $dummy = regsubst($icon, '[\'\\]', '\\\1', 'G')
+ $escaped_icon = "'${dummy}'"
+ }
+ else {
+ $escaped_icon = "null"
+ }
+ $escaped_position = regsubst($position, '\D', '', 'G')
+
+ case $ensure {
+ default: {
+ err("unknown ensure value ${ensure}")
+ }
+ present: {
+ # This is apparently how you do a conditional INSERT in PostgreSQL - sorry
+ $update_sql = "UPDATE post_action_types SET name_key = '$escaped_key', is_flag = $escaped_flag, icon = $escaped_icon, position = $escaped_position WHERE id = $escaped_id RETURNING 1"
+ $columns = "id, name_key, is_flag, icon, position, created_at, updated_at"
+ $values = "SELECT $escaped_id, '$escaped_key', $escaped_flag, $escaped_icon, $escaped_position, CURRENT_TIMESTAMP, CURRENT_TIMESTAMP"
+
+ postgresql_psql {"WITH upd AS ($update_sql) INSERT INTO post_action_types ($columns) $values WHERE NOT EXISTS (SELECT * FROM upd)":
+ db => 'discourse',
+ psql_user => 'discourse',
+ unless => 'SELECT false'
+ }
+ }
+ absent: {
+ postgresql_psql {"DELETE FROM post_action_types WHERE id = $escaped_id":
+ db => 'discourse',
+ psql_user => 'discourse',
+ unless => 'SELECT false'
+ }
+ }
+ }
+}
« no previous file with comments | « modules/discourse/manifests/init.pp ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld