Skip to content

Instantly share code, notes, and snippets.

@hugoduncan
Created November 2, 2010 19:36
Show Gist options
  • Save hugoduncan/660158 to your computer and use it in GitHub Desktop.
Save hugoduncan/660158 to your computer and use it in GitHub Desktop.
postgresql-script
(defn postgresql-script
"Execute a postgresql script.
Options for how this script should be run:
:as-user username - Run this script having sudoed to this (system) user. Default: postgres"
[request sql-script & {:keys [as-user ignore-result]
:as options
:or {as-user "postgres"}}]
(-> request
(exec-script/exec-checked-script
"PostgreSQL temp command file"
(var psql_commands (file/make-temp-file "postgresql")))
(remote-file/remote-file
(stevedore/script @psql_commands)
:no-versioning true
:literal true
:content sql-script)
;; Don't check result, as some postgres commands are meant to fail.
;; (Ex: Create db on a db that already exists, it does nothing, so fail is fine).
(exec-script/exec-script
("{\n" sudo "-u" ~as-user psql "-f" @psql_commands > "/dev/null" ~(when ignore-result "|| 0") "\n}"))
(remote-file/remote-file
(stevedore/script @psql_commands)
:action :delete)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment