From 06322641b5edccf5ca01477e16564db49b25f756 Mon Sep 17 00:00:00 2001 From: Dariusz Kedzierski <dkedzierski@syncad.com> Date: Tue, 8 Sep 2020 12:54:05 +0200 Subject: [PATCH] Remove FKs before initial sync and recreate after initial sync --- hive/db/db_state.py | 16 ++++++++-------- hive/db/schema.py | 32 +++++++++++++++++++++++++------- 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/hive/db/db_state.py b/hive/db/db_state.py index 405b944e2..3c97d5031 100644 --- a/hive/db/db_state.py +++ b/hive/db/db_state.py @@ -162,10 +162,9 @@ class DbState: except sqlalchemy.exc.ProgrammingError as ex: log.warning("Ignoring ex: {}".format(ex)) - # TODO: #111 - #for key in cls._all_foreign_keys(): - # log.info("Drop fk %s", key.name) - # key.drop(engine) + from hive.db.schema import drop_fk, create_fk + log.info("Dropping FKs") + drop_fk(cls.db()) log.info("[INIT] Finish pre-initial sync hooks") @@ -241,12 +240,13 @@ class DbState: update_active_starting_from_posts_on_block(last_imported_block, current_imported_block) + log.info("Recreating FKs") + from hive.db.schema import create_fk + create_fk(cls.db()) + time_end = perf_counter() log.info("[INIT] update_all_posts_active executed in %fs", time_end - time_start) - # TODO: #111 - #for key in cls._all_foreign_keys(): - # log.info("Create fk %s", key.name) - # key.create(engine) + @staticmethod def status(): diff --git a/hive/db/schema.py b/hive/db/schema.py index 400748ddf..7d630aaf1 100644 --- a/hive/db/schema.py +++ b/hive/db/schema.py @@ -183,11 +183,11 @@ def build_metadata(): sa.PrimaryKeyConstraint('author_id', 'permlink_id', 'voter_id', name='hive_votes_pk'), - sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id']), - sa.ForeignKeyConstraint(['voter_id'], ['hive_accounts.id']), - sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id']), - sa.ForeignKeyConstraint(['permlink_id'], ['hive_permlink_data.id']), - sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num']), + sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_votes_fk1'), + sa.ForeignKeyConstraint(['voter_id'], ['hive_accounts.id'], name='hive_votes_fk2'), + sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id'], name='hive_votes_fk3'), + sa.ForeignKeyConstraint(['permlink_id'], ['hive_permlink_data.id'], name='hive_votes_fk4'), + sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num'], name='hive_votes_fk5'), sa.Index('hive_votes_post_id_idx', 'post_id'), sa.Index('hive_votes_voter_id_idx', 'voter_id'), @@ -206,8 +206,8 @@ def build_metadata(): sa.Column('post_id', sa.Integer, nullable=False), sa.Column('tag_id', sa.Integer, nullable=False), sa.PrimaryKeyConstraint('post_id', 'tag_id', name='hive_post_tags_pk1'), - sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id']), - sa.ForeignKeyConstraint(['tag_id'], ['hive_tag_data.id']), + sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_post_tags_fk1'), + sa.ForeignKeyConstraint(['tag_id'], ['hive_tag_data.id'], name='hive_post_tags_fk2'), sa.Index('hive_post_tags_post_id_idx', 'post_id'), sa.Index('hive_post_tags_tag_id_idx', 'tag_id') ) @@ -377,6 +377,24 @@ def teardown(db): """Drop all tables""" build_metadata().drop_all(db.engine()) +def drop_fk(db): + db.query_no_return("START TRANSACTION") + for table in build_metadata().sorted_tables: + for fk in table.foreign_keys: + sql = """ALTER TABLE {} DROP CONSTRAINT {}""".format(table.name, fk.name) + db.query_no_return(sql) + db.query_no_return("COMMIT") + +def create_fk(db): + from sqlalchemy.schema import AddConstraint + from sqlalchemy import text + connection = db.engine().connect() + connection.execute(text("START TRANSACTION")) + for table in build_metadata().sorted_tables: + for fk in table.foreign_keys: + connection.execute(AddConstraint(fk.constraint)) + connection.execute(text("COMMIT")) + def setup(db): """Creates all tables and seed data""" # initialize schema -- GitLab