- 
                Notifications
    
You must be signed in to change notification settings  - Fork 32
 
          ♻️ introduce licensed_resources (🗃️)
          #7190
        
          New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            matusdrobuliak66
  merged 9 commits into
  ITISFoundation:master
from
matusdrobuliak66:vip-models-split-licensed-items-2-attempt
  
      
      
   
  Feb 10, 2025 
      
    
  
     Merged
                    Changes from 6 commits
      Commits
    
    
            Show all changes
          
          
            9 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      eada1fd
              
                introduce licensed resources
              
              
                matusdrobuliak66 8a1fcb8
              
                Merge branch 'master' into vip-models-split-licensed-items-2-attempt
              
              
                matusdrobuliak66 70d2ba5
              
                Merge branch 'master' of github.com:ITISFoundation/osparc-simcore int…
              
              
                matusdrobuliak66 6907294
              
                fix revision id
              
              
                matusdrobuliak66 34d6d64
              
                remove redundant comment
              
              
                matusdrobuliak66 bf795e5
              
                Merge branch 'master' into vip-models-split-licensed-items-2-attempt
              
              
                matusdrobuliak66 8230164
              
                review @pcrespov
              
              
                matusdrobuliak66 6e5d93b
              
                fix revision id
              
              
                matusdrobuliak66 0da143c
              
                Merge branch 'master' into vip-models-split-licensed-items-2-attempt
              
              
                matusdrobuliak66 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            89 changes: 89 additions & 0 deletions
          
          89 
        
  ...e/src/simcore_postgres_database/migration/versions/68777fdf9539_add_licensed_resources.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| """add licensed resources | ||
| 
     | 
||
| Revision ID: 68777fdf9539 | ||
| Revises: 3fe27ff48f73 | ||
| Create Date: 2025-02-09 10:24:50.533653+00:00 | ||
| 
     | 
||
| """ | ||
| import sqlalchemy as sa | ||
| from alembic import op | ||
| from sqlalchemy.dialects import postgresql | ||
| 
     | 
||
| # revision identifiers, used by Alembic. | ||
| revision = "68777fdf9539" | ||
| down_revision = "3fe27ff48f73" | ||
| branch_labels = None | ||
| depends_on = None | ||
| 
     | 
||
| 
     | 
||
| def upgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.create_table( | ||
| "licensed_resources", | ||
| sa.Column( | ||
| "licensed_resource_id", | ||
| postgresql.UUID(as_uuid=True), | ||
| server_default=sa.text("gen_random_uuid()"), | ||
| nullable=False, | ||
| ), | ||
| sa.Column("display_name", sa.String(), nullable=False), | ||
| sa.Column("licensed_resource_name", sa.String(), nullable=False), | ||
| sa.Column( | ||
| "licensed_resource_type", | ||
| sa.Enum("VIP_MODEL", name="licensedresourcetype"), | ||
| nullable=False, | ||
| ), | ||
| sa.Column( | ||
| "licensed_resource_data", | ||
| postgresql.JSONB(astext_type=sa.Text()), | ||
| nullable=True, | ||
| ), | ||
| sa.Column( | ||
| "created", | ||
| sa.DateTime(timezone=True), | ||
| server_default=sa.text("now()"), | ||
| nullable=False, | ||
| ), | ||
| sa.Column( | ||
| "modified", | ||
| sa.DateTime(timezone=True), | ||
| server_default=sa.text("now()"), | ||
| nullable=False, | ||
| ), | ||
| sa.Column( | ||
| "trashed", | ||
| sa.DateTime(timezone=True), | ||
| nullable=True, | ||
| comment="The date and time when the licensed_resources was marked as trashed. Null if the licensed_resources has not been trashed [default].", | ||
| ), | ||
| sa.PrimaryKeyConstraint("licensed_resource_id"), | ||
| sa.UniqueConstraint( | ||
| "licensed_resource_name", | ||
| "licensed_resource_type", | ||
| name="uq_licensed_resource_name_type2", | ||
| ), | ||
| ) | ||
| # ### end Alembic commands ### | ||
| 
     | 
