diff --git a/.bumpversion.cfg b/.bumpversion.cfg index 69e8b6b3..4afb4fd6 100644 --- a/.bumpversion.cfg +++ b/.bumpversion.cfg @@ -1,5 +1,5 @@ [bumpversion] -current_version = 0.51.7 +current_version = 0.51.8 parse = (?P\d+)\.(?P\d+)\.(?P\d+) serialize = {major}.{minor}.{patch} diff --git a/osxphotos/_version.py b/osxphotos/_version.py index 959391f8..67d3b2c4 100644 --- a/osxphotos/_version.py +++ b/osxphotos/_version.py @@ -1,3 +1,3 @@ """ version info """ -__version__ = "0.51.7" +__version__ = "0.51.8" diff --git a/osxphotos/export_db.py b/osxphotos/export_db.py index 7230346a..5c22b970 100644 --- a/osxphotos/export_db.py +++ b/osxphotos/export_db.py @@ -740,10 +740,16 @@ class ExportDB: def _migrate_7_0_to_7_1(self, conn): c = conn.cursor() - c.execute("""ALTER TABLE export_data ADD COLUMN timestamp DATETIME;""") + # timestamp column should not exist but this prevents error if migration is run on an already migrated database + # reference #794 + results = c.execute( + "SELECT COUNT(*) FROM pragma_table_info('export_data') WHERE name='timestamp';" + ).fetchone() + if results[0] == 0: + c.execute("""ALTER TABLE export_data ADD COLUMN timestamp DATETIME;""") c.execute( """ - CREATE TRIGGER insert_timestamp_trigger + CREATE TRIGGER IF NOT EXISTS insert_timestamp_trigger AFTER INSERT ON export_data BEGIN UPDATE export_data SET timestamp = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW') WHERE id = NEW.id; @@ -752,7 +758,7 @@ class ExportDB: ) c.execute( """ - CREATE TRIGGER update_timestamp_trigger + CREATE TRIGGER IF NOT EXISTS update_timestamp_trigger AFTER UPDATE On export_data BEGIN UPDATE export_data SET timestamp = STRFTIME('%Y-%m-%d %H:%M:%f', 'NOW') WHERE id = NEW.id; diff --git a/tests/test_export_db.py b/tests/test_export_db.py index 698829f8..5afed657 100644 --- a/tests/test_export_db.py +++ b/tests/test_export_db.py @@ -4,6 +4,7 @@ import os import pathlib import sqlite3 import tempfile +import shutil import pytest @@ -26,7 +27,7 @@ DIGEST_DATA = "FIZZ" EXIF_DATA2 = """[{"_CreatedBy": "osxphotos, https://github.com/RhetTbull/osxphotos", "XMP:Title": "St. James's Park", "XMP:TagsList": ["London 2018", "St. James's Park", "England", "United Kingdom", "UK", "London"], "IPTC:Keywords": ["London 2018", "St. James's Park", "England", "United Kingdom", "UK", "London"], "XMP:Subject": ["London 2018", "St. James's Park", "England", "United Kingdom", "UK", "London"], "EXIF:GPSLatitude": "51 deg 30' 12.86\" N", "EXIF:GPSLongitude": "0 deg 7' 54.50\" W", "Composite:GPSPosition": "51 deg 30' 12.86\" N, 0 deg 7' 54.50\" W", "EXIF:GPSLatitudeRef": "North", "EXIF:GPSLongitudeRef": "West", "EXIF:DateTimeOriginal": "2018:10:13 09:18:12", "EXIF:OffsetTimeOriginal": "-04:00", "EXIF:ModifyDate": "2019:12:08 14:06:44"}]""" INFO_DATA2 = """{"uuid": "F2BB3F98-90F0-4E4C-A09B-25C6822A4529", "filename": "F2BB3F98-90F0-4E4C-A09B-25C6822A4529.jpeg", "original_filename": "IMG_8440.JPG", "date": "2019-06-11T11:42:06.711805-07:00", "description": null, "title": null, "keywords": [], "labels": ["Sky", "Cloudy", "Fence", "Land", "Outdoor", "Park", "Amusement Park", "Roller Coaster"], "albums": [], "folders": {}, "persons": [], "path": "/Volumes/MacBook Catalina - Data/Users/rhet/Pictures/Photos Library.photoslibrary/originals/F/F2BB3F98-90F0-4E4C-A09B-25C6822A4529.jpeg", "ismissing": false, "hasadjustments": false, "external_edit": false, "favorite": false, "hidden": false, "latitude": 33.81558666666667, "longitude": -117.99298, "path_edited": null, "shared": false, "isphoto": true, "ismovie": false, "uti": "public.jpeg", "burst": false, "live_photo": false, "path_live_photo": null, "iscloudasset": true, "incloud": true, "date_modified": "2019-10-14T00:51:47.141950-07:00", "portrait": false, "screenshot": false, "slow_mo": false, "time_lapse": false, "hdr": false, "selfie": false, "panorama": false, "has_raw": false, "uti_raw": null, "path_raw": null, "place": {"name": "Adventure City, Stanton, California, United States", "names": {"field0": [], "country": ["United States"], "state_province": ["California"], "sub_administrative_area": ["Orange"], "city": ["Stanton", "Anaheim", "Anaheim"], "field5": [], "additional_city_info": ["West Anaheim"], "ocean": [], "area_of_interest": ["Adventure City", "Adventure City"], "inland_water": [], "field10": [], "region": [], "sub_throughfare": [], "field13": [], "postal_code": [], "field15": [], "field16": [], "street_address": [], "body_of_water": []}, "country_code": "US", "ishome": false, "address_str": "Adventure City, 1240 S Beach Blvd, Anaheim, CA 92804, United States", "address": {"street": "1240 S Beach Blvd", "sub_locality": "West Anaheim", "city": "Stanton", "sub_administrative_area": "Orange", "state_province": "CA", "postal_code": "92804", "country": "United States", "iso_country_code": "US"}}, "exif": {"flash_fired": false, "iso": 25, "metering_mode": 5, "sample_rate": null, "track_format": null, "white_balance": 0, "aperture": 2.2, "bit_rate": null, "duration": null, "exposure_bias": 0.0, "focal_length": 4.15, "fps": null, "latitude": null, "longitude": null, "shutter_speed": 0.0004940711462450593, "camera_make": "Apple", "camera_model": "iPhone 6s", "codec": null, "lens_model": "iPhone 6s back camera 4.15mm f/2.2"}}""" DIGEST_DATA2 = "BUZZ" -DATABASE_VERSION1 = "tests/export_db_version1.db" +EXPORT_DATABASE_V1 = "tests/export_db_version1.db" def test_export_db(): @@ -439,3 +440,29 @@ def test_get_export_db_version(): osxphotos_ver, export_db_ver = export_db_get_version(dbname) assert osxphotos_ver == __version__ assert export_db_ver == OSXPHOTOS_EXPORTDB_VERSION + + +def test_export_db_migration(tmp_path): + """Test export_db migration""" + # create export db with version 1 + test_db = tmp_path / "osxphotos_export.db" + shutil.copyfile(EXPORT_DATABASE_V1, test_db) + export_db = ExportDB(test_db, tmp_path) + assert export_db.was_upgraded + assert export_db.version == OSXPHOTOS_EXPORTDB_VERSION + + # now open again, should not be upgraded + export_db = ExportDB(test_db, tmp_path) + assert not export_db.was_upgraded + assert export_db.version == OSXPHOTOS_EXPORTDB_VERSION + + # now let's mess with the version to force a migration, see #794 + conn = sqlite3.connect(test_db) + c = conn.cursor() + c.execute("DELETE FROM version;") + c.execute("INSERT INTO version VALUES (1, '0.34.3', '1.0');") + conn.commit() + conn.close() + export_db = ExportDB(test_db, tmp_path) + assert export_db.was_upgraded + assert export_db.version == OSXPHOTOS_EXPORTDB_VERSION