[misc] Migration script creates nice output now

This commit is contained in:
missytake 2023-04-01 20:59:43 +02:00 committed by Gitea
parent ea58f4a150
commit e9eb98dba8

View file

@ -3,6 +3,7 @@ import argparse
from time import time, sleep from time import time, sleep
from os import urandom from os import urandom
from tortoise import Tortoise from tortoise import Tortoise
from tortoise.exceptions import DoesNotExist
import asyncio import asyncio
from kibicara.model import Admin, Hood, IncludePattern, ExcludePattern from kibicara.model import Admin, Hood, IncludePattern, ExcludePattern
@ -18,8 +19,6 @@ class OldDataBase:
self.cur = self.conn.cursor() self.cur = self.conn.cursor()
def execute(self, *args, **kwargs): def execute(self, *args, **kwargs):
if len(args) > 1:
print(args[0], args[1][0])
return self.cur.execute(*args, **kwargs) return self.cur.execute(*args, **kwargs)
def commit(self): def commit(self):
@ -83,16 +82,13 @@ class Main:
url = instance[1] url = instance[1]
client_id = instance[2] client_id = instance[2]
client_secret = instance[3] client_secret = instance[3]
print(url)
print(client_id)
print(client_secret)
await MastodonInstance.create( await MastodonInstance.create(
name=url, client_id=client_id, client_secret=client_secret name=url, client_id=client_id, client_secret=client_secret
) )
print(f"Created Mastodon Instance: {url}")
old_users = self.old_db.execute("SELECT * FROM user;").fetchall() old_users = self.old_db.execute("SELECT * FROM user;").fetchall()
for user in old_users: for user in old_users:
print(user)
user_id = user[0] user_id = user[0]
user_passhash = user[1] user_passhash = user[1]
user_enabled = user[2] user_enabled = user[2]
@ -103,7 +99,10 @@ class Main:
email = self.old_db.execute( email = self.old_db.execute(
"SELECT email FROM email WHERE user_id=?", (user_id,) "SELECT email FROM email WHERE user_id=?", (user_id,)
).fetchone()[0] ).fetchone()[0]
print("email:", email) try:
await Admin.get(email=email)
continue # skip if the user already exists.
except DoesNotExist:
admin = await Admin.create(email=email, passhash=user_passhash) admin = await Admin.create(email=email, passhash=user_passhash)
city = self.old_db.execute( city = self.old_db.execute(
@ -111,23 +110,25 @@ class Main:
).fetchone() ).fetchone()
city_name = city[2] city_name = city[2]
city_markdown = city[3] city_markdown = city[3]
# print(city_name)
# print(city_markdown)
hood = await Hood.create(name=city_name, landingpage=city_markdown) hood = await Hood.create(name=city_name, landingpage=city_markdown)
await hood.admins.add(admin) await hood.admins.add(admin)
print()
print(
"Migrated user %s with email %s for the city %s"
"with the following accounts:" % (user_id, email, city_name)
)
patterns = self.old_db.execute( patterns = self.old_db.execute(
"SELECT patterns FROM triggerpatterns WHERE user_id=?", (user_id,) "SELECT patterns FROM triggerpatterns WHERE user_id=?", (user_id,)
).fetchone()[0] ).fetchone()[0]
for pattern in patterns.splitlines(): for pattern in patterns.splitlines():
print("trigger:", pattern)
await IncludePattern.create(hood=hood, pattern=pattern) await IncludePattern.create(hood=hood, pattern=pattern)
badwords = self.old_db.execute( badwords = self.old_db.execute(
"SELECT words FROM badwords WHERE user_id=?", (user_id,) "SELECT words FROM badwords WHERE user_id=?", (user_id,)
).fetchone()[0] ).fetchone()[0]
for badword in badwords.splitlines(): for badword in badwords.splitlines():
print("bad word:", badword)
await ExcludePattern.create(hood=hood, pattern=badword) await ExcludePattern.create(hood=hood, pattern=badword)
mastodon_account = self.old_db.execute( mastodon_account = self.old_db.execute(
@ -147,9 +148,7 @@ class Main:
access_token=access_token, access_token=access_token,
enabled=mastodon_enabled, enabled=mastodon_enabled,
) )
# print(instance_url) print(f"Mastodon: {instance_url}, {access_token}")
# print(access_token)
# print(mastodon_enabled)
telegram_account = self.old_db.execute( telegram_account = self.old_db.execute(
"SELECT apikey, active FROM telegram_accounts WHERE user_id=?", "SELECT apikey, active FROM telegram_accounts WHERE user_id=?",
@ -158,8 +157,6 @@ class Main:
if telegram_account[0] != "": if telegram_account[0] != "":
telegram_apikey = telegram_account[0] telegram_apikey = telegram_account[0]
telegram_enabled = telegram_account[1] telegram_enabled = telegram_account[1]
# print(telegram_apikey)
# print(telegram_enabled)
telegram = await Telegram.create( telegram = await Telegram.create(
hood=hood, hood=hood,
api_token=telegram_apikey, api_token=telegram_apikey,
@ -173,6 +170,7 @@ class Main:
for subscriber in telegram_subscribers: for subscriber in telegram_subscribers:
subscriber_id = subscriber[0] subscriber_id = subscriber[0]
await TelegramSubscriber.create(bot=telegram, user_id=subscriber_id) await TelegramSubscriber.create(bot=telegram, user_id=subscriber_id)
print(f"Telegram: {len(telegram_subscribers)} subscribers")
mail_subscribers = self.old_db.execute( mail_subscribers = self.old_db.execute(
"SELECT email FROM mailinglist WHERE user_id=?", (user_id,) "SELECT email FROM mailinglist WHERE user_id=?", (user_id,)
@ -185,7 +183,7 @@ class Main:
for subscriber in mail_subscribers: for subscriber in mail_subscribers:
subscriber_email = subscriber[0] subscriber_email = subscriber[0]
await EmailSubscriber.create(email=subscriber_email, hood=hood) await EmailSubscriber.create(email=subscriber_email, hood=hood)
# print("subscriber:", subscriber_email) print(f"E-Mail: {len(mail_subscribers)} subscribers")
twitter = self.old_db.execute( twitter = self.old_db.execute(
"SELECT * FROM twitter_accounts WHERE user_id=?", (user_id,) "SELECT * FROM twitter_accounts WHERE user_id=?", (user_id,)
@ -206,4 +204,5 @@ class Main:
verified=False, verified=False,
enabled=True, enabled=True,
) )
print(f"Twitter: last ID: {seen_tweet}")
await Tortoise.close_connections() await Tortoise.close_connections()