Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
5 changes: 5 additions & 0 deletions src/flickrhistory/basicflickrhistorydownloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from .cache import Cache
from .cacheupdaterthread import CacheUpdaterThread
from .config import Config
from .database import Session
from .licensedownloader import LicenseDownloader
from .photodownloaderthread import PhotoDownloaderThread
from .photoupdaterthread import PhotoUpdaterThread
Expand Down Expand Up @@ -56,6 +57,10 @@ def __init__(self):

def download(self):
"""Download all georeferenced flickr posts."""
# create a session to initialise the database
_ = Session()

# download an updated list of possible licenses
LicenseDownloader(self._api_key_manager).update_licenses()

for gap in self.gaps_in_download_history:
Expand Down
68 changes: 51 additions & 17 deletions src/flickrhistory/database/databaseschemaupdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,55 @@
SCHEMA_UPDATES = {
# 0 -> 1
1: """
ALTER TABLE
photos
ADD COLUMN IF NOT EXISTS
geo_accuracy SMALLINT;

CREATE TABLE IF NOT EXISTS
licenses (
id INTEGER,
name TEXT,
url TEXT
);

ALTER TABLE
photos
ADD COLUMN IF NOT EXISTS
license INTEGER REFERENCES licenses(id);
CREATE TABLE licenses (
id integer NOT NULL,
name text,
url text
);

CREATE SEQUENCE licenses_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

ALTER SEQUENCE licenses_id_seq OWNED BY licenses.id;

ALTER TABLE ONLY photos ADD COLUMN geo_accuracy SMALLINT, ADD COLUMN license_id INTEGER;

CREATE TABLE tag_photo_associations (
tag_tag text NOT NULL,
photo_id bigint NOT NULL
);

CREATE TABLE tags (
tag text NOT NULL
);

ALTER TABLE ONLY licenses ALTER COLUMN id SET DEFAULT nextval('licenses_id_seq'::regclass);

ALTER TABLE ONLY licenses
ADD CONSTRAINT licenses_pkey PRIMARY KEY (id);

ALTER TABLE ONLY tag_photo_associations
ADD CONSTRAINT tag_photo_associations_pkey PRIMARY KEY (tag_tag, photo_id);

ALTER TABLE ONLY tags
ADD CONSTRAINT tags_pkey PRIMARY KEY (tag);

CREATE INDEX ix_photos_license_id ON photos USING btree (license_id);

ALTER TABLE ONLY photos RENAME CONSTRAINT "FlickrUser" TO "User";

ALTER TABLE ONLY photos ADD CONSTRAINT photos_license_id_fkey FOREIGN KEY (license_id) REFERENCES licenses(id);

ALTER TABLE ONLY tag_photo_associations
ADD CONSTRAINT tag_photo_associations_photo_id_fkey FOREIGN KEY (photo_id) REFERENCES photos(id);

ALTER TABLE ONLY tag_photo_associations
ADD CONSTRAINT tag_photo_associations_tag_tag_fkey FOREIGN KEY (tag_tag) REFERENCES tags(tag);
""",
}

Expand Down Expand Up @@ -94,9 +127,10 @@ def update_to_latest(self):
file=sys.stderr,
flush=True, # so that we don’t seem without work
)
with self.engine.connect() as connection:
with self.engine.begin() as connection:
next_version = self.installed_version + 1
connection.execute(sqlalchemy.text(SCHEMA_UPDATES[next_version]))
connection.commit()
self.set_schema_version(next_version)
installed_version = self.installed_version

Expand Down
1 change: 1 addition & 0 deletions src/flickrhistory/database/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ def __new__(cls, *args, **kwargs):

if sqlalchemy.inspect(engine).has_table(Photo.__table__.name): # data exist
DatabaseSchemaUpdater().update_to_latest()
Base.metadata.create_all(engine, checkfirst=True)
else:
Base.metadata.create_all(engine)
DatabaseSchemaUpdater().set_schema_version(DatabaseSchemaUpdater.LATEST)
Expand Down