forked from Nukesor/ultimate-poll-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
initdb.py
executable file
·53 lines (40 loc) · 1.46 KB
/
initdb.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/bin/env python
"""Create a new database with schema."""
from contextlib import contextmanager
import typer
from sqlalchemy_utils.functions import database_exists, create_database, drop_database
from pollbot.db import engine, base
from pollbot.models import * # noqa
def initialize_database(exist_ok: bool = False, drop_existing: bool = False):
db_url = engine.url
typer.echo(f"Using database at {db_url}")
if database_exists(db_url):
if drop_existing:
with wrap_echo("Dropping database"):
drop_database(db_url)
elif not exist_ok:
typer.echo(
f"Database already exists, aborting.\n"
f"Use --exist-ok if you are sure the database is uninitialized and contains no data.\n"
f"Use --drop-existing if you want to recreate it.",
err=True,
)
return
with wrap_echo("Creating database"):
create_database(db_url)
pass
with engine.connect() as con:
with wrap_echo("Installing pgcrypto extension"):
con.execute("CREATE EXTENSION IF NOT EXISTS pgcrypto;")
pass
with wrap_echo("Creating metadata"):
base.metadata.create_all()
pass
typer.echo("Database initialization complete.")
@contextmanager
def wrap_echo(msg: str):
typer.echo(f"{msg}... ", nl=False)
yield
typer.echo("done.")
if __name__ == "__main__":
typer.run(initialize_database)