From 7f0411da4d7c3ca1fa2b642ccc245d5fc6f31617 Mon Sep 17 00:00:00 2001 From: Emmanuel Garette Date: Mon, 16 Dec 2019 16:17:27 +0100 Subject: [PATCH] local_ application has to be created in internal source --- messages/v1/messages/servermodel.list.yml | 2 +- .../services/servermodel/servermodel.py | 17 +++++-- src/risotto/services/source/source.py | 48 +++++++++++-------- 3 files changed, 43 insertions(+), 24 deletions(-) diff --git a/messages/v1/messages/servermodel.list.yml b/messages/v1/messages/servermodel.list.yml index 05be732..5faa2f5 100644 --- a/messages/v1/messages/servermodel.list.yml +++ b/messages/v1/messages/servermodel.list.yml @@ -9,7 +9,7 @@ pattern: rpc public: true parameters: - sourceid: + source_id: type: Number shortarg: s description: | diff --git a/src/risotto/services/servermodel/servermodel.py b/src/risotto/services/servermodel/servermodel.py index 745d954..da937b8 100644 --- a/src/risotto/services/servermodel/servermodel.py +++ b/src/risotto/services/servermodel/servermodel.py @@ -12,6 +12,17 @@ from ...error import ExecutionError class Risotto(Controller): + async def on_join(self, + risotto_context: Context) -> None: + internal_source = await self.call('v1.source.create', + risotto_context, + source_name='internal', + source_url='none') + self.internal_release_id = await self.call('v1.source.release.create', + risotto_context, + source_id=internal_source['source_id'], + release_name='none') + async def _servermodel_create(self, risotto_context: Context, servermodel_name: str, @@ -27,7 +38,7 @@ class Risotto(Controller): risotto_context, applicationservice_name=as_name, applicationservice_description=as_description, - release_id=release_id) + release_id=self.internal_release_id) servermodel = await risotto_context.connection.fetchval(servermodel_update, sm_name, sm_description, @@ -118,7 +129,7 @@ class Risotto(Controller): @register('v1.servermodel.list', None, database=True) async def servermodel_list(self, risotto_context: Context, - sourceid: int): + source_id: int): sql = ''' SELECT ServermodelId as servermodel_id, ServermodelName as servermodel_name, ServermodelDescription as servermodel_description, ServermodelParentsId as servermodel_parents_id, ServermodelReleaseId as release_id FROM Servermodel ''' @@ -177,4 +188,4 @@ class Risotto(Controller): """ - return {'servermodelid': 1, 'servermodelname': 'name', 'servermodeldescription': 'description', 'subreleasename': 'name', 'sourceid': 1, 'schema': schema, 'creolefuncs': ''} + return {'servermodelid': 1, 'servermodelname': 'name', 'servermodeldescription': 'description', 'subreleasename': 'name', 'source_id': 1, 'schema': schema, 'creolefuncs': ''} diff --git a/src/risotto/services/source/source.py b/src/risotto/services/source/source.py index 7d50dcf..9290f98 100644 --- a/src/risotto/services/source/source.py +++ b/src/risotto/services/source/source.py @@ -3,24 +3,6 @@ from ...controller import Controller from ...register import register from ...context import Context -VERSION_INIT = """ --- Création de la table Source -CREATE TABLE Source ( - SourceId SERIAL PRIMARY KEY, - SourceName VARCHAR(255) NOT NULL UNIQUE, - SourceURL TEXT -); - --- Création de la table Release -CREATE TABLE Release ( - ReleaseId SERIAL PRIMARY KEY, - ReleaseName VARCHAR(255) NOT NULL, - ReleaseSourceId INTEGER NOT NULL, - UNIQUE (ReleaseName, ReleaseSourceId), - FOREIGN KEY (ReleaseSourceId) REFERENCES Source(SourceId) -); -""" -RELEASE_QUERY = """SELECT ReleaseId as release_id, SourceName as source_name, SourceURL as source_url, ReleaseName as release_name FROM Release, Source WHERE Source.SourceId=Release.ReleaseSourceId""" class Risotto(Controller): @register('v1.source.create', None, database=True) @@ -64,6 +46,7 @@ class Risotto(Controller): risotto_context: Context, source_id: int, release_name: str): + # source.release.create is an upsert, do not using it release_insert = """INSERT INTO Release(ReleaseName, ReleaseSourceId) VALUES ($1, $2) RETURNING ReleaseId """ @@ -73,7 +56,32 @@ class Risotto(Controller): return {'release_id': release_id, 'release_name': release_name} + @register('v1.source.release.create', None, database=True) + async def source_release_create(self, + risotto_context: Context, + source_id: str, + release_name: str) -> Dict: + source_get = """SELECT SourceId as source_id, SourceName as source_name, SourceURL as source_url + FROM Source + WHERE SourceId = $1 + """ + release_upsert = """INSERT INTO Release(ReleaseName, ReleaseSourceId) VALUES ($1, $2) + ON CONFLICT (ReleaseName, ReleaseSourceId) DO UPDATE SET ReleaseName = $1 + RETURNING ReleaseId + """ + source = dict(await risotto_context.connection.fetchrow(source_get, + source_id)) + release_id = await risotto_context.connection.fetchval(release_upsert, + release_name, + source_id) + del source['source_id'] + source['release_id'] = release_id + source['release_name'] = release_name + return source + @register('v1.source.release.list', None, database=True) - async def release_list(self, risotto_context): - result = await risotto_context.connection.fetch(RELEASE_QUERY) + async def release_list(self, + risotto_context): + release_query = """SELECT ReleaseId as release_id, SourceName as source_name, SourceURL as source_url, ReleaseName as release_name FROM Release, Source WHERE Source.SourceId=Release.ReleaseSourceId""" + result = await risotto_context.connection.fetch(release_name) return [dict(r) for r in result]