-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #186 from VForWaTer/location_nullable
entries.location nullable + db revision
- Loading branch information
Showing
7 changed files
with
115 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = '0.6.0' | ||
__version__ = '0.6.1' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
""" | ||
Metacatalog database revision | ||
----------------------------- | ||
date: 2022-10-05T14:41:32.055433 | ||
revision #11 | ||
Make column entries.location nullable (raster data do not have a POINT location). | ||
Attention, the downgrade sets everywhere where entries.location == NULL to | ||
(POINT 0 0), which may not correspond to the state of the database before upgrading, | ||
so the revision upgrade cannot be undone. | ||
""" | ||
from sqlalchemy.orm import Session | ||
from metacatalog import api, models | ||
|
||
UPGRADE_SQL = """ | ||
-- entries.location nullable | ||
ALTER TABLE entries ALTER COLUMN location DROP NOT NULL; | ||
COMMIT; | ||
""" | ||
|
||
DOWNGRADE_SQL = """ | ||
-- replace eventually existing NULL values with (POINT 0 0) | ||
UPDATE entries SET location = 'SRID=4326; POINT (0 0)' WHERE location IS NULL; | ||
COMMIT; | ||
-- entries.location not nullable | ||
ALTER TABLE entries ALTER COLUMN location SET NOT NULL; | ||
COMMIT; | ||
""" | ||
|
||
# define the upgrade function | ||
def upgrade(session: Session): | ||
print('run update') | ||
# make entries.location nullable | ||
with session.bind.connect() as con: | ||
con.execute(UPGRADE_SQL) | ||
|
||
|
||
# define the downgrade function | ||
def downgrade(session: Session): | ||
print('run downgrade') | ||
# make entries.location not nullable | ||
with session.bind.connect() as con: | ||
con.execute(DOWNGRADE_SQL) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--CREATE OR REPLACE VIEW locations_realdata AS | ||
select | ||
st_asewkt(t.point_location), | ||
t.*, | ||
st_area(t.geom::geography) as "area_sqm" | ||
from | ||
(SELECT | ||
entries.id, | ||
case when entries.location is not null then | ||
entries.location | ||
else | ||
case when spatial_scales.extent is not null then | ||
st_centroid(spatial_scales.extent) | ||
else | ||
case when entries.geom is not null then | ||
st_centroid(entries.geom) | ||
else | ||
null::geometry | ||
end | ||
end | ||
end | ||
as point_location | ||
, | ||
case when entries.geom is not null then | ||
entries.geom | ||
else | ||
case when spatial_scales.extent is not null then | ||
spatial_scales.extent | ||
else | ||
entries.location | ||
end | ||
end | ||
as "geom" | ||
FROM entries | ||
LEFT JOIN datasources ON entries.datasource_id = datasources.id | ||
LEFT JOIN spatial_scales ON spatial_scales.id = datasources.spatial_scale_id | ||
) as t | ||
|
||
order by t.id asc |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters