Skip to content
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

Updating migration file for engagement metadata #2368

Merged
merged 5 commits into from
Jan 30, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/met-api-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ jobs:
KEYCLOAK_TEST_ADMIN_SECRET: "2222222222"
KEYCLOAK_TEST_AUTH_AUDIENCE: "met-web"
KEYCLOAK_TEST_AUTH_CLIENT_SECRET: "1111111111"
KEYCLOAK_TEST_BASE_URL: "http://localhost:8081"
KEYCLOAK_TEST_BASE_URL: "http://localhost:8081/auth"
KEYCLOAK_TEST_REALMNAME: "demo"
USE_TEST_KEYCLOAK_DOCKER: "YES"
SQLALCHEMY_DATABASE_URI: "postgresql://postgres:postgres@localhost:5432/postgres"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ def upgrade():
sa.Column('created_date', sa.DateTime(), nullable=False),
sa.Column('updated_date', sa.DateTime(), nullable=True),
sa.Column('engagement_id', sa.Integer(), nullable=False),
sa.Column('project_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True),
sa.Column('created_by', sa.String(length=50), nullable=True),
sa.Column('updated_by', sa.String(length=50), nullable=True),
sa.ForeignKeyConstraint(['engagement_id'], ['engagement.id'], ondelete='CASCADE'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,38 @@ def upgrade():
sa.PrimaryKeyConstraint('id', name='pk_engagement_metadata_taxa'),
sa.UniqueConstraint('id', name='uq_engagement_metadata_taxa_id')
)
# remove old primary key constraint from engagement_metadata.engagement_id
# remove old data from engagement_metadata table
op.execute('DELETE FROM engagement_metadata')

# Drop the existing primary key constraint
op.drop_constraint('engagement_metadata_pkey', 'engagement_metadata', type_='primary')

# Create a new index on engagement_metadata_taxa
op.create_index(op.f('ix_engagement_metadata_taxa_position'), 'engagement_metadata_taxa', ['position'], unique=False)

# Add new columns to engagement_metadata
op.add_column('engagement_metadata', sa.Column('id', sa.Integer(), nullable=False))
# add new primary key constraint on engagement_metadata.id
op.add_column('engagement_metadata', sa.Column('taxon_id', sa.Integer(), nullable=False))
op.add_column('engagement_metadata', sa.Column('value', sa.Text(), nullable=False))

# Create a new primary key constraint on the 'id' column
op.create_primary_key('pk_engagement_metadata', 'engagement_metadata', ['id'])
# add autoincrement to engagement_metadata.id by creating a sequence

# Create a new sequence and set it as the default for the 'id' column
op.execute('CREATE SEQUENCE engagement_metadata_id_seq START 1')
op.execute('ALTER TABLE engagement_metadata ALTER COLUMN id SET DEFAULT nextval(\'engagement_metadata_id_seq\')')
op.execute('ALTER SEQUENCE engagement_metadata_id_seq OWNED BY engagement_metadata.id')
# remove not-null constraint from engagement_metadata.engagement_id

# Remove the not-null constraint from 'engagement_id'
op.alter_column('engagement_metadata', 'engagement_id', existing_type=sa.INTEGER(), nullable=True)

op.add_column('engagement_metadata', sa.Column('taxon_id', sa.Integer(), nullable=False))
op.add_column('engagement_metadata', sa.Column('value', sa.Text(), nullable=False))
# Create a foreign key constraint
op.create_foreign_key('fk_engagement_meta_taxon', 'engagement_metadata', 'engagement_metadata_taxa', ['taxon_id'], ['id'], ondelete='CASCADE')

# Remove the 'project_tracking_id' and 'project_metadata' column
op.drop_column('engagement_metadata', 'project_tracking_id')
op.drop_column('engagement_metadata', 'project_metadata')

# add default taxa for default tenant
default_short_name = current_app.config.get('DEFAULT_TENANT_SHORT_NAME')
tenant_id = TenantModel.find_by_short_name(default_short_name).id
Expand Down Expand Up @@ -133,16 +148,17 @@ def upgrade():


def downgrade():
op.add_column('engagement_metadata', sa.Column('project_metadata', postgresql.JSONB(astext_type=sa.Text()), nullable=True))
op.add_column('engagement_metadata', sa.Column('project_tracking_id', sa.VARCHAR(length=100), autoincrement=False, nullable=True))
op.alter_column('engagement_metadata', 'engagement_id', existing_type=sa.INTEGER(), nullable=False)
op.drop_constraint('fk_engagement_meta_taxon', 'engagement_metadata', type_='foreignkey')
op.drop_column('engagement_metadata', 'value')
op.drop_column('engagement_metadata', 'taxon_id')
# remove primary key constraint from engagement_metadata.id
op.drop_constraint('pk_engagement_metadata', 'engagement_metadata', type_='primary')
op.drop_column('engagement_metadata', 'value')
op.drop_column('engagement_metadata', 'taxon_id')
op.drop_column('engagement_metadata', 'id')
op.drop_index(op.f('ix_engagement_metadata_taxa_position'), table_name='engagement_metadata_taxa')
# add primary key constraint to engagement_metadata.engagement_id
op.create_primary_key('pk_engagement_metadata', 'engagement_metadata', ['engagement_id'])
op.create_primary_key('engagement_metadata_pkey', 'engagement_metadata', ['engagement_id'])
op.drop_table('engagement_metadata_taxa')
# ### end Alembic commands ###
2 changes: 1 addition & 1 deletion met-api/sample.env
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ DATABASE_TEST_HOST=
DATABASE_TEST_PORT=

# A keycloak server is started automatically by Pytest; there is no need to start your own instance.
KEYCLOAK_TEST_BASE_URL="http://localhost:8081"
KEYCLOAK_TEST_BASE_URL="http://localhost:8081/auth"

# Docker database settings
# If unset, uses the same settings as the main database
Expand Down
4 changes: 2 additions & 2 deletions met-api/src/met_api/resources/metadata_taxon.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
from met_api.utils.util import allowedorigins, cors_preflight


VIEW_TAXA_ROLES = [Role.VIEW_TENANT.value, Role.CREATE_TENANT.value]
MODIFY_TAXA_ROLES = [Role.CREATE_TENANT.value]
VIEW_TAXA_ROLES = [Role.VIEW_ENGAGEMENT.value, Role.CREATE_ENGAGEMENT.value]
MODIFY_TAXA_ROLES = [Role.EDIT_ENGAGEMENT.value]
TAXON_NOT_FOUND_MSG = 'Metadata taxon was not found'

API = Namespace('metadata_taxa', description="Endpoints for managing the taxa "
Expand Down
4 changes: 1 addition & 3 deletions met-api/tests/utilities/factory_scenarios.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,7 @@ class TestJwtClaims(dict, Enum):
'export_proponent_comment_sheet',
'export_internal_comment_sheet',
'export_cac_form_to_sheet',
'view_members',
'create_tenant',
'view_tenant',
'view_members'
]
}
team_member_role = {
Expand Down
Loading