@@ -178,12 +178,16 @@ def render_postgres_script(namespace,template,data):
178178 file = ntp .spool_template (template , out , data )
179179 return os .path .abspath (file )
180180
181- def exec_psql_command (pod_name ,path_to_psql_script ,path_to_pgpass ):
181+ def exec_psql_command (pod_name ,path_to_psql_script ,path_to_pgpass , additional_psql_args = '' ):
182182 logging .info (f"passing script { path_to_psql_script } to pod { pod_name } " )
183183 res = kube .kubectl ("cp" ,path_to_psql_script ,f"{ pod_name } :{ path_to_psql_script } " )
184184 res = kube .kubectl ("cp" ,path_to_pgpass ,f"{ pod_name } :/tmp/.pgpass" )
185185 res = kube .kubectl ("exec" ,"-it" ,pod_name ,"--" ,"/bin/bash" ,"-c" ,f"chmod 600 /tmp/.pgpass" )
186- res = kube .kubectl ("exec" ,"-it" ,pod_name ,"--" ,"/bin/bash" ,"-c" ,f"PGPASSFILE='/tmp/.pgpass' psql --username postgres --dbname postgres -f { path_to_psql_script } " )
186+
187+ cmd = f"PGPASSFILE='/tmp/.pgpass' psql --username postgres --dbname postgres { additional_psql_args } -f { path_to_psql_script } "
188+ logging .info (f"executing command: { cmd } " )
189+ res = kube .kubectl ("exec" ,"-it" ,pod_name ,"--" ,"/bin/bash" ,"-c" ,cmd )
190+
187191 os .remove (path_to_psql_script )
188192 os .remove (path_to_pgpass )
189193 return res
@@ -209,6 +213,10 @@ def create_db_user(ucfg: UserConfig, user_metadata: UserMetadata):
209213 if res :
210214 _add_pdb_user_metadata (ucfg , user_metadata )
211215
216+ path_to_pgpass = render_postgres_script (ucfg .get ('namespace' ),"dbname_pgpass_tpl.properties" ,data )
217+ path_to_schema_script = render_postgres_script (ucfg .get ('namespace' ),"postgres_manage_user_schema_tpl.sql" ,data )
218+ res = exec_psql_command_in_db (database ,pod_name ,path_to_schema_script ,path_to_pgpass )
219+
212220 data ["extensions" ]= ["vector" ]
213221 path_to_pgpass = render_postgres_script (ucfg .get ('namespace' ),"dbname_pgpass_tpl.properties" ,data )
214222 path_to_extensions_script = render_postgres_script (ucfg .get ('namespace' ),"postgres_manage_user_extension_tpl.sql" ,data )
@@ -232,12 +240,17 @@ def delete_db_user(namespace, database):
232240 data ["database" ]= database
233241 data ["mode" ]= "delete"
234242
235- path_to_pgpass = render_postgres_script (namespace ,"pgpass_tpl.properties" ,data )
236- path_to_mdb_script = render_postgres_script (namespace ,"postgres_manage_user_tpl.sql" ,data )
243+
237244 pod_name = util .get_pod_name_by_selector ("app=nuvolaris-postgres" ,"{.items[?(@.metadata.labels.replicationRole == 'primary')].metadata.name}" )
238245
239246 if (pod_name ):
240- res = exec_psql_command (pod_name ,path_to_mdb_script ,path_to_pgpass )
247+ path_to_pgpass = render_postgres_script (namespace ,"pgpass_tpl.properties" ,data )
248+ path_to_ter_script = render_postgres_script (namespace ,"postgres_terminate_tpl.sql" ,data )
249+ res = exec_psql_command (pod_name ,path_to_ter_script ,path_to_pgpass ,' -q -t ' )
250+
251+ path_to_pgpass = render_postgres_script (namespace ,"pgpass_tpl.properties" ,data )
252+ path_to_mdb_script = render_postgres_script (namespace ,"postgres_manage_user_tpl.sql" ,data )
253+ res += exec_psql_command (pod_name ,path_to_mdb_script ,path_to_pgpass )
241254 return res
242255
243256 return None
0 commit comments