Skip to content

Commit

Permalink
Merge pull request #194 from raphaelrpl/b-1.0
Browse files Browse the repository at this point in the history
🐛 Fix bug related collection tiles trigger (close #193)
  • Loading branch information
raphaelrpl authored May 12, 2023
2 parents 5b85d42 + 7691b8d commit 99c7359
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 8 deletions.
7 changes: 7 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
Changes
=======

Version 1.0.2 (2023-05-12)
--------------------------

- Fix trigger related collection tiles, ensure JSON concatenation `#193 <https://github.com/brazil-data-cube/bdc-catalog/issues/193>`_.
- Alter JSONB fields entities with ``server_default`` values in tables ``bdc.collections`` and ``bdc.items`` `#193 <https://github.com/brazil-data-cube/bdc-catalog/issues/193>`_.


Version 1.0.1 (2022-12-06)
--------------------------

Expand Down
65 changes: 65 additions & 0 deletions bdc_catalog/alembic/98ba50e6e9ab_fix_optional_jsonb_columns.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
"""fix optional jsonb columns
Revision ID: 98ba50e6e9ab
Revises: f3112636be24
Create Date: 2023-05-12 09:00:13.859502
"""
from alembic import op
import sqlalchemy as sa
from sqlalchemy.dialects import postgresql

# revision identifiers, used by Alembic.
revision = '98ba50e6e9ab'
down_revision = 'f3112636be24'
branch_labels = ()
depends_on = None


def upgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('collections', 'properties',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=True,
server_default='{}',
comment='Contains the properties offered by STAC collections',
schema='bdc')

op.alter_column('collections', 'summaries',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=True,
server_default='{}',
comment='Contains the STAC Collection summaries.',
schema='bdc')

op.alter_column('collections', 'item_assets',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=True,
server_default='{}',
comment='Contains the STAC Extension Item Assets.',
schema='bdc')

# ### end Alembic commands ###


def downgrade():
# ### commands auto generated by Alembic - please adjust! ###
op.alter_column('collections', 'properties',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=True,
comment='Contains the properties offered by STAC collections',
schema='bdc')

op.alter_column('collections', 'summaries',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=True,
comment='Contains the STAC Collection summaries.',
schema='bdc')

op.alter_column('collections', 'item_assets',
existing_type=postgresql.JSONB(astext_type=sa.Text()),
nullable=True,
comment='Contains the STAC Extension Item Assets.',
schema='bdc')

# ### end Alembic commands ###
9 changes: 6 additions & 3 deletions bdc_catalog/models/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ class Collection(BaseModel):
comment='Follow the JSONSchema @jsonschemas/collection-metadata.json')
keywords = Column('keywords', ARRAY(String))
properties = Column('properties', JSONB('bdc-catalog/collection-properties.json'),
comment='Contains the properties offered by STAC collections')
comment='Contains the properties offered by STAC collections',
default={}, server_default='{}')
summaries = Column('summaries', JSONB('bdc-catalog/collection-summaries.json'),
comment='Contains the STAC Collection summaries.')
comment='Contains the STAC Collection summaries.',
default={}, server_default='{}')
item_assets = Column('item_assets', JSONB('bdc-catalog/collection-item-assets.json'),
comment='Contains the STAC Extension Item Assets.')
comment='Contains the STAC Extension Item Assets.',
default={}, server_default='{}')
is_available = Column(Boolean(), nullable=False, default=False, server_default='False')
is_public = Column(Boolean(), nullable=False, default=True, server_default='true')
category = Column(enum_collection_category, nullable=False)
Expand Down
5 changes: 3 additions & 2 deletions bdc_catalog/models/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,8 @@ class Item(BaseModel):
cloud_cover = Column(Numeric)
assets = Column(JSONB('bdc-catalog/item-assets.json'), comment='Follow the JSONSchema @jsonschemas/item-assets.json')
metadata_ = Column('metadata', JSONB('bdc-catalog/item-metadata.json'),
comment='Follow the JSONSchema @jsonschemas/item-metadata.json')
comment='Follow the JSONSchema @jsonschemas/item-metadata.json',
default={}, server_default='{}')
provider_id = Column(ForeignKey(f'{BDC_CATALOG_SCHEMA}.providers.id', onupdate='CASCADE', ondelete='CASCADE'))
bbox = Column(Geometry(geometry_type='Polygon', srid=4326, spatial_index=False))
footprint = Column(Geometry(geometry_type='Polygon', srid=4326, spatial_index=False))
Expand Down Expand Up @@ -213,7 +214,7 @@ def add_asset(self, name: str, file: str, role: List[str], href: str, **kwargs):
mime_type = kwargs.get('mime_type')
if mime_type is None:
# Seek in band
if self.collection_id is None:
if self.collection is None and self.collection_id is None:
raise ValueError('Could not determine Mimetype when Item collection is None.')
collection = self.collection
mime_type = mimetypes.guess_type(os.path.basename(file))[0]
Expand Down
5 changes: 3 additions & 2 deletions bdc_catalog/triggers/collection_tiles.sql
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ RETURNS trigger AS $$
BEGIN
-- Once Item update/insert, calculate the min/max time and update in collections.
UPDATE bdc.collections
SET properties = COALESCE(properties, '{}'::JSONB) || tiles
SET properties = COALESCE(NULLIF(properties, 'null'), '{}'::JSONB) || COALESCE(NULLIF(tiles, 'null'), '{}'::JSONB)
FROM (
SELECT ('{"bdc:tiles": '||to_json(array_agg(DISTINCT bdc.tiles.name))||'}')::JSONB as tiles
FROM bdc.tiles, bdc.items
WHERE bdc.items.collection_id = NEW.collection_id
AND bdc.items.tile_id = bdc.tiles.id
) t
WHERE id = NEW.collection_id;
WHERE id = NEW.collection_id
AND grid_ref_sys_id is not null;

RETURN NEW;
END;
Expand Down
2 changes: 1 addition & 1 deletion bdc_catalog/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@
"""Version information for BDC-Catalog."""


__version__ = '1.0.1'
__version__ = '1.0.2'

0 comments on commit 99c7359

Please sign in to comment.