-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
163 lines (124 loc) · 4.94 KB
/
main.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import asyncio
import sqlalchemy as sa
import pandas
from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import declarative_base, sessionmaker
from sqlalchemy.sql import text
Base = declarative_base()
class Person(Base):
__tablename__ = "person"
id = sa.Column(sa.Integer, primary_key=True)
name = sa.Column(sa.String(30))
age = sa.Column(sa.Integer)
def __repr__(self):
return f"Person(id={self.id}, name={self.name}, age={self.age})"
def prettyprint(result):
dataframe = pandas.DataFrame(result.all(), columns=result.keys())
markdown = dataframe.to_markdown()
print("\n" + markdown + "\n")
async def check_postgres_version(session):
result = await session.execute(text("select version()"))
prettyprint(result)
async def check_postgres_schema(session):
result = await session.execute(text("select pg_catalog.current_schema()"))
prettyprint(result)
async def check_db_info(session):
result = await session.execute(
text(
"select oid, datname, datconnlimit, dattablespace, datcollate, datctype, datcollversion"
" from pg_catalog.pg_database"
" where datname = 'postgres'"
)
)
prettyprint(result)
async def check_db_size(session):
result = await session.execute(text("select pg_catalog.pg_size_pretty(pg_database_size(current_database()))"))
prettyprint(result)
async def check_db_config(session):
result = await session.execute(text("select source, setting from pg_catalog.pg_settings where source != 'default'"))
prettyprint(result)
async def check_tables(session):
result = await session.execute(text("select * from pg_catalog.pg_tables where schemaname = 'public'"))
prettyprint(result)
async def check_columns(session):
result = await session.execute(
text(
"select column_name, ordinal_position, column_default, is_nullable, data_type, character_maximum_length,"
" numeric_precision, is_updatable"
" from information_schema.columns"
" where table_name = 'person'"
)
)
prettyprint(result)
async def postgres_env_check(engine):
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
async with async_session() as session:
async with session.begin():
await check_postgres_version(session)
await check_postgres_schema(session)
await check_db_info(session)
await check_db_size(session)
await check_db_config(session)
await check_tables(session)
await check_columns(session)
async def env_check(engine):
if engine.name == "postgresql":
await postgres_env_check(engine)
else:
print("Performing other database environment check")
async def init(engine):
async with engine.begin() as conn:
await conn.run_sync(Base.metadata.create_all, checkfirst=True)
async def create(engine):
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
async with async_session() as session:
async with session.begin():
statement = sa.insert(Person).values(
[
{"name": "jack", "age": 22},
{"name": "eric", "age": 24},
{"name": "lucy", "age": 31},
]
)
await session.execute(statement)
await session.commit()
async def read_and_print(engine):
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
async with async_session() as session:
async with session.begin():
result = await session.execute(sa.select(Person))
print(" Result:")
for person in result.scalars():
print(" " + str(person))
async def update(engine):
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
async with async_session() as session:
await session.execute(sa.update(Person).values({"age": Person.age + 1}))
await session.commit()
async def delete(engine):
async_session = sessionmaker(engine, expire_on_commit=False, class_=AsyncSession)
async with async_session() as session:
async with session.begin():
await session.execute(sa.delete(Person))
await session.commit()
async def main():
engines = [
# create_async_engine("postgresql+asyncpg://postgres:postgres@localhost:5432/postgres", echo=True),
create_async_engine("mysql+asyncmy://root:P%40ssw0rd@localhost:3306/myschema", echo=True),
]
action_funcs = [
env_check,
init,
read_and_print,
create,
read_and_print,
update,
read_and_print,
delete,
read_and_print,
]
for engine in engines:
for func in action_funcs:
await func(engine=engine)
if __name__ == "__main__":
asyncio.run(main())