Create migrations

This commit is contained in:
LoRiot 2022-10-14 18:05:04 +03:00
parent 18ebb8f9f5
commit 9d36720cc6
5 changed files with 59 additions and 62 deletions

View file

@ -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, \
);
"""

View file

View file

@ -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())

View file

@ -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())

View file

@ -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())