Create migrations
This commit is contained in:
parent
18ebb8f9f5
commit
9d36720cc6
|
@ -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, \
|
|
||||||
);
|
|
||||||
|
|
||||||
"""
|
|
0
db/migrations/__init__.py
Normal file
0
db/migrations/__init__.py
Normal file
19
db/migrations/drop_tables.py
Normal file
19
db/migrations/drop_tables.py
Normal 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())
|
40
db/migrations/init_tables.py
Normal file
40
db/migrations/init_tables.py
Normal 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())
|
42
manage_db.py
42
manage_db.py
|
@ -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())
|
|
||||||
|
|
Reference in a new issue