Add set-password build target
authorKarl O. Pinc <kop@karlpinc.com>
Fri, 8 Sep 2023 21:46:50 +0000 (16:46 -0500)
committerKarl O. Pinc <kop@karlpinc.com>
Fri, 8 Sep 2023 21:46:50 +0000 (16:46 -0500)
make_files/make_db.mk

index f577833fbb5711348edfddb197f26ae7bfd2a444..565780c27a9740f205c30ff56965b03f6a556d38 100644 (file)
@@ -445,10 +445,12 @@ $(DB_TARGETS): %.sql: %.m4 $(DB_DEPENDS)
 ##     to be the ADMINUSER role, the role used to make database objects.
 ##   TARGET_USER
 ##     The user to be created or otherwise modified.
+##   NEW_PASSWORD
+##     The password to assign.
 ##   NEW_DB
 ##     The database to be created.
 ##   NEW_DESCRIPTION
-##     The description of the new object (database)
+##     The description of the new object (database).
 ##
 ## Usage:
 ##  The expectation is that first an administrative user will be created, then
@@ -476,6 +478,7 @@ SET_DATESTYLE := ALTER DATABASE $(NEW_DB) \
                      SET DATESTYLE =  'ISO,MDY';
 CREATE_ADMINUSER := CREATE ROLE $(TARGET_USER) LOGIN SUPERUSER CREATEROLE \
                     PASSWORD '$(NEW_PASSWORD)';
+SET_PASSWORD := ALTER ROLE $(TARGET_USER) PASSWORD '$(NEW_PASSWORD)'
 
 ## The available targets for make (make TARGET) are:
 ##
@@ -523,6 +526,18 @@ init-database:
 ## Lesser used targets:
 ##
 
+##   set-password       Set the password of TARGET_USER to NEW_PASSWORD
+.PHONY: set-password
+set-password:
+       [ -n "$(NEW_PASSWORD)" ] \
+           || { printf 'The NEW_PASSWORD variable must be set\n' >&2 ; \
+                exit 1 ; }
+       ( $(PSQL_SETUP) \
+         printf "$(SET_PASSWORD)\n" ; \
+         ) \
+         | psql $(PSQL_ARGS) $(PSQL_SINGLE_TRANS) --set=ON_ERROR_STOP=y
+
+
 ##   set-search_path    Set the default search_path the database named
 ##                      by the TARGET_DB variable.
 .PHONY: set-search_path