database Package¶
database
Package¶
Roles in this namespace are meant to enable database management for database servers as MySQL, MongoDB, Redis and such.
postgresql
Module¶
Roles in this namespace are meant to provide PostgreSQL database management utilities.
-
class
provy.more.base.database.postgresql.
BasePostgreSQLRole
(prov, context)[source]¶ Bases:
provy.core.roles.Role
-
create_database
(database, owner=None)[source]¶ Creates a database.
Parameters: - database (
str
) – Name of the database to be created. - owner (
str
) – The database owner. If not provided, will be the Postgres default.
Returns: The result output of the execution.
Return type: str
Example:
class MySampleRole(Role): def provision(self): with self.using(PostgreSQLRole) as role: role.create_database("foo", owner="john")
- database (
-
create_user
(username, ask_password=True, is_superuser=False, can_create_databases=False, can_create_roles=False)[source]¶ Creates a user for the database.
Parameters: - username (
str
) – Name of the user to be created. - ask_password (
bool
) – IfFalse
, doesn’t ask for the user password now. Defaults toTrue
, which makes the role prompt for the password. - is_superuser (
bool
) – IfTrue
, creates as a superuser and ignores can_create_databases and can_create_roles arguments (as they would be implicit). Defaults toFalse
. - can_create_databases (
bool
) – IfTrue
, gives database creation privilege to the user. Defaults toFalse
. - can_create_roles (
bool
) – IfTrue
, gives this user privilege to create other users. Defaults toFalse
.
Returns: The result output of the execution.
Return type: str
Example:
class MySampleRole(Role): def provision(self): with self.using(PostgreSQLRole) as role: role.create_user("john", ask_password=False)
- username (
-
database_exists
(database)[source]¶ Checks if a database exists.
Parameters: database ( str
) – Name of the database to be checked.Returns: Whether the database exists. Return type: bool
Example:
class MySampleRole(Role): def provision(self): with self.using(PostgreSQLRole) as role: role.database_exists("foo") # True or False
-
drop_database
(database)[source]¶ Drops a database.
Parameters: database ( str
) – Name of the database to be dropped.Returns: The result output of the execution. Return type: str
Example:
class MySampleRole(Role): def provision(self): with self.using(PostgreSQLRole) as role: role.drop_database("foo")
-
drop_user
(username)[source]¶ Drops a user from the database.
Parameters: username ( str
) – Name of the user to be dropped.Returns: The result output of the execution. Return type: str
Example:
class MySampleRole(Role): def provision(self): with self.using(PostgreSQLRole) as role: role.drop_user("john")
-
ensure_database
(database, owner=None)[source]¶ Ensures that a database exists. If it doesn’t, create it.
Parameters: - database (
str
) – Name of the database to be checked/created. - owner (
str
) – The database owner. If not provided, will be the Postgres default.
Returns: The result output of the execution.
Return type: str
Example:
class MySampleRole(Role): def provision(self): with self.using(PostgreSQLRole) as role: role.ensure_database("foo", owner="john")
- database (
-
ensure_user
(username, ask_password=True, is_superuser=False, can_create_databases=False, can_create_roles=False)[source]¶ Ensures that a user exists in the database. If it doesn’t, create it.
Parameters: - username (
str
) – Name of the user to be checked/created. - ask_password (
bool
) – IfFalse
, doesn’t ask for the user password now. Defaults toTrue
, which makes the role prompt for the password. - is_superuser (
bool
) – IfTrue
, creates as a superuser and ignores can_create_databases and can_create_roles arguments (as they would be implicit). Defaults toFalse
. - can_create_databases (
bool
) – IfTrue
, gives database creation privilege to the user. Defaults toFalse
. - can_create_roles (
bool
) – IfTrue
, gives this user privilege to create other users. Defaults toFalse
.
Returns: The result output of the execution.
Return type: str
Example:
class MySampleRole(Role): def provision(self): with self.using(PostgreSQLRole) as role: role.ensure_user("john", ask_password=False)
- username (
-
provision
()[source]¶ This method should be implemented by the concrete PostgreSQLRole classes, according to each provisioning steps for each distribution.
Raise: NotImplementedError
-
user_exists
(username)[source]¶ Checks if a user exists in the database.
Parameters: username ( str
) – Name of the user to be checked.Returns: Whether the user exists or not. Return type: bool
Example:
class MySampleRole(Role): def provision(self): with self.using(PostgreSQLRole) as role: role.user_exists("john") # True or False
-