Databases¶
First, we'll go over basics and examine the Database class. Then, we'll start
to build our example. You can also skip ahead to the example.
The Database class¶
You can import Database like so:
Database is a representation of a Postgres database.
It is a cluster-wide entity. This means each database in your cluster must have a unique name. The __init__ args
correspond to the SQL CREATE DATABASE arguments found in the Postgres documentation.
Take a look at the class docstrings for more detail, like an explanation of the __init__ args, the various
methods defined, what classes it inherits from, and more.
Example¶
Let's start building our example. We need a database for each stage, so let's create a function
that takes a stage (like dev or prod) as input and declares a database with it:
This imports the Database class, defines a function declare_stage that accepts a string name of a stage,
and declares a Database with the stage as the name. Now let's define a main function that creates all three
stages we want to create:
from dbdeclare.entities import Database
def declare_stage(stage: str) -> None:
Database(name=stage)
def main() -> None:
stages = ["test", "dev", "prod"]
for stage in stages:
declare_stage(stage)
Here, we add a main function with no inputs, define a list of desired stages, loop over them, and run the
declare_stage function for each stage.
You can run the entire file:
from dbdeclare.entities import Database
def declare_stage(stage: str) -> None:
Database(name=stage)
def main() -> None:
stages = ["test", "dev", "prod"]
for stage in stages:
declare_stage(stage)
if __name__ == "__main__":
main()
This declares all three databases. Excellent! Note that we haven't created anything in our cluster yet, we have only declared our databases in code. We'll get to creation, but first let's add some roles.