Skip to content

Commit

Permalink
Database: Add create_constraint parameter; Fix #4737
Browse files Browse the repository at this point in the history
Rename SUBS_HISTORY_RETROACTIVE_CHK (as in schema.sql for Oracle)
  • Loading branch information
bziemons committed Jul 16, 2021
1 parent 99b5095 commit 386a88f
Show file tree
Hide file tree
Showing 21 changed files with 276 additions and 100 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2020 CERN
# Copyright 2015-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
# - Vincent Garonne <vincent.garonne@cern.ch>, 2015-2017
# - Martin Barisits <martin.barisits@cern.ch>, 2016
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019-2020
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2021

''' add didtype_chck to requests '''

Expand All @@ -41,7 +42,10 @@ def upgrade():

if context.get_context().dialect.name in ['oracle', 'mysql']: # pylint: disable=no-member
add_column('requests', sa.Column('did_type',
sa.Enum(DIDType, name='REQUESTS_DIDTYPE_CHK', values_callable=lambda obj: [e.value for e in obj]),
sa.Enum(DIDType,
name='REQUESTS_DIDTYPE_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj]),
default=DIDType.FILE), schema=schema[:-1])
# we don't want checks on the history table, fake the DID type
add_column('requests_history', sa.Column('did_type', sa.String(1)), schema=schema[:-1])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,10 +14,11 @@
# limitations under the License.
#
# Authors:
# - Vincent Garonne <vgaronne@gmail.com>, 2015-2017
# - Vincent Garonne <vincent.garonne@cern.ch>, 2015-2017
# - Martin Barisits <martin.barisits@cern.ch>, 2016-2017
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019-2021
# - Robert Illingworth <illingwo@fnal.gov>, 2019
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2021

''' asynchronous rules and rule approval '''

Expand All @@ -39,19 +41,19 @@ def upgrade():
schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''

if context.get_context().dialect.name == 'oracle':
add_column('rules', sa.Column('ignore_account_limit', sa.Boolean(name='RULES_IGNORE_ACCOUNT_LIMIT_CHK'), default=False))
add_column('rules', sa.Column('ignore_account_limit', sa.Boolean(name='RULES_IGNORE_ACCOUNT_LIMIT_CHK', create_constraint=True), default=False))
try_drop_constraint('RULES_STATE_CHK', 'rules')
create_check_constraint('RULES_STATE_CHK', 'rules', "state IN ('S', 'R', 'U', 'O', 'W', 'I')")

elif context.get_context().dialect.name == 'postgresql':
add_column('rules', sa.Column('ignore_account_limit', sa.Boolean(name='RULES_IGNORE_ACCOUNT_LIMIT_CHK'), default=False), schema=schema[:-1])
add_column('rules', sa.Column('ignore_account_limit', sa.Boolean(name='RULES_IGNORE_ACCOUNT_LIMIT_CHK', create_constraint=True), default=False), schema=schema[:-1])
op.execute('ALTER TABLE ' + schema + 'rules DROP CONSTRAINT IF EXISTS "RULES_STATE_CHK", ALTER COLUMN state TYPE CHAR')
op.execute("DROP TYPE \"RULES_STATE_CHK\"")
op.execute("CREATE TYPE \"RULES_STATE_CHK\" AS ENUM('S', 'R', 'U', 'O', 'W', 'I')")
op.execute("ALTER TABLE %srules ALTER COLUMN state TYPE \"RULES_STATE_CHK\" USING state::\"RULES_STATE_CHK\"" % schema)

elif context.get_context().dialect.name == 'mysql':
add_column('rules', sa.Column('ignore_account_limit', sa.Boolean(name='RULES_IGNORE_ACCOUNT_LIMIT_CHK'), default=False), schema=schema[:-1])
add_column('rules', sa.Column('ignore_account_limit', sa.Boolean(name='RULES_IGNORE_ACCOUNT_LIMIT_CHK', create_constraint=True), default=False), schema=schema[:-1])
op.execute('ALTER TABLE ' + schema + 'rules DROP CHECK RULES_STATE_CHK') # pylint: disable=no-member
create_check_constraint('RULES_STATE_CHK', 'rules', "state IN ('S', 'R', 'U', 'O', 'W', 'I')")

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Copyright 2013-2019 CERN for the benefit of the ATLAS collaboration.
# -*- coding: utf-8 -*-
# Copyright 2015-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -13,9 +14,10 @@
# limitations under the License.
#
# Authors:
# - Martin Barisits <martin.barisits@cern.ch>, 2015
# - Vincent Garonne <vincent.garonne@cern.ch>, 2017
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019
# - Vincent Garonne <vincent.garonne@cern.ch>, 2015-2017
# - Martin Barisits <martin.barisits@cern.ch>, 2016
# - Mario Lassnig <mario.lassnig@cern.ch>, 2017-2019
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2021

''' add ignore_availability column to rules '''

Expand All @@ -37,7 +39,7 @@ def upgrade():

if context.get_context().dialect.name in ['oracle', 'mysql', 'postgresql']:
schema = context.get_context().version_table_schema if context.get_context().version_table_schema else ''
add_column('rules', sa.Column('ignore_availability', sa.Boolean(name='RULES_IGNORE_AVAILABILITY_CHK'), default=False), schema=schema)
add_column('rules', sa.Column('ignore_availability', sa.Boolean(name='RULES_IGNORE_AVAILABILITY_CHK', create_constraint=True), default=False), schema=schema)


def downgrade():
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2020 CERN
# Copyright 2015-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,6 +18,7 @@
# - Martin Barisits <martin.barisits@cern.ch>, 2016
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019-2020
# - James Perry <j.perry@epcc.ed.ac.uk>, 2020
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2021

''' add convention table and closed_at to dids '''

Expand Down Expand Up @@ -50,7 +51,10 @@ def upgrade():
create_table('naming_conventions',
sa.Column('scope', sa.String(get_schema_value('SCOPE_LENGTH'))),
sa.Column('regexp', sa.String(255)),
sa.Column('convention_type', sa.Enum(KeyType, name='CVT_TYPE_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('convention_type', sa.Enum(KeyType,
name='CVT_TYPE_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('updated_at', sa.DateTime, default=datetime.datetime.utcnow),
sa.Column('created_at', sa.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow))
create_primary_key('NAMING_CONVENTIONS_PK', 'naming_conventions', ['scope'])
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,10 +14,11 @@
# limitations under the License.
#
# Authors:
# - Vincent Garonne <vgaronne@gmail.com>, 2015-2017
# - Vincent Garonne <vincent.garonne@cern.ch>, 2015-2017
# - Martin Barisits <martin.barisits@cern.ch>, 2016
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019-2021
# - Robert Illingworth <illingwo@fnal.gov>, 2019
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2021

''' added staging_area column '''

Expand All @@ -39,19 +41,19 @@ def upgrade():
schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''

if context.get_context().dialect.name == 'oracle':
add_column('rses', sa.Column('staging_area', sa.Boolean(name='RSE_STAGING_AREA_CHK'), default=False))
add_column('rses', sa.Column('staging_area', sa.Boolean(name='RSE_STAGING_AREA_CHK', create_constraint=True), default=False))
try_drop_constraint('REQUESTS_TYPE_CHK', 'requests')
create_check_constraint(constraint_name='REQUESTS_TYPE_CHK', table_name='requests',
condition="request_type in ('U', 'D', 'T', 'I', '0')")

elif context.get_context().dialect.name == 'postgresql':
add_column('rses', sa.Column('staging_area', sa.Boolean(name='RSE_STAGING_AREA_CHK'), default=False), schema=schema[:-1])
add_column('rses', sa.Column('staging_area', sa.Boolean(name='RSE_STAGING_AREA_CHK', create_constraint=True), default=False), schema=schema[:-1])
drop_constraint('REQUESTS_TYPE_CHK', 'requests', type_='check')
create_check_constraint(constraint_name='REQUESTS_TYPE_CHK', table_name='requests',
condition="request_type in ('U', 'D', 'T', 'I', '0')")

elif context.get_context().dialect.name == 'mysql':
add_column('rses', sa.Column('staging_area', sa.Boolean(name='RSE_STAGING_AREA_CHK'), default=False), schema=schema[:-1])
add_column('rses', sa.Column('staging_area', sa.Boolean(name='RSE_STAGING_AREA_CHK', create_constraint=True), default=False), schema=schema[:-1])
op.execute('ALTER TABLE ' + schema + 'requests DROP CHECK REQUESTS_TYPE_CHK') # pylint: disable=no-member
create_check_constraint(constraint_name='REQUESTS_TYPE_CHK', table_name='requests',
condition="request_type in ('U', 'D', 'T', 'I', '0')")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2020 CERN
# Copyright 2015-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
# - Vincent Garonne <vincent.garonne@cern.ch>, 2015-2017
# - Martin Barisits <martin.barisits@cern.ch>, 2016
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019-2020
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2021

''' created rule history tables '''

Expand Down Expand Up @@ -49,8 +50,14 @@ def upgrade():
sa.Column('account', sa.String(25)),
sa.Column('scope', sa.String(25)),
sa.Column('name', sa.String(255)),
sa.Column('did_type', sa.Enum(DIDType, name='RULES_HIST_RECENT_DIDTYPE_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('state', sa.Enum(RuleState, name='RULES_HIST_RECENT_STATE_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('did_type', sa.Enum(DIDType,
name='RULES_HIST_RECENT_DIDTYPE_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('state', sa.Enum(RuleState,
name='RULES_HIST_RECENT_STATE_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('error', sa.String(255)),
sa.Column('rse_expression', sa.String(255)),
sa.Column('copies', sa.SmallInteger),
Expand All @@ -62,8 +69,14 @@ def upgrade():
sa.Column('locks_stuck_cnt', sa.BigInteger),
sa.Column('source_replica_expression', sa.String(255)),
sa.Column('activity', sa.String(50)),
sa.Column('grouping', sa.Enum(RuleGrouping, name='RULES_HIST_RECENT_GROUPING_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('notification', sa.Enum(RuleNotification, name='RULES_HIST_RECENT_NOTIFY_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('grouping', sa.Enum(RuleGrouping,
name='RULES_HIST_RECENT_GROUPING_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('notification', sa.Enum(RuleNotification,
name='RULES_HIST_RECENT_NOTIFY_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('stuck_at', sa.DateTime),
sa.Column('purge_replicas', sa.Boolean()),
sa.Column('ignore_availability', sa.Boolean()),
Expand All @@ -77,8 +90,14 @@ def upgrade():
sa.Column('account', sa.String(25)),
sa.Column('scope', sa.String(25)),
sa.Column('name', sa.String(255)),
sa.Column('did_type', sa.Enum(DIDType, name='RULES_HISTORY_DIDTYPE_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('state', sa.Enum(RuleState, name='RULES_HISTORY_STATE_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('did_type', sa.Enum(DIDType,
name='RULES_HISTORY_DIDTYPE_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('state', sa.Enum(RuleState,
name='RULES_HISTORY_STATE_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('error', sa.String(255)),
sa.Column('rse_expression', sa.String(255)),
sa.Column('copies', sa.SmallInteger),
Expand All @@ -90,8 +109,14 @@ def upgrade():
sa.Column('locks_stuck_cnt', sa.BigInteger),
sa.Column('source_replica_expression', sa.String(255)),
sa.Column('activity', sa.String(50)),
sa.Column('grouping', sa.Enum(RuleGrouping, name='RULES_HISTORY_GROUPING_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('notification', sa.Enum(RuleNotification, name='RULES_HISTORY_NOTIFY_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('grouping', sa.Enum(RuleGrouping,
name='RULES_HISTORY_GROUPING_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('notification', sa.Enum(RuleNotification,
name='RULES_HISTORY_NOTIFY_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('stuck_at', sa.DateTime),
sa.Column('purge_replicas', sa.Boolean()),
sa.Column('ignore_availability', sa.Boolean()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2020 CERN
# Copyright 2015-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
# - Vincent Garonne <vincent.garonne@cern.ch>, 2015-2017
# - Martin Barisits <martin.barisits@cern.ch>, 2016
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019-2020
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2021

''' create collection_replicas_updates table '''

Expand Down Expand Up @@ -52,7 +53,9 @@ def upgrade():
sa.Column('id', GUID()),
sa.Column('scope', sa.String(25)),
sa.Column('name', sa.String(255)),
sa.Column('did_type', sa.Enum(DIDType, name='UPDATED_COL_REP_TYPE_CHK',
sa.Column('did_type', sa.Enum(DIDType,
name='UPDATED_COL_REP_TYPE_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('rse_id', GUID()),
sa.Column('created_at', sa.DateTime, default=datetime.datetime.utcnow),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2020 CERN
# Copyright 2015-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
# - Vincent Garonne <vincent.garonne@cern.ch>, 2015-2017
# - Martin Barisits <martin.barisits@cern.ch>, 2016
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019-2020
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2021

''' add notification column to rules '''

Expand All @@ -40,7 +41,9 @@ def upgrade():
schema = context.get_context().version_table_schema + '.' if context.get_context().version_table_schema else ''

if context.get_context().dialect.name in ['oracle', 'mysql']:
add_column('rules', sa.Column('notification', sa.Enum(RuleNotification, name='RULES_NOTIFICATION_CHK',
add_column('rules', sa.Column('notification', sa.Enum(RuleNotification,
name='RULES_NOTIFICATION_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj]),
default=RuleNotification.NO), schema=schema[:-1])
elif context.get_context().dialect.name == 'postgresql':
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# -*- coding: utf-8 -*-
# Copyright 2015-2020 CERN
# Copyright 2015-2021 CERN
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -17,6 +17,7 @@
# - Vincent Garonne <vincent.garonne@cern.ch>, 2015-2017
# - Martin Barisits <martin.barisits@cern.ch>, 2016-2019
# - Mario Lassnig <mario.lassnig@cern.ch>, 2019-2020
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2021

''' create collection replica table '''

Expand Down Expand Up @@ -45,11 +46,18 @@ def upgrade():
create_table('collection_replicas',
sa.Column('scope', sa.String(25)),
sa.Column('name', sa.String(255)),
sa.Column('did_type', sa.Enum(DIDType, name='COLLECTION_REPLICAS_TYPE_CHK', values_callable=lambda obj: [e.value for e in obj])),
sa.Column('did_type', sa.Enum(DIDType,
name='COLLECTION_REPLICAS_TYPE_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj])),
sa.Column('rse_id', GUID()),
sa.Column('bytes', sa.BigInteger),
sa.Column('length', sa.BigInteger),
sa.Column('state', sa.Enum(ReplicaState, name='COLLECTION_REPLICAS_STATE_CHK', values_callable=lambda obj: [e.value for e in obj]), default=ReplicaState.UNAVAILABLE),
sa.Column('state', sa.Enum(ReplicaState,
name='COLLECTION_REPLICAS_STATE_CHK',
create_constraint=True,
values_callable=lambda obj: [e.value for e in obj]),
default=ReplicaState.UNAVAILABLE),
sa.Column('accessed_at', sa.DateTime),
sa.Column('created_at', sa.DateTime, default=datetime.datetime.utcnow),
sa.Column('updated_at', sa.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow))
Expand Down

0 comments on commit 386a88f

Please sign in to comment.