||
| # Migration of licensed resources from licensed_items table to new licensed_resources table | ||
| op.execute( | ||
| sa.DDL( | ||
| """ | ||
| INSERT INTO licensed_resources (display_name, licensed_resource_name, licensed_resource_type, licensed_resource_data, created, modified) | ||
| SELECT | ||
| display_name, | ||
| licensed_resource_name, | ||
| licensed_resource_type, | ||
| licensed_resource_data, | ||
| CURRENT_TIMESTAMP as created, | ||
| CURRENT_TIMESTAMP as modified | ||
| FROM licensed_items | ||
| """ | ||
| ) | ||
| ) | ||
| 
     | 
||
| 
     | 
||
| def downgrade(): | ||
| # ### commands auto generated by Alembic - please adjust! ### | ||
| op.drop_table("licensed_resources") | ||
| # ### end Alembic commands ### | 
        
          
  
    
      
          
            58 changes: 58 additions & 0 deletions
          
          58 
        
  packages/postgres-database/src/simcore_postgres_database/models/licensed_resources.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| """ resource_tracker_service_runs table | ||
| """ | ||
| 
     | 
||
| 
     | 
||
| import sqlalchemy as sa | ||
| from sqlalchemy.dialects import postgresql | ||
| 
     | 
||
| from ._common import ( | ||
| column_created_datetime, | ||
| column_modified_datetime, | ||
| column_trashed_datetime, | ||
| ) | ||
| from .base import metadata | ||
| from .licensed_items import LicensedResourceType | ||
| 
     | 
||
| licensed_resources = sa.Table( | ||
| "licensed_resources", | ||
| metadata, | ||
| sa.Column( | ||
| "licensed_resource_id", | ||
| postgresql.UUID(as_uuid=True), | ||
| nullable=False, | ||
| primary_key=True, | ||
| server_default=sa.text("gen_random_uuid()"), | ||
| ), | ||
| sa.Column( | ||
| "display_name", | ||
| sa.String, | ||
| nullable=False, | ||
| doc="Display name for front-end", | ||
| ), | ||
| sa.Column( | ||
| "licensed_resource_name", | ||
| sa.String, | ||
| nullable=False, | ||
| doc="Resource name identifier", | ||
| ), | ||
| sa.Column( | ||
| "licensed_resource_type", | ||
| sa.Enum(LicensedResourceType), | ||
| nullable=False, | ||
| doc="Resource type, ex. VIP_MODEL", | ||
| ), | ||
| sa.Column( | ||
| "licensed_resource_data", | ||
| postgresql.JSONB, | ||
| nullable=True, | ||
| doc="Resource metadata. Used for read-only purposes", | ||
| ), | ||
| column_created_datetime(timezone=True), | ||
| column_modified_datetime(timezone=True), | ||
| column_trashed_datetime("licensed_resources"), | ||
| sa.UniqueConstraint( | ||
| "licensed_resource_name", | ||
| "licensed_resource_type", | ||
| name="uq_licensed_resource_name_type2", | ||
| ), | ||
| ) | 
        
          
  
    
      
          
            6 changes: 0 additions & 6 deletions
          
          6 
        
  services/web/server/src/simcore_service_webserver/folders/_folders_repository.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            6 changes: 0 additions & 6 deletions
          
          6 
        
  services/web/server/src/simcore_service_webserver/licenses/_licensed_items_repository.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            104 changes: 104 additions & 0 deletions
          
          104 
        
  services/web/server/src/simcore_service_webserver/licenses/_licensed_resources_repository.py
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,104 @@ | ||
| import logging | ||
| from typing import Any | ||
| 
     | 
