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
2 changes: 1 addition & 1 deletion src/flickrhistory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,4 @@
except ImportError:
pass

__version__ = "0.3.0.dev0"
__version__ = "0.3.0.dev1"
2 changes: 1 addition & 1 deletion src/flickrhistory/database/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"UserSaver",
]

from .engine import Session
from .models import License, Photo, User
from .photo_saver import PhotoSaver
from .session import Session
from .user_saver import UserSaver
17 changes: 9 additions & 8 deletions src/flickrhistory/database/databaseschemaupdater.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

import sqlalchemy

from .engine import engine
from ..config import Config


# for now, schema updates are SQL only and work on PostgreSQL, only.
Expand Down Expand Up @@ -48,7 +48,9 @@ class DatabaseSchemaUpdater:
def __init__(self):
"""Update the database schema if necessary."""
# Try to create database table for schema version
with engine.begin() as connection:
with Config() as config:
self.engine = sqlalchemy.create_engine(config["database_connection_string"])
with self.engine.begin() as connection:
connection.execute(
sqlalchemy.text(
"""
Expand All @@ -65,7 +67,7 @@ def __init__(self):
@property
def installed_version(self):
"""Return current version."""
with engine.connect() as connection:
with self.engine.connect() as connection:
installed_version = connection.execute(
sqlalchemy.text(
"""
Expand All @@ -92,18 +94,17 @@ def update_to_latest(self):
file=sys.stderr,
flush=True, # so that we don’t seem without work
)
with engine.begin() as connection:
with self.engine.connect() as connection:
next_version = self.installed_version + 1
connection.execute(sqlalchemy.text(SCHEMA_UPDATES[next_version]))
self.set_schema_version(next_version)
installed_version = self.installed_version

@classmethod
def set_schema_version(cls, version):
def set_schema_version(self, version):
"""Set the schema version (without running update scripts)."""
if version == cls.LATEST:
if version == self.LATEST:
version = max(SCHEMA_UPDATES.keys())
with engine.begin() as connection:
with self.engine.begin() as connection:
connection.execute(
sqlalchemy.text(
"""
Expand Down
42 changes: 0 additions & 42 deletions src/flickrhistory/database/engine.py

This file was deleted.

12 changes: 0 additions & 12 deletions src/flickrhistory/database/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,7 @@
]


import sqlalchemy

from ..engine import engine
from .base import Base
from ..databaseschemaupdater import DatabaseSchemaUpdater
from .license import License
from .photo import Photo
from .tag import Tag
from .user import User


if sqlalchemy.inspect(engine).has_table(Photo.__table__.name): # data exist
DatabaseSchemaUpdater().update_to_latest()
else:
Base.metadata.create_all(engine)
DatabaseSchemaUpdater().set_schema_version(DatabaseSchemaUpdater.LATEST)
2 changes: 1 addition & 1 deletion src/flickrhistory/database/photo_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import datetime

from .models import License, Photo, Tag
from .engine import Session
from .session import Session
from .user_saver import UserSaver


Expand Down
57 changes: 57 additions & 0 deletions src/flickrhistory/database/session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-


"""An SQLAlchemy sessionmaker."""


__all__ = ["Session"]


import multiprocessing

import sqlalchemy
import sqlalchemy.orm

from ..config import Config
from .databaseschemaupdater import DatabaseSchemaUpdater
from .models import Photo
from .models.base import Base


POOL_SIZE = multiprocessing.cpu_count() * 10


class Session:
"""An sqlachemy.Session."""

_sessionmaker = None

def __new__(cls, *args, **kwargs):
"""Return an sqlachemy.Session."""
if cls._sessionmaker is None:
with Config() as config:
engine = sqlalchemy.create_engine(
config["database_connection_string"],
pool_size=POOL_SIZE,
max_overflow=POOL_SIZE,
)

with engine.begin() as connection:
connection.execute(
sqlalchemy.text(
"""
CREATE EXTENSION IF NOT EXISTS
postgis;
"""
)
)

if sqlalchemy.inspect(engine).has_table(Photo.__table__.name): # data exist
DatabaseSchemaUpdater().update_to_latest()
else:
Base.metadata.create_all(engine)
DatabaseSchemaUpdater().set_schema_version(DatabaseSchemaUpdater.LATEST)

cls._sessionmaker = sqlalchemy.orm.sessionmaker(engine, autoflush=False)
return cls._sessionmaker()
2 changes: 1 addition & 1 deletion src/flickrhistory/database/user_saver.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import datetime

from .models import User
from .engine import Session
from .session import Session


__all__ = ["UserSaver"]
Expand Down