Script 3fdedd58ac73_add_module_table_py
[hide private]
[frames] | no frames]

Source Code for Script script-3fdedd58ac73_add_module_table_py

  1  """add module table 
  2   
  3  Revision ID: 3fdedd58ac73 
  4  Revises: 412c2c8d9da 
  5  Create Date: 2016-10-26 22:01:09.361070 
  6   
  7  """ 
  8   
  9  # revision identifiers, used by Alembic. 
 10  revision = '3fdedd58ac73' 
 11  down_revision = '412c2c8d9da' 
 12   
 13  from alembic import op 
 14  import sqlalchemy as sa 
 15   
 16  from coprs.models import Module, Action, Copr, User, Group 
 17  from sqlalchemy.orm import sessionmaker 
 18   
 19  import json 
 20  import base64 
 21  import modulemd 
 22  import yaml 
 23  from coprs.logic.coprs_logic import CoprsLogic 
 24  from coprs.logic.actions_logic import ActionsLogic 
 25  from coprs.helpers import ActionTypeEnum 
 26   
 27   
28 -def upgrade():
29 bind = op.get_bind() 30 Session = sessionmaker() 31 session = Session(bind=bind) 32 33 op.create_table( 34 "module", 35 sa.Column("id", sa.Integer, primary_key=True), 36 sa.Column("name", sa.String(100), nullable=False), 37 sa.Column("stream", sa.String(100), nullable=False), 38 sa.Column("version", sa.Integer, nullable=False), 39 sa.Column("summary", sa.String(100), nullable=False), 40 sa.Column("description", sa.Text), 41 sa.Column("created_on", sa.Integer, nullable=True), 42 sa.Column("yaml_b64", sa.Text), 43 sa.Column("copr_id", sa.Integer, sa.ForeignKey("copr.id")), 44 ) 45 op.create_unique_constraint("unique_name_stream_version_copr_id", "module", 46 ["name", "stream", "version", "copr_id"]) 47 session.commit() 48 49 # Now, let's seed the table with existing modules which are violently stored in the `action` table 50 added_modules = set() 51 for action in ActionsLogic.get_many(ActionTypeEnum("build_module")).order_by(Action.id.desc()): 52 data = json.loads(action.data) 53 if not "ownername" in data: 54 continue # already new action format 55 copr = get_copr(session, data["ownername"], data["projectname"]) 56 yml_str = base64.b64decode(data["modulemd_b64"]) 57 yml = yaml.safe_load(yml_str) 58 59 mmd = modulemd.ModuleMetadata() 60 mmd.name = yml["data"]["name"] 61 mmd.stream = "" 62 mmd.version = action.created_on 63 mmd.summary = yml["data"]["summary"] 64 65 if "filter" in yml["data"]["components"]["rpms"]: 66 for package in yml["data"]["components"]["rpms"]["filter"]: 67 mmd.filter.add_rpm(package) 68 69 for package in yml["data"]["components"]["rpms"]["api"]: 70 mmd.api.add_rpm(package) 71 72 for profile_name in yml["data"]["profiles"]: 73 mmd.profiles[profile_name] = modulemd.profile.ModuleProfile() 74 for package in yml["data"]["profiles"][profile_name]["rpms"]: 75 mmd.profiles[profile_name].add_rpm(package) 76 77 module_kwargs = { 78 "name": mmd.name, 79 "stream": mmd.stream, 80 "version": mmd.version, 81 "summary": mmd.summary, 82 "description": mmd.description, 83 "yaml_b64": base64.b64encode(mmd.dumps()), 84 "created_on": action.created_on, 85 "copr_id": copr.id, 86 } 87 88 # There is no constraint for currently existing modules, but in new table, there 89 # must be unique (copr, nsv). Therefore in the case of duplicit modules, 90 # we will add only the newest one 91 if full_module_name(copr, mmd) in added_modules: 92 print("Skipping {}; Already exists".format(full_module_name(copr, mmd))) 93 continue 94 else: 95 print("Adding {}".format(full_module_name(copr, mmd))) 96 97 session.add(Module(**module_kwargs)) 98 added_modules.add(full_module_name(copr, mmd))
99 100
101 -def downgrade():
102 ### commands auto generated by Alembic - please adjust! ### 103 op.drop_table('module')
104 ### end Alembic commands ### 105 106
107 -def full_module_name(copr, mmd):
108 return "{}/{}-{}-{}".format( 109 copr.full_name, mmd.name, mmd.stream, mmd.version 110 )
111 112
113 -def get_copr(session, ownername, projectname):
114 if ownername[0] == "@": 115 coprs = CoprsLogic.filter_by_group_name(session.query(Copr), ownername[1:]) 116 else: 117 coprs = CoprsLogic.filter_by_user_name(session.query(Copr), ownername) 118 return CoprsLogic.filter_by_name(coprs, projectname).first()
119