From 6cc2a91632fff044d08f2529d705b5298104e41e Mon Sep 17 00:00:00 2001 From: Shyam Sunder Date: Sat, 7 Mar 2020 21:39:22 -0500 Subject: [PATCH] server/image_search: add migrations for elasticsearch removal --- server/szurubooru/facade.py | 13 +++++--- server/szurubooru/func/posts.py | 9 ++++-- ...6ea6584b8_generate_post_signature_table.py | 30 +++++++++++++++++++ 3 files changed, 46 insertions(+), 6 deletions(-) create mode 100644 server/szurubooru/migrations/versions/52d6ea6584b8_generate_post_signature_table.py diff --git a/server/szurubooru/facade.py b/server/szurubooru/facade.py index 3220b56e..ee069867 100644 --- a/server/szurubooru/facade.py +++ b/server/szurubooru/facade.py @@ -8,7 +8,8 @@ import coloredlogs import sqlalchemy as sa import sqlalchemy.orm.exc from szurubooru import config, db, errors, rest -from szurubooru.func import file_uploads +from szurubooru.func.posts import update_all_post_signatures +from szurubooru.func.file_uploads import purge_old_uploads # pylint: disable=unused-import from szurubooru import api, middleware @@ -106,10 +107,10 @@ def validate_config() -> None: 'From address must be set to use mail-based password reset') -def purge_old_uploads() -> None: +def purge_old_uploads_daemon() -> None: while True: try: - file_uploads.purge_old_uploads() + purge_old_uploads() except Exception as ex: logging.exception(ex) time.sleep(60 * 5) @@ -125,10 +126,14 @@ def create_app() -> Callable[[Any, Any], Any]: if config.config['show_sql']: logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) - purge_thread = threading.Thread(target=purge_old_uploads) + purge_thread = threading.Thread(target=purge_old_uploads_daemon) purge_thread.daemon = True purge_thread.start() + hashing_thread = threading.Thread(target=update_all_post_signatures) + hashing_thread.daemon = False + hashing_thread.start() + db.session.commit() rest.errors.handle(errors.AuthError, _on_auth_error) diff --git a/server/szurubooru/func/posts.py b/server/szurubooru/func/posts.py index 7b41a268..366b1315 100644 --- a/server/szurubooru/func/posts.py +++ b/server/szurubooru/func/posts.py @@ -513,8 +513,13 @@ def update_all_post_signatures() -> None: .order_by(model.Post.post_id.asc()) .all()) for post in posts_to_hash: - logger.info('Generating hash info for %d', post.post_id) - generate_post_signature(post, files.get(get_post_content_path(post))) + try: + generate_post_signature( + post, files.get(get_post_content_path(post))) + db.session.commit() + logger.info('Hashed Post %d', post.post_id) + except Exception as ex: + logger.exception(ex) def update_post_content(post: model.Post, content: Optional[bytes]) -> None: diff --git a/server/szurubooru/migrations/versions/52d6ea6584b8_generate_post_signature_table.py b/server/szurubooru/migrations/versions/52d6ea6584b8_generate_post_signature_table.py new file mode 100644 index 00000000..5a865739 --- /dev/null +++ b/server/szurubooru/migrations/versions/52d6ea6584b8_generate_post_signature_table.py @@ -0,0 +1,30 @@ +''' +Generate post signature table + +Revision ID: 52d6ea6584b8 +Created at: 2020-03-07 17:03:40.193512 +''' + +import sqlalchemy as sa +from alembic import op + + +revision = '52d6ea6584b8' +down_revision = '3c1f0316fa7f' +branch_labels = None +depends_on = None + + +def upgrade(): + ArrayType = sa.dialects.postgresql.ARRAY(sa.Integer, dimensions=1) + op.create_table( + 'post_signature', + sa.Column('post_id', sa.Integer(), nullable=False), + sa.Column('signature', sa.LargeBinary(), nullable=False), + sa.Column('words', ArrayType, nullable=False), + sa.ForeignKeyConstraint(['post_id'], ['post.id']), + sa.PrimaryKeyConstraint('post_id')) + + +def downgrade(): + op.drop_table('post_signature')