Commit e068d6af authored by Bilal Elmoussaoui's avatar Bilal Elmoussaoui

Migrations: handle the difference between the old and new database scheme

parent 95db21ae
Pipeline #84223 passed with stages
in 28 minutes and 45 seconds
......@@ -13,9 +13,14 @@ def do_step(conn):
content = str(g_file.load_contents(None)[1].decode("utf-8"))
data = json.loads(content)
providers = []
providers_db = conn.execute("SELECT name FROM providers").fetchall()
providers_db = [provider[0].lower() for provider in providers_db]
for provider_name, provider_info in data.items():
providers.append((provider_name, provider_info['url'],
provider_info['doc'], provider_info['img'],))
if not provider_name.lower() in providers_db:
providers.append((provider_name, provider_info['url'],
provider_info['doc'], provider_info['img'],))
query = "INSERT INTO providers (name, website, doc_url, image) VALUES (?, ?, ?, ?)"
conn.executemany(query, providers)
conn.commit()
......
......@@ -3,25 +3,36 @@ Restore old accounts
"""
from yoyo import step
from collections import OrderedDict
__depends__ = {'authenticator_20190525_03_R7miN-add-default-providers'}
def do_step(conn):
accounts = conn.execute("SELECT * FROM accounts").fetchall()
# atler columns from secret id to token_id before
# to fix the issue on older db
try:
accounts = conn.execute("SELECT id, username, token_id, provider FROM accounts").fetchall()
except Exception:
accounts = conn.execute("SELECT id, username, secret_id, provider FROM accounts").fetchall()
_accounts = []
providers_db = conn.execute("SELECT id, name FROM providers").fetchall()
providers = {}
providers = OrderedDict()
for provider_id, provider_name in providers_db:
providers[provider_name.lower()] = provider_id
providers[provider_id] = provider_name.lower()
cursor = conn.cursor()
for account_id, username, provider_name, secret_id in accounts:
if provider_name.lower() not in providers.keys():
cursor.execute("INSERT INTO providers (name) VALUES (?)", (provider_name, ))
provider_id = cursor.lastrowid
for account_id, username, secret_id, provider in accounts:
if isinstance(provider, str):
if provider.lower() not in providers.values():
cursor.execute("INSERT INTO providers (name) VALUES (?)", (provider, ))
provider_id = cursor.lastrowid
else:
provider_index = list(providers.values()).index(provider.lower())
provider_id = list(providers.keys())[provider_index]
else:
provider_id = providers[provider_name.lower()]
provider_id = provider
_accounts.append((account_id, username, provider_id, secret_id))
cursor.execute(" ALTER TABLE accounts RENAME TO tmp;")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment