Skip to content
Snippets Groups Projects
Commit 06322641 authored by Dariusz Kędzierski's avatar Dariusz Kędzierski
Browse files

Remove FKs before initial sync and recreate after initial sync

parent e3cb8b90
No related branches found
No related tags found
4 merge requests!456Release candidate v1 24,!230Setup monitoring with pghero,!138Small typos fixed,!124Remove FKs before initial sync and recreate after initial sync
...@@ -162,10 +162,9 @@ class DbState: ...@@ -162,10 +162,9 @@ class DbState:
except sqlalchemy.exc.ProgrammingError as ex: except sqlalchemy.exc.ProgrammingError as ex:
log.warning("Ignoring ex: {}".format(ex)) log.warning("Ignoring ex: {}".format(ex))
# TODO: #111 from hive.db.schema import drop_fk, create_fk
#for key in cls._all_foreign_keys(): log.info("Dropping FKs")
# log.info("Drop fk %s", key.name) drop_fk(cls.db())
# key.drop(engine)
log.info("[INIT] Finish pre-initial sync hooks") log.info("[INIT] Finish pre-initial sync hooks")
...@@ -241,12 +240,13 @@ class DbState: ...@@ -241,12 +240,13 @@ class DbState:
update_active_starting_from_posts_on_block(last_imported_block, current_imported_block) 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() time_end = perf_counter()
log.info("[INIT] update_all_posts_active executed in %fs", time_end - time_start) 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 @staticmethod
def status(): def status():
......
...@@ -183,11 +183,11 @@ def build_metadata(): ...@@ -183,11 +183,11 @@ def build_metadata():
sa.PrimaryKeyConstraint('author_id', 'permlink_id', 'voter_id', name='hive_votes_pk'), sa.PrimaryKeyConstraint('author_id', 'permlink_id', 'voter_id', name='hive_votes_pk'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id']), sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_votes_fk1'),
sa.ForeignKeyConstraint(['voter_id'], ['hive_accounts.id']), sa.ForeignKeyConstraint(['voter_id'], ['hive_accounts.id'], name='hive_votes_fk2'),
sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id']), sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id'], name='hive_votes_fk3'),
sa.ForeignKeyConstraint(['permlink_id'], ['hive_permlink_data.id']), sa.ForeignKeyConstraint(['permlink_id'], ['hive_permlink_data.id'], name='hive_votes_fk4'),
sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num']), sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num'], name='hive_votes_fk5'),
sa.Index('hive_votes_post_id_idx', 'post_id'), sa.Index('hive_votes_post_id_idx', 'post_id'),
sa.Index('hive_votes_voter_id_idx', 'voter_id'), sa.Index('hive_votes_voter_id_idx', 'voter_id'),
...@@ -206,8 +206,8 @@ def build_metadata(): ...@@ -206,8 +206,8 @@ def build_metadata():
sa.Column('post_id', sa.Integer, nullable=False), sa.Column('post_id', sa.Integer, nullable=False),
sa.Column('tag_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.PrimaryKeyConstraint('post_id', 'tag_id', name='hive_post_tags_pk1'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id']), sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_post_tags_fk1'),
sa.ForeignKeyConstraint(['tag_id'], ['hive_tag_data.id']), 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_post_id_idx', 'post_id'),
sa.Index('hive_post_tags_tag_id_idx', 'tag_id') sa.Index('hive_post_tags_tag_id_idx', 'tag_id')
) )
...@@ -377,6 +377,24 @@ def teardown(db): ...@@ -377,6 +377,24 @@ def teardown(db):
"""Drop all tables""" """Drop all tables"""
build_metadata().drop_all(db.engine()) 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): def setup(db):
"""Creates all tables and seed data""" """Creates all tables and seed data"""
# initialize schema # initialize schema
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment