-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDBMigration.py
More file actions
72 lines (61 loc) · 1.83 KB
/
DBMigration.py
File metadata and controls
72 lines (61 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""
This script is intended to migrate from the old sqlite StumbleServer db to the 2020 model.
"""
import sqlite3, json, os
from sqlalchemy.orm import sessionmaker
import sqlalchemy
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from SqlAlchemyTables import *
db_string = os.environ["DB_STRING"]
#engine = create_engine(db_string, echo=False)
#engine = create_engine("sqlite:////home/hapax/projects/StumbleServer/test.db", echo=False)
Session = sessionmaker(bind=engine)
Base.metadata.create_all(engine)
conn = sqlite3.connect("OldStumble.db")
cur = conn.cursor()
"""
Copy over sites.
"""
cur.execute("select * from sites;")
rows = cur.fetchall()
unique_sites = {}
for row in rows:
name = row[1].strip()
unique_sites[name] = row
session = Session()
u = User(id="0")
session.add(u)
for row in unique_sites:
siteId, url = unique_sites[row]
session.add(Site(id=siteId.strip(), url=url.strip()))
session.add(Submission(userId=u.id, url=url.strip(), status=1, reason="Imported in db migration."))
session.commit()
cur.execute("select * from users;")
rows = cur.fetchall()
#including_count = 0
#excluding_count = 0
count = 0
good_count = 0
session = Session()
for i, row in enumerate(rows):
userId, j = row
j = j.replace("\n", "")
j = json.loads(j)
if type(j) == type({}):
user = User(id=userId)
session.add(user)
for k in j:
try:
s = session.query(Site).get(k)
if not s:
continue
session.add(Visit(siteId=k, userId=user.id, liked=j[k].get("liked", False)))
good_count += 1
except:
count += 1
if i % 10 == 0:
print(f"{i}/{len(rows)}")
session.commit()
session.commit()
print(f"Errors: {count} / {good_count + count}")