||
| from aiohttp import web | ||
| from models_library.licenses import LicensedResourceDB, LicensedResourceType | ||
| from simcore_postgres_database.models.licensed_resources import licensed_resources | ||
| from simcore_postgres_database.utils_repos import ( | ||
| get_columns_from_db_model, | ||
| pass_or_acquire_connection, | ||
| transaction_context, | ||
| ) | ||
| from sqlalchemy import func | ||
| from sqlalchemy.dialects import postgresql | ||
| from sqlalchemy.ext.asyncio import AsyncConnection | ||
| from sqlalchemy.sql import select | ||
| 
     | 
||
| from ..db.plugin import get_asyncpg_engine | ||
| from .errors import LicensedResourceNotFoundError | ||
| 
     | 
||
| _logger = logging.getLogger(__name__) | ||
| 
     | 
||
| 
     | 
||
| _SELECTION_ARGS = get_columns_from_db_model(licensed_resources, LicensedResourceDB) | ||
| 
     | 
||
| 
     | 
||
| def _create_insert_query( | ||
| display_name: str, | ||
| licensed_resource_name: str, | ||
| licensed_resource_type: LicensedResourceType, | ||
| licensed_resource_data: dict[str, Any] | None, | ||
| ): | ||
| return ( | ||
| postgresql.insert(licensed_resources) | ||
| .values( | ||
| licensed_resource_name=licensed_resource_name, | ||
| licensed_resource_type=licensed_resource_type, | ||
| licensed_resource_data=licensed_resource_data, | ||
| display_name=display_name, | ||
| created=func.now(), | ||
| modified=func.now(), | ||
| ) | ||
| .returning(*_SELECTION_ARGS) | ||
| ) | ||
| 
     | 
||
| 
     | 
||
| async def create_if_not_exists( | ||
| app: web.Application, | ||
| connection: AsyncConnection | None = None, | ||
| *, | ||
| display_name: str, | ||
| licensed_resource_name: str, | ||
| licensed_resource_type: LicensedResourceType, | ||
| licensed_resource_data: dict[str, Any] | None = None, | ||
| ) -> LicensedResourceDB: | ||
| 
     | 
||
| insert_or_none_query = _create_insert_query( | ||
| display_name, | ||
| licensed_resource_name, | ||
| licensed_resource_type, | ||
| licensed_resource_data, | ||
| ).on_conflict_do_nothing() | ||
| 
     | 
||
| async with transaction_context(get_asyncpg_engine(app), connection) as conn: | ||
| result = await conn.execute(insert_or_none_query) | ||
| row = result.one_or_none() | ||
| 
     | 
||
| if row is None: | ||
| select_query = select(*_SELECTION_ARGS).where( | ||
| (licensed_resources.c.licensed_resource_name == licensed_resource_name) | ||
| & ( | ||
| licensed_resources.c.licensed_resource_type | ||
| == licensed_resource_type | ||
| ) | ||
| ) | ||
| 
     | 
||
| result = await conn.execute(select_query) | ||
| row = result.one() | ||
| 
     | 
||
| assert row is not None # nosec | ||
| return LicensedResourceDB.model_validate(row) | ||
| 
     | 
||
| 
     | 
||
| async def get_by_resource_identifier( | ||
| app: web.Application, | ||
| connection: AsyncConnection | None = None, | ||
| *, | ||
| licensed_resource_name: str, | ||
| licensed_resource_type: LicensedResourceType, | ||
| ) -> LicensedResourceDB: | ||
| select_query = select(*_SELECTION_ARGS).where( | ||
| (licensed_resources.c.licensed_resource_name == licensed_resource_name) | ||
| & (licensed_resources.c.licensed_resource_type == licensed_resource_type) | ||
| ) | ||
| 
     | 
||
| async with pass_or_acquire_connection(get_asyncpg_engine(app), connection) as conn: | ||
| result = await conn.execute(select_query) | ||
| row = result.one_or_none() | ||
| if row is None: | ||
| raise LicensedResourceNotFoundError( | ||
| licensed_item_id="Unkown", # <-- NOTE: will be changed for licensed_resource_id | ||
| licensed_resource_name=licensed_resource_name, | ||
| licensed_resource_type=licensed_resource_type, | ||
| ) | ||
| return LicensedResourceDB.model_validate(row) | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.