From 9d36720cc657ec255f3709aa8c570ffce3b842fc Mon Sep 17 00:00:00 2001 From: LoRiot Date: Fri, 14 Oct 2022 18:05:04 +0300 Subject: [PATCH] Create migrations --- db/init_db.py | 20 ----------------- db/migrations/__init__.py | 0 db/migrations/drop_tables.py | 19 ++++++++++++++++ db/migrations/init_tables.py | 40 ++++++++++++++++++++++++++++++++++ manage_db.py | 42 ------------------------------------ 5 files changed, 59 insertions(+), 62 deletions(-) delete mode 100644 db/init_db.py create mode 100644 db/migrations/__init__.py create mode 100644 db/migrations/drop_tables.py create mode 100644 db/migrations/init_tables.py delete mode 100644 manage_db.py diff --git a/db/init_db.py b/db/init_db.py deleted file mode 100644 index 4ef6654..0000000 --- a/db/init_db.py +++ /dev/null @@ -1,20 +0,0 @@ -init_db_tables_sql = """ - - CREATE TABLE projects ( \ - id SERIAL PRIMARY KEY, \ - name varchar(50), \ - description varchar(1000), \ - creator varchar(30), \ - contributors smallint DEFAULT 1, \ - status bit(1) DEFAULT 1, \ - tags varchar(100), \ - category varchar(20), \ - creation_date date, \ - ); - - CREATE TABLE users ( \ - user_id bigint PRIMARY KEY, \ - is_admin boolean DEFAULT FALSE, \ - ); - -""" \ No newline at end of file diff --git a/db/migrations/__init__.py b/db/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/db/migrations/drop_tables.py b/db/migrations/drop_tables.py new file mode 100644 index 0000000..9d3f062 --- /dev/null +++ b/db/migrations/drop_tables.py @@ -0,0 +1,19 @@ +import asyncio +import asyncpg +import os +from asyncpg.connection import Connection + + +async def drop_tables(connection: Connection): + await connection.execute("DROP TABLE users, projects CASCADE") + + +async def main(): + DB_URL = os.getenv("DB_URL") + connection = await asyncpg.connect(DB_URL) + await drop_tables(connection) + await connection.close() + + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/db/migrations/init_tables.py b/db/migrations/init_tables.py new file mode 100644 index 0000000..80e2bf8 --- /dev/null +++ b/db/migrations/init_tables.py @@ -0,0 +1,40 @@ +import asyncio +import asyncpg +import os +from asyncpg.connection import Connection + + +async def init_tables(connection: Connection): + init_users_query = """ + CREATE TABLE users ( \ + user_id bigint PRIMARY KEY, \ + is_admin boolean DEFAULT FALSE \ + ); + """ + + await connection.execute(init_users_query) + init_projects_query = """ + CREATE TABLE projects ( + id SERIAL PRIMARY KEY, \ + name varchar(50), \ + description varchar(1000), \ + creator bigint REFERENCES users, \ + contributors smallint DEFAULT 1, \ + status bit(1) DEFAULT B'1', \ + tag varchar(100), \ + category varchar(20), \ + creation_date date DEFAULT CURRENT_DATE \ + ); + """ + await connection.execute(init_projects_query) + + +async def main(): + DB_URL = os.getenv("DB_URL") + connection = await asyncpg.connect(DB_URL) + await init_tables(connection) + await connection.close() + + +if __name__ == '__main__': + asyncio.run(main()) diff --git a/manage_db.py b/manage_db.py deleted file mode 100644 index ce04bf3..0000000 --- a/manage_db.py +++ /dev/null @@ -1,42 +0,0 @@ -from loguru import logger -import pretty_errors - -import argparse -from argparse import ArgumentParser - -def lmao(): - print("lmao") - -def init_argparser() -> ArgumentParser: - argparser = argparse.ArgumentParser( - description="List fish in aquarium.", - argument_default=argparse.SUPPRESS - ) - argparser.add_argument( - "--init", "-i", - help="Инициализирует базу данных", - type=str, - ) - argparser.add_argument( - "--make-adm", "-m", - help="Делает пользователя админом", - type=str, - ) - argparser.add_argument( - "--del-adm", "-d", - help="Забирает права админа у пользователя", - type=str, - ) - argparser.add_argument( - "--delete-db", - help="Снести базу данных", - action="lmao" - ) - - return argparser - - -if __name__ == '__main__': - argparser = init_argparser() - args = vars(argparser.parse_args()) -