This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
generated from communitiesuk/funding-service-design-TEMPLATE
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtasks.py
48 lines (40 loc) · 1.62 KB
/
tasks.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
import os
from urllib.parse import urlparse
from colored import attr
from colored import fg
from colored import stylize
from invoke import task
ECHO_STYLE = fg("light_gray") + attr("bold")
@task
def recreate_local_db(c):
"""Create a clean database for testing"""
# As we assume "db_name" is not yet created. First we need to connect to psql with an existing database
# Replace database in database_url with "postgres" db
database_url = os.environ.get("DATABASE_URL")
if not database_url:
raise Exception("Please set the environmental variable 'DATABASE_URL'!")
parsed_db_url = urlparse(database_url)
database_host = parsed_db_url.hostname
db_name = parsed_db_url.path.lstrip("/")
parsed_db_url = parsed_db_url._replace(path="/postgres")
database_url = parsed_db_url.geturl()
c.run(f'psql {database_url} -c "DROP DATABASE IF EXISTS {db_name} WITH (FORCE);"')
print(
stylize(
f"{db_name} db dropped from {database_host}...",
ECHO_STYLE,
)
)
c.run(f'psql {database_url} -c "CREATE DATABASE {db_name};"')
c.run(f'psql {database_url} -c "create extension if not exists ltree;"')
print(stylize(f"{db_name} db created on {database_host}...", ECHO_STYLE))
@task
def truncate_data(c):
database_url = os.environ.get("DATABASE_URL")
if not database_url:
raise Exception("Please set the environmental variable 'DATABASE_URL'!")
c.run(
f'psql {database_url} -c "TRUNCATE TABLE section, round, fund,'
" assessment_field, form_name, section_field CASCADE;ALTER SEQUENCE"
' section_id_seq RESTART WITH 1;";'
)