|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# You may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# http://www.apache.org/licenses/LICENSE-2.0OA |
| 7 | +# |
| 8 | +# Authors: |
| 9 | +# - Wen Guan, <[email protected]>, 2025 |
| 10 | + |
| 11 | +"""add campaign group |
| 12 | +
|
| 13 | +Revision ID: 6931b48500a2 |
| 14 | +Revises: 8fe5cab9bde1 |
| 15 | +Create Date: 2025-02-21 11:44:59.871046+00:00 |
| 16 | +
|
| 17 | +""" |
| 18 | +import datetime |
| 19 | +from alembic import op |
| 20 | +from alembic import context |
| 21 | +import sqlalchemy as sa |
| 22 | + |
| 23 | +from idds.common.constants import (RequestGroupType, RequestGroupStatus, RequestGroupLocking) |
| 24 | +from idds.orm.base.types import JSON, JSONString, EnumWithValue |
| 25 | +from idds.common.constants import (SCOPE_LENGTH, NAME_LENGTH) |
| 26 | + |
| 27 | + |
| 28 | +# revision identifiers, used by Alembic. |
| 29 | +revision = '6931b48500a2' |
| 30 | +down_revision = '8fe5cab9bde1' |
| 31 | +branch_labels = None |
| 32 | +depends_on = None |
| 33 | + |
| 34 | + |
| 35 | +def upgrade() -> None: |
| 36 | + if context.get_context().dialect.name in ['oracle', 'mysql', 'postgresql']: |
| 37 | + schema = context.get_context().version_table_schema if context.get_context().version_table_schema else '' |
| 38 | + |
| 39 | + # requests_group table |
| 40 | + op.create_table('requests_group', |
| 41 | + sa.Column('group_id', sa.BigInteger(), sa.Sequence('REQUEST_GROUP_ID_SEQ', schema=schema)), |
| 42 | + sa.Column('campaign', sa.String(50), nullable=False, default='Default'), |
| 43 | + sa.Column('campaign_scope', sa.String(SCOPE_LENGTH), nullable=False, default='Default'), |
| 44 | + sa.Column('campaign_group', sa.String(NAME_LENGTH), nullable=False), |
| 45 | + sa.Column('campaign_tag', sa.String(100), nullable=False), |
| 46 | + sa.Column('requester', sa.String(20)), |
| 47 | + sa.Column('group_type', EnumWithValue(RequestGroupType), nullable=False), |
| 48 | + sa.Column('username', sa.String(20)), |
| 49 | + sa.Column('userdn', sa.String(200)), |
| 50 | + sa.Column('priority', sa.Integer(), nullable=False, default=50), |
| 51 | + sa.Column('status', EnumWithValue(RequestGroupStatus), nullable=False), |
| 52 | + sa.Column('substatus', EnumWithValue(RequestGroupStatus), default=0), |
| 53 | + sa.Column('oldstatus', EnumWithValue(RequestGroupStatus), default=0), |
| 54 | + sa.Column('locking', EnumWithValue(RequestGroupLocking), nullable=False), |
| 55 | + sa.Column("created_at", sa.DateTime, default=datetime.datetime.utcnow, nullable=False), |
| 56 | + sa.Column("updated_at", sa.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow, nullable=False), |
| 57 | + sa.Column("next_poll_at", sa.DateTime, default=datetime.datetime.utcnow), |
| 58 | + sa.Column("accessed_at", sa.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow), |
| 59 | + sa.Column("expired_at", sa.DateTime), |
| 60 | + sa.Column('new_retries', sa.Integer(), default=0), |
| 61 | + sa.Column('update_retries', sa.Integer(), default=0), |
| 62 | + sa.Column('max_new_retries', sa.Integer(), default=3), |
| 63 | + sa.Column('max_update_retries', sa.Integer(), default=0), |
| 64 | + sa.Column('new_poll_period', sa.Interval(), default=datetime.timedelta(seconds=1)), |
| 65 | + sa.Column('update_poll_period', sa.Interval(), default=datetime.timedelta(seconds=10)), |
| 66 | + sa.Column('site', sa.String(50)), |
| 67 | + sa.Column('locking_hostname', sa.String(50)), |
| 68 | + sa.Column('locking_pid', sa.BigInteger(), autoincrement=False), |
| 69 | + sa.Column('locking_thread_id', sa.BigInteger(), autoincrement=False), |
| 70 | + sa.Column('locking_thread_name', sa.String(100)), |
| 71 | + sa.Column('errors', JSONString(1024)), |
| 72 | + sa.Column('group_metadata', JSON()), |
| 73 | + sa.Column('processing_metadata', JSON()), |
| 74 | + schema=schema) |
| 75 | + |
| 76 | + op.create_primary_key('REQUESTGROUP_PK', 'requests_group', ['group_id'], schema=schema) |
| 77 | + op.create_unique_constraint('REQUESTGROUP_CM_UQ', 'requests_group', ['campaign', 'campaign_scope', 'campaign_group', 'campaign_tag'], schema=schema) |
| 78 | + op.create_check_constraint('REQUESTGROUP_STATUS_ID_NN', 'requests_group', 'status IS NOT NULL', schema=schema) |
| 79 | + op.create_index('REQUESTGROUP_CM_NAME_IDX', 'requests_group', ['campaign', 'campaign_scope', 'campaign_group', 'campaign_tag'], unique=True, schema=schema) |
| 80 | + op.create_index('REQUESTGROUP_STATUS_SITE', 'requests_group', ['status', 'site', 'group_id']), |
| 81 | + op.create_index('REQUESTGROUP_STATUS_PRIO_IDX', 'requests_group', ['status', 'priority', 'group_id', 'locking', 'updated_at', 'next_poll_at', 'created_at']), |
| 82 | + op.create_index('REQUESTGROUP_STATUS_POLL_IDX', 'requests_group', ['status', 'priority', 'locking', 'updated_at', 'new_poll_period', 'update_poll_period', 'created_at', 'group_id']) |
| 83 | + |
| 84 | + # requests |
| 85 | + op.alter_column('requests', 'campaign_tag', type_=sa.String(100), schema=schema) |
| 86 | + op.add_column('requests', sa.Column('group_id', sa.BigInteger()), schema=schema) |
| 87 | + op.create_foreign_key('REQUESTS_GROUP_ID_FK', 'requests', 'requests_group', ['group_id'], ['group_id'], source_schema=schema, referent_schema=schema) |
| 88 | + |
| 89 | + |
| 90 | +def downgrade() -> None: |
| 91 | + if context.get_context().dialect.name in ['oracle', 'mysql', 'postgresql']: |
| 92 | + schema = context.get_context().version_table_schema if context.get_context().version_table_schema else '' |
| 93 | + |
| 94 | + # requests_group |
| 95 | + op.drop_constraint('REQUESTGROUP_STATUS_ID_NN', table_name='requests_group', schema=schema) |
| 96 | + op.drop_constraint('REQUESTGROUP_CM_UQ', table_name='requests_group', schema=schema) |
| 97 | + op.drop_constraint('REQUESTGROUP_PK', table_name='requests_group', schema=schema) |
| 98 | + |
| 99 | + op.drop_index('REQUESTGROUP_CM_NAME_IDX', table_name='requests_group', schema=schema) |
| 100 | + op.drop_index('REQUESTGROUP_STATUS_SITE', table_name='requests_group', schema=schema) |
| 101 | + op.drop_index('REQUESTGROUP_STATUS_PRIO_IDX', table_name='requests_group', schema=schema) |
| 102 | + op.drop_index('REQUESTGROUP_STATUS_POLL_IDX', table_name='requests_group', schema=schema) |
| 103 | + |
| 104 | + op.drop_table('requests_group', schema=schema) |
| 105 | + |
| 106 | + # requests |
| 107 | + op.alter_column('requests', 'campaign_tag', type_=sa.String(20), schema=schema) |
| 108 | + op.drop_constraint('REQUESTS_GROUP_ID_FK', table_name='requests', schema=schema) |
| 109 | + op.drop_column('requests', 'group_id', schema=schema) |
0 commit comments