migrate old filehost to new saddle
This commit is contained in:
parent
16a4e2dc9f
commit
168272761a
53
migrate_filehost.py
Executable file
53
migrate_filehost.py
Executable file
|
@ -0,0 +1,53 @@
|
||||||
|
#!/usr/bin/env python3
|
||||||
|
"""
|
||||||
|
Migrates a filehost sqlite database to Saddle's PostgreSQL database.
|
||||||
|
"""
|
||||||
|
import sys
|
||||||
|
import asyncio
|
||||||
|
import sqlite3
|
||||||
|
from datetime import datetime, timezone
|
||||||
|
|
||||||
|
import asyncpg
|
||||||
|
|
||||||
|
import config
|
||||||
|
|
||||||
|
def get_data(database_path):
|
||||||
|
"""Retrieves data from the filehost database."""
|
||||||
|
conn = sqlite3.connect(database_path)
|
||||||
|
conn.row_factory = sqlite3.Row
|
||||||
|
cur = conn.cursor()
|
||||||
|
data = cur.execute("SELECT * FROM uploads")
|
||||||
|
return data
|
||||||
|
|
||||||
|
|
||||||
|
def process_data(data):
|
||||||
|
"""Processes the data so that it may be inserted into PostgreSQL."""
|
||||||
|
new_data = []
|
||||||
|
for record in data:
|
||||||
|
filename = record['filename']
|
||||||
|
_id = filename.partition('_')[0]
|
||||||
|
users = {'iou1name': 1, 'Adalwulf': 2, 'consultx': 3, None: 1}
|
||||||
|
user_id = users[record['uploaded_by']]
|
||||||
|
upload_date = datetime.utcfromtimestamp(record['uploaded_date'])
|
||||||
|
upload_date.replace(tzinfo=timezone.utc)
|
||||||
|
|
||||||
|
tup = (user_id, _id, filename, upload_date)
|
||||||
|
new_data.append(tup)
|
||||||
|
return new_data
|
||||||
|
|
||||||
|
|
||||||
|
async def insert_data(data):
|
||||||
|
"""Inserts the processed data into PostgreSQL."""
|
||||||
|
conn = await asyncpg.connect(**config.db)
|
||||||
|
await conn.executemany(
|
||||||
|
"INSERT INTO upload (user_id, id, filename, upload_date) "
|
||||||
|
"VALUES ($1, $2, $3, $4)",
|
||||||
|
data)
|
||||||
|
await conn.close()
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
database_path = sys.argv[1]
|
||||||
|
data = get_data(database_path)
|
||||||
|
data = process_data(data)
|
||||||
|
asyncio.get_event_loop().run_until_complete(insert_data(data))
|
|
@ -174,8 +174,7 @@ def download_file(url, timeout=10, max_file_size=config.client_max_size):
|
||||||
|
|
||||||
async def init_app():
|
async def init_app():
|
||||||
"""Initializes the application."""
|
"""Initializes the application."""
|
||||||
#app = web.Application(middlewares=[buckler_aiohttp.buckler_session])
|
app = web.Application(middlewares=[buckler_aiohttp.buckler_session])
|
||||||
app = web.Application()
|
|
||||||
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates'))
|
aiohttp_jinja2.setup(app, loader=jinja2.FileSystemLoader('templates'))
|
||||||
app['pool'] = await asyncpg.create_pool(**config.db)
|
app['pool'] = await asyncpg.create_pool(**config.db)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user