risotto/script/database_manager.py

111 lines
3.2 KiB
Python
Raw Normal View History

2019-12-13 13:55:30 +01:00
import asyncpg
import asyncio
from risotto.config import get_config
VERSION_INIT = """
-- Source
2019-12-13 13:55:30 +01:00
CREATE TABLE Source (
SourceId SERIAL PRIMARY KEY,
SourceName VARCHAR(255) NOT NULL UNIQUE,
SourceURL TEXT
);
-- Release
2019-12-13 13:55:30 +01:00
CREATE TABLE Release (
ReleaseId SERIAL PRIMARY KEY,
ReleaseName VARCHAR(255) NOT NULL,
ReleaseSourceId INTEGER NOT NULL,
2019-12-19 09:18:41 +01:00
ReleaseDistribution VARCHAR(20) CONSTRAINT releasedistribution_choice CHECK (ReleaseDistribution IN ('last', 'n-1', 'n-2')),
2019-12-13 13:55:30 +01:00
UNIQUE (ReleaseName, ReleaseSourceId),
2019-12-18 17:11:42 +01:00
UNIQUE (ReleaseDistribution, ReleaseSourceId),
2019-12-13 13:55:30 +01:00
FOREIGN KEY (ReleaseSourceId) REFERENCES Source(SourceId)
);
-- Servermodel
2019-12-13 16:42:10 +01:00
CREATE TABLE Servermodel (
ServermodelId SERIAL PRIMARY KEY,
ServermodelName VARCHAR(255) NOT NULL,
ServermodelDescription VARCHAR(255) NOT NULL,
ServermodelParentsId INTEGER [] DEFAULT '{}',
ServermodelReleaseId INTEGER NOT NULL,
ServermodelApplicationserviceId INTEGER NOT NULL,
2019-12-13 16:42:10 +01:00
UNIQUE (ServermodelName, ServermodelReleaseId)
2019-12-13 13:55:30 +01:00
);
CREATE INDEX ServermodelApplicationserviceId_index ON Servermodel (ServermodelApplicationserviceId);
-- Applicationservice
CREATE TABLE Applicationservice (
ApplicationserviceId SERIAL PRIMARY KEY,
ApplicationserviceName VARCHAR(255) NOT NULL,
ApplicationserviceDescription VARCHAR(255) NOT NULL,
ApplicationserviceReleaseId INTEGER NOT NULL,
UNIQUE (ApplicationserviceName, ApplicationserviceReleaseId)
);
CREATE TABLE ApplicationserviceDependency (
ApplicationserviceId INTEGER NOT NULL,
ApplicationserviceDependencyId INTEGER NOT NULL,
UNIQUE(ApplicationserviceId, ApplicationserviceDependencyId),
FOREIGN KEY (ApplicationserviceId) REFERENCES Applicationservice(ApplicationserviceId),
FOREIGN KEY (ApplicationserviceDependencyId) REFERENCES Applicationservice(ApplicationserviceId)
2019-12-13 13:55:30 +01:00
);
-- Server
2019-12-19 17:24:20 +01:00
CREATE TABLE Server (
ServerId SERIAL PRIMARY KEY,
2019-12-20 10:58:12 +01:00
ServerName VARCHAR(255) NOT NULL UNIQUE,
2019-12-19 17:24:20 +01:00
ServerDescription VARCHAR(255) NOT NULL,
ServerServermodelId INTEGER NOT NULL
2019-12-13 13:55:30 +01:00
);
-- User, Role and ACL
2019-12-27 15:09:38 +01:00
CREATE TABLE RisottoUser (
UserId SERIAL PRIMARY KEY,
UserLogin VARCHAR(100) NOT NULL UNIQUE,
UserName VARCHAR(100) NOT NULL,
UserSurname VARCHAR(100) NOT NULL
);
CREATE TABLE UserRole (
RoleId SERIAL PRIMARY KEY,
RoleUserId INTEGER NOT NULL,
RoleName VARCHAR(255) NOT NULL,
RoleAttribute VARCHAR(255),
RoleAttributeValue VARCHAR(255),
FOREIGN KEY (RoleUserId) REFERENCES RisottoUser(UserId)
);
CREATE TABLE URI (
URIId SERIAL PRIMARY KEY,
URIName VARCHAR(255) NOT NULL UNIQUE
);
CREATE TABLE RoleURI (
RoleName VARCHAR(255) NOT NULL,
URIId INTEGER NOT NULL,
FOREIGN KEY (URIId) REFERENCES URI(URIId),
PRIMARY KEY (RoleName, URIId)
);
-- Log
2019-12-28 12:29:11 +01:00
CREATE TABLE log(
Msg VARCHAR(255) NOT NULL,
Level VARCHAR(10) NOT NULL,
2020-01-13 19:53:09 +01:00
Path VARCHAR(255),
2019-12-28 12:29:11 +01:00
Username VARCHAR(100) NOT NULL,
Data JSON,
Date timestamp DEFAULT current_timestamp
);
2019-12-13 13:55:30 +01:00
"""
async def main():
2020-01-30 16:22:06 +01:00
db_conf = get_config()['database']['dsn']
pool = await asyncpg.create_pool(db_conf)
2019-12-13 13:55:30 +01:00
async with pool.acquire() as connection:
async with connection.transaction():
returns = await connection.execute(VERSION_INIT)
2019-12-13 16:42:10 +01:00
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
# asyncio.run(main())