"""geenforce plugin baseline (real create).

This plugin was built after the ADR-008 ownership cutover, so this baseline
actually CREATES the plugin's tables (the core chain never knew about them). It
runs from `flask plugin install geenforce` / `flask plugin upgrade-all` after
`flask db upgrade` builds the core schema. All foreign keys are intra-plugin, so
tables are created parent-first. computertypeid / measuringtooltypeid /
publishedby are SOFT references (plain columns, no FK) to optional plugins/core.
"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = 'geenforce0001baseline'
down_revision = None
branch_labels = None
depends_on = None


def upgrade():
    op.create_table(
        'manifestscopes',
        sa.Column('scopeid', sa.Integer(), nullable=False),
        sa.Column('scopename', sa.String(length=64), nullable=False),
        sa.Column('phase', sa.String(length=16), nullable=False),
        sa.Column('computertypeid', sa.Integer(), nullable=True),
        sa.Column('measuringtooltypeid', sa.Integer(), nullable=True),
        sa.Column('manifestversion', sa.String(length=16), nullable=False),
        sa.Column('description', sa.String(length=255), nullable=True),
        sa.Column('topcomment', sa.Text(), nullable=True),
        sa.Column('site', sa.String(length=100), nullable=True),
        sa.Column('iscommon', sa.Boolean(), nullable=False),
        sa.Column('createddate', sa.DateTime(), nullable=False),
        sa.Column('modifieddate', sa.DateTime(), nullable=False),
        sa.Column('isactive', sa.Boolean(), nullable=False),
        sa.PrimaryKeyConstraint('scopeid'),
        sa.UniqueConstraint('scopename', 'phase', name='uq_scope_name_phase'),
    )
    op.create_table(
        'manifestentries',
        sa.Column('entryid', sa.Integer(), nullable=False),
        sa.Column('scopeid', sa.Integer(), nullable=False),
        sa.Column('sortorder', sa.Integer(), nullable=False),
        sa.Column('name', sa.String(length=128), nullable=False),
        sa.Column('entrytype', sa.String(length=16), nullable=False),
        sa.Column('comment', sa.Text(), nullable=True),
        sa.Column('installer', sa.String(length=255), nullable=True),
        sa.Column('installargs', sa.Text(), nullable=True),
        sa.Column('scriptpath', sa.String(length=255), nullable=True),
        sa.Column('scriptargs', sa.String(length=255), nullable=True),
        sa.Column('sourcepath', sa.String(length=255), nullable=True),
        sa.Column('destination', sa.String(length=255), nullable=True),
        sa.Column('regpath', sa.String(length=255), nullable=True),
        sa.Column('regname', sa.String(length=128), nullable=True),
        sa.Column('regvalue', sa.Text(), nullable=True),
        sa.Column('regtype', sa.String(length=16), nullable=True),
        sa.Column('payloadsource', sa.String(length=8), nullable=False),
        sa.Column('payloadref', sa.String(length=512), nullable=True),
        sa.Column('payloadsha256', sa.String(length=64), nullable=True),
        sa.Column('detectionmethod', sa.String(length=16), nullable=True),
        sa.Column('detectionpath', sa.String(length=255), nullable=True),
        sa.Column('detectionname', sa.String(length=128), nullable=True),
        sa.Column('detectionvalue', sa.String(length=255), nullable=True),
        sa.Column('detectionpattern', sa.String(length=255), nullable=True),
        sa.Column('cmmversion', sa.String(length=16), nullable=True),
        sa.Column('logfile', sa.String(length=255), nullable=True),
        sa.Column('waittimeoutsec', sa.Integer(), nullable=True),
        sa.Column('applymode', sa.String(length=16), nullable=True),
        sa.Column('updatewindow', sa.String(length=11), nullable=True),
        sa.Column('appid', sa.Integer(), nullable=True),
        sa.Column('preenrollment', sa.Boolean(), nullable=False),
        sa.Column('killafterdetection', sa.Boolean(), nullable=False),
        sa.Column('pctypesstrict', sa.Boolean(), nullable=False),
        sa.Column('createddate', sa.DateTime(), nullable=False),
        sa.Column('modifieddate', sa.DateTime(), nullable=False),
        sa.Column('isactive', sa.Boolean(), nullable=False),
        sa.ForeignKeyConstraint(['scopeid'], ['manifestscopes.scopeid'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('entryid'),
        sa.UniqueConstraint('scopeid', 'name', name='uq_entry_scope_name'),
    )
    op.create_index('idx_entry_scope', 'manifestentries', ['scopeid'])
    op.create_index('idx_entry_scope_order', 'manifestentries',
                    ['scopeid', 'sortorder'])
    op.create_table(
        'manifestentrypctypes',
        sa.Column('entrypctypeid', sa.Integer(), nullable=False),
        sa.Column('entryid', sa.Integer(), nullable=False),
        sa.Column('sortorder', sa.Integer(), nullable=False),
        sa.Column('pctypevalue', sa.String(length=64), nullable=False),
        sa.ForeignKeyConstraint(['entryid'], ['manifestentries.entryid'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('entrypctypeid'),
    )
    op.create_index('idx_pctype_entry', 'manifestentrypctypes', ['entryid'])
    op.create_table(
        'manifestentryhostnames',
        sa.Column('entryhostnameid', sa.Integer(), nullable=False),
        sa.Column('entryid', sa.Integer(), nullable=False),
        sa.Column('sortorder', sa.Integer(), nullable=False),
        sa.Column('hostnamepattern', sa.String(length=64), nullable=False),
        sa.ForeignKeyConstraint(['entryid'], ['manifestentries.entryid'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('entryhostnameid'),
    )
    op.create_index('idx_hostname_entry', 'manifestentryhostnames', ['entryid'])
    op.create_table(
        'manifestentrymachinenumbers',
        sa.Column('entrymachinenumberid', sa.Integer(), nullable=False),
        sa.Column('entryid', sa.Integer(), nullable=False),
        sa.Column('sortorder', sa.Integer(), nullable=False),
        sa.Column('machinenumber', sa.String(length=16), nullable=False),
        sa.ForeignKeyConstraint(['entryid'], ['manifestentries.entryid'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('entrymachinenumberid'),
    )
    op.create_index('idx_machinenumber_entry', 'manifestentrymachinenumbers',
                    ['entryid'])
    op.create_table(
        'manifestinusechecks',
        sa.Column('inusecheckid', sa.Integer(), nullable=False),
        sa.Column('entryid', sa.Integer(), nullable=False),
        sa.Column('behavior', sa.String(length=20), nullable=False),
        sa.ForeignKeyConstraint(['entryid'], ['manifestentries.entryid'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('inusecheckid'),
        sa.UniqueConstraint('entryid'),
    )
    op.create_table(
        'manifestinusecheckprocesses',
        sa.Column('inusecheckprocessid', sa.Integer(), nullable=False),
        sa.Column('inusecheckid', sa.Integer(), nullable=False),
        sa.Column('sortorder', sa.Integer(), nullable=False),
        sa.Column('processname', sa.String(length=64), nullable=False),
        sa.Column('exepath', sa.String(length=255), nullable=True),
        sa.Column('gracefulclosetimeoutsec', sa.Integer(), nullable=True),
        sa.ForeignKeyConstraint(['inusecheckid'],
                                ['manifestinusechecks.inusecheckid'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('inusecheckprocessid'),
    )
    op.create_index('idx_process_check', 'manifestinusecheckprocesses',
                    ['inusecheckid'])
    op.create_table(
        'manifestpublishedversions',
        sa.Column('publishedversionid', sa.Integer(), nullable=False),
        sa.Column('scopeid', sa.Integer(), nullable=False),
        sa.Column('versionnumber', sa.Integer(), nullable=False),
        sa.Column('manifestjson', sa.Text(length=16777215), nullable=False),
        sa.Column('publishedat', sa.DateTime(), nullable=False),
        sa.Column('publishedby', sa.Integer(), nullable=True),
        sa.Column('iscurrent', sa.Boolean(), nullable=False),
        sa.Column('notes', sa.String(length=255), nullable=True),
        sa.ForeignKeyConstraint(['scopeid'], ['manifestscopes.scopeid'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('publishedversionid'),
        sa.UniqueConstraint('scopeid', 'versionnumber',
                            name='uq_published_scope_version'),
    )
    op.create_index('idx_published_scope', 'manifestpublishedversions',
                    ['scopeid'])
    op.create_table(
        'manifestpayloads',
        sa.Column('payloadid', sa.Integer(), nullable=False),
        sa.Column('entryid', sa.Integer(), nullable=False),
        sa.Column('filename', sa.String(length=255), nullable=False),
        sa.Column('contenttype', sa.String(length=128), nullable=True),
        sa.Column('payloadbytes', sa.LargeBinary(length=16777215), nullable=False),
        sa.Column('payloadsha256', sa.String(length=64), nullable=False),
        sa.Column('uploadedat', sa.DateTime(), nullable=False),
        sa.ForeignKeyConstraint(['entryid'], ['manifestentries.entryid'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('payloadid'),
    )
    op.create_index('idx_payload_entry', 'manifestpayloads', ['entryid'])
    op.create_table(
        'manifestenforcementreports',
        sa.Column('reportid', sa.Integer(), nullable=False),
        sa.Column('hostname', sa.String(length=100), nullable=False),
        sa.Column('scopename', sa.String(length=64), nullable=False),
        sa.Column('phase', sa.String(length=16), nullable=False),
        sa.Column('appliedversion', sa.Integer(), nullable=True),
        sa.Column('enforcerversion', sa.String(length=20), nullable=True),
        sa.Column('installedcount', sa.Integer(), nullable=False),
        sa.Column('skippedcount', sa.Integer(), nullable=False),
        sa.Column('failedcount', sa.Integer(), nullable=False),
        sa.Column('filteredcount', sa.Integer(), nullable=False),
        sa.Column('status', sa.String(length=16), nullable=False),
        sa.Column('lastcheckin', sa.DateTime(), nullable=True),
        sa.Column('receivedat', sa.DateTime(), nullable=False),
        sa.Column('iscurrent', sa.Boolean(), nullable=False),
        sa.PrimaryKeyConstraint('reportid'),
    )
    op.create_index('idx_report_host', 'manifestenforcementreports',
                    ['hostname'])
    op.create_index('idx_report_current', 'manifestenforcementreports',
                    ['iscurrent'])
    op.create_index('idx_report_host_scope', 'manifestenforcementreports',
                    ['hostname', 'scopename', 'phase'])
    op.create_table(
        'manifestenforcementresults',
        sa.Column('resultid', sa.Integer(), nullable=False),
        sa.Column('reportid', sa.Integer(), nullable=False),
        sa.Column('entryname', sa.String(length=128), nullable=False),
        sa.Column('action', sa.String(length=16), nullable=False),
        sa.Column('selfhealed', sa.Boolean(), nullable=False),
        sa.Column('exitcode', sa.Integer(), nullable=True),
        sa.Column('message', sa.Text(), nullable=True),
        sa.ForeignKeyConstraint(['reportid'],
                                ['manifestenforcementreports.reportid'],
                                ondelete='CASCADE'),
        sa.PrimaryKeyConstraint('resultid'),
    )
    op.create_index('idx_result_report', 'manifestenforcementresults',
                    ['reportid'])
    op.create_table(
        'pctypealiases',
        sa.Column('aliasid', sa.Integer(), nullable=False),
        sa.Column('aliasgroup', sa.Integer(), nullable=False),
        sa.Column('aliasname', sa.String(length=64), nullable=False),
        sa.PrimaryKeyConstraint('aliasid'),
        sa.UniqueConstraint('aliasgroup', 'aliasname',
                            name='uq_alias_group_name'),
    )
    op.create_index('idx_alias_group', 'pctypealiases', ['aliasgroup'])


def downgrade():
    op.drop_table('pctypealiases')
    op.drop_table('manifestenforcementresults')
    op.drop_table('manifestenforcementreports')
    op.drop_table('manifestpayloads')
    op.drop_table('manifestpublishedversions')
    op.drop_table('manifestinusecheckprocesses')
    op.drop_table('manifestinusechecks')
    op.drop_table('manifestentrymachinenumbers')
    op.drop_table('manifestentryhostnames')
    op.drop_table('manifestentrypctypes')
    op.drop_table('manifestentries')
    op.drop_table('manifestscopes')
