@@ -144,18 +144,13 @@ def _get_mysql_roles(self, name_pattern: str) -> typing.Set[str]:
144
144
logger .debug (f"MySQL roles found for { name_pattern = } : { len (rows )} " )
145
145
return {row [0 ] for row in rows }
146
146
147
- def create_application_database (self , * , database : str ) -> str :
147
+ def _create_application_database (self , * , database : str , rolename : str ) -> str :
148
148
"""Create database for related database_provides application."""
149
- role_name = f"charmed_dba_{ database } "
150
- if len (role_name ) >= _ROLE_MAX_LENGTH :
151
- logger .exception ("Failed to create application database" )
152
- raise ValueError ("Role name longer than 32 characters" )
153
-
154
149
statements = [
155
150
f"CREATE DATABASE IF NOT EXISTS `{ database } `" ,
156
- f"CREATE ROLE IF NOT EXISTS `{ role_name } `" ,
157
- f"GRANT SELECT, INSERT, DELETE, UPDATE, EXECUTE ON `{ database } `.* TO { role_name } " ,
158
- f"GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE VIEW, DROP, INDEX, LOCK TABLES, REFERENCES, TRIGGER ON `{ database } `.* TO { role_name } " ,
151
+ f"CREATE ROLE IF NOT EXISTS `{ rolename } `" ,
152
+ f"GRANT SELECT, INSERT, DELETE, UPDATE, EXECUTE ON `{ database } `.* TO { rolename } " ,
153
+ f"GRANT ALTER, ALTER ROUTINE, CREATE, CREATE ROUTINE, CREATE VIEW, DROP, INDEX, LOCK TABLES, REFERENCES, TRIGGER ON `{ database } `.* TO { rolename } " ,
159
154
]
160
155
161
156
mysql_roles = self ._get_mysql_roles ("charmed_%" )
@@ -173,7 +168,7 @@ def create_application_database(self, *, database: str) -> str:
173
168
logger .debug (f"Created { database = } " )
174
169
return database
175
170
176
- def create_application_user (self , * , database : str , username : str ) -> str :
171
+ def _create_application_user (self , * , database : str , username : str ) -> str :
177
172
"""Create database user for related database_provides application."""
178
173
attributes = self ._get_attributes ()
179
174
password = utils .generate_password ()
@@ -185,6 +180,16 @@ def create_application_user(self, *, database: str, username: str) -> str:
185
180
logger .debug (f"Created { username = } with { attributes = } " )
186
181
return password
187
182
183
+ def create_application_database (self , * , database : str , username : str ) -> str :
184
+ """Create both the database and the relation user, returning its password."""
185
+ rolename = f"charmed_dba_{ database } "
186
+ if len (rolename ) >= _ROLE_MAX_LENGTH :
187
+ raise ValueError ("Database DBA role longer than 32 characters" )
188
+
189
+ ________ = self ._create_application_database (database = database , rolename = rolename )
190
+ password = self ._create_application_user (database = database , username = username )
191
+ return password
192
+
188
193
def add_attributes_to_mysql_router_user (
189
194
self , * , username : str , router_id : str , unit_name : str
190
195
) -> None :
0 commit comments