This repository has been archived on 2022-10-22. You can view files and clone it, but cannot push or open issues or pull requests.
it-volunteers-for-peace/db/migrations/init_tables.py

57 lines
1.3 KiB
Python
Raw Normal View History

2022-10-14 15:05:04 +00:00
import asyncio
import asyncpg
import os
from asyncpg.connection import Connection
async def init_tables(connection: Connection):
init_users_query = """
2022-10-15 15:28:02 +00:00
CREATE TABLE users (
user_id bigint PRIMARY KEY,
2022-10-16 13:29:21 +00:00
chat_id bigint
2022-10-14 15:05:04 +00:00
);
"""
await connection.execute(init_users_query)
2022-10-15 14:17:10 +00:00
init_tags_query = """
2022-10-15 15:28:02 +00:00
CREATE TABLE tags (
id SERIAL PRIMARY KEY,
name varchar(100)
2022-10-15 14:17:10 +00:00
);
"""
await connection.execute(init_tags_query)
init_categories_query = """
2022-10-15 15:28:02 +00:00
CREATE TABLE categories (
id SERIAL PRIMARY KEY,
name varchar(20)
2022-10-15 14:17:10 +00:00
);
"""
await connection.execute(init_categories_query)
2022-10-14 15:05:04 +00:00
init_projects_query = """
CREATE TABLE projects (
2022-10-15 15:28:02 +00:00
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 int REFERENCES tags,
category int REFERENCES categories,
creation_date date DEFAULT CURRENT_DATE
2022-10-14 15:05:04 +00:00
);
"""
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())