following almost done

This commit is contained in:
tonyrewin 2022-09-18 21:10:57 +03:00
parent 1122fe580b
commit 40ea813399

View File

@ -35,6 +35,34 @@ def get_shout_slug(entry):
return slug return slug
async def create_shout(shout_dict, userslug):
s = Shout.create(**shout_dict)
with local_session() as session:
topics = session.query(ShoutTopic).where(ShoutTopic.shout == s.slug).all()
for tpc in topics:
tf = session.query(
TopicFollower
).where(
TopicFollower.follower == userslug
).filter(
TopicFollower.topic == tpc.slug
).first()
if not tf:
tf = TopicFollower.create(topic=tpc.slug, follower=userslug, auto=True)
session.add(tf)
await TopicStorage.update_topic(tpc.slug)
srf = session.query(ShoutReactionsFollower).where(
ShoutReactionsFollower.shout == s.slug
).filter(
ShoutReactionsFollower.follower == userslug
).first()
if not srf:
srf = ShoutReactionsFollower.create(shout=s.slug, follower=userslug, auto=True)
session.add(srf)
session.commit()
async def migrate(entry, storage): async def migrate(entry, storage):
# init, set title and layout # init, set title and layout
r = { r = {
@ -164,45 +192,35 @@ async def migrate(entry, storage):
raise Exception("could not get a user") raise Exception("could not get a user")
shout_dict["authors"] = [user, ] shout_dict["authors"] = [user, ]
try: try:
s = Shout.create(**shout_dict) await create_shout(shout_dict, userslug)
with local_session() as session:
topics = session.query(ShoutTopic).where(ShoutTopic.shout == s.slug).all()
for tpc in topics:
tf = session.query(TopicFollower).where(TopicFollower.follower ==
userslug).filter(TopicFollower.topic ==
tpc.slug).first()
if not tf:
tf = TopicFollower.create(topic=tpc.slug, follower=userslug, auto=True)
session.add(tf)
await TopicStorage.update_topic(tpc.slug)
srf = session.query(ShoutReactionsFollower).where(
ShoutReactionsFollower.shout == s.slug
).filter(
ShoutReactionsFollower.follower == userslug
).first()
if not srf:
srf = ShoutReactionsFollower.create(shout=s.slug, follower=userslug, auto=True)
session.add(srf)
session.commit()
except IntegrityError as e: except IntegrityError as e:
with local_session() as session: with local_session() as session:
s = session.query(Shout).filter(Shout.slug == shout_dict["slug"]).first() s = session.query(Shout).filter(Shout.slug == shout_dict["slug"]).first()
bump = False bump = False
if s: if s:
for key in shout_dict: if s.authors[0] != userslug:
if key in s.__dict__: # create new with different slug
if s.__dict__[key] != shout_dict[key]: shout_dict["slug"] += '-' + shout_dict["layout"]
print( try:
"[migration] shout already exists, but differs in %s" await create_shout(shout_dict, userslug)
% key except IntegrityError as e:
) print(e)
bump = True
else:
print("[migration] shout already exists, but lacks %s" % key)
bump = True bump = True
if bump: else:
s.update(shout_dict) # update old
for key in shout_dict:
if key in s.__dict__:
if s.__dict__[key] != shout_dict[key]:
print(
"[migration] shout already exists, but differs in %s"
% key
)
bump = True
else:
print("[migration] shout already exists, but lacks %s" % key)
bump = True
if bump:
s.update(shout_dict)
else: else:
print("[migration] something went wrong with shout: \n%r" % shout_dict) print("[migration] something went wrong with shout: \n%r" % shout_dict)
raise e raise e