Debian Codegraph @

examples/x.py

raw symbols SCIP
1 #!/usr/bin/env python3
2
3 import sys
4 import os
5 from os import path
6
7 # base dir of sqlx workspace
8 dir_workspace = path.dirname(path.dirname(path.realpath(__file__)))
9
10 # dir of tests
11 dir_tests = path.join(dir_workspace, "tests")
12
13 # extend import path to tests/
14 sys.path.append(dir_tests)
15
16 import subprocess
17 import time
18 import argparse
19 from docker import start_database
20
21 parser = argparse.ArgumentParser()
22 parser.add_argument("-p", "--project")
23 parser.add_argument("-l", "--list-projects", action="store_true")
24
25 argv, unknown = parser.parse_known_args()
26
27
28 def run(command, env=None, cwd=None, display=None):
29 if display:
30 print(f"\x1b[93m $ {display}\x1b[0m")
31
32 else:
33 print(f"\x1b[93m $ {command}\x1b[0m")
34
35 res = subprocess.run(
36 command.split(" "),
37 env=dict(**os.environ, **env),
38 cwd=cwd,
39 )
40
41 if res.returncode != 0:
42 sys.exit(res.returncode)
43
44
45 def sqlx(command, url, cwd=None):
46 run(f"cargo --quiet run -p sqlx-cli --bin sqlx -- {command}", cwd=cwd, env={"DATABASE_URL": url},
47 display=f"sqlx {command}")
48
49
50 def project(name, database=None, driver=None):
51 if argv.list_projects:
52 print(f"{name}")
53 return
54
55 if argv.project and name != argv.project:
56 return
57
58 print(f"\x1b[2m # {name}\x1b[0m")
59
60 env = {}
61
62 cwd = path.join(dir_workspace, "examples", name)
63
64 if database is not None:
65 database_url = start_database(driver, database, cwd=cwd)
66 env["DATABASE_URL"] = database_url
67
68 # show the database url
69 print(f"\x1b[94m @ {database_url}\x1b[0m")
70
71 # database drop (if exists)
72 sqlx("db drop -y", database_url, cwd=cwd)
73
74 # database create
75 sqlx("db create", database_url, cwd=cwd)
76
77 # migrate
78 sqlx("migrate run", database_url, cwd=cwd)
79
80 # check
81 run("cargo check", cwd=cwd, env=env)
82
83
84 # todos
85 project("mysql/todos", driver="mysql_8", database="todos")
86 project("postgres/todos", driver="postgres_12", database="todos")
87 project("sqlite/todos", driver="sqlite", database="todos.db")