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")
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) – If False, doesn’t ask for the user password now. Defaults to True, which makes the role prompt for the password.
  • is_superuser (bool) – If True, creates as a superuser and ignores can_create_databases and can_create_roles arguments (as they would be implicit). Defaults to False.
  • can_create_databases (bool) – If True, gives database creation privilege to the user. Defaults to False.
  • can_create_roles (bool) – If True, gives this user privilege to create other users. Defaults to False.
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)
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")
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) – If False, doesn’t ask for the user password now. Defaults to True, which makes the role prompt for the password.
  • is_superuser (bool) – If True, creates as a superuser and ignores can_create_databases and can_create_roles arguments (as they would be implicit). Defaults to False.
  • can_create_databases (bool) – If True, gives database creation privilege to the user. Defaults to False.
  • can_create_roles (bool) – If True, gives this user privilege to create other users. Defaults to False.
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)
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