mirror of
https://github.com/NoiTheCat/WorldTime.git
synced 2024-11-21 14:34:36 +00:00
Add database copy utility
This commit is contained in:
parent
b57a7b9b37
commit
2b42e4b00d
1 changed files with 40 additions and 0 deletions
40
dbtransfer.py
Normal file
40
dbtransfer.py
Normal file
|
@ -0,0 +1,40 @@
|
|||
#!/usr/bin/env python3
|
||||
|
||||
# Utility for carrying over data from an older SQLite
|
||||
# database into a newer PostgreSQL one.
|
||||
|
||||
# Edit this value:
|
||||
PgConnectionString = ''
|
||||
|
||||
import sqlite3
|
||||
import psycopg2
|
||||
|
||||
sldb = sqlite3.connect('users.db')
|
||||
slcur = sldb.cursor()
|
||||
pgdb = psycopg2.connect(PgConnectionString)
|
||||
pgcur = pgdb.cursor()
|
||||
|
||||
pgcur.execute("""
|
||||
CREATE TABLE IF NOT EXISTS userdata (
|
||||
guild_id BIGINT,
|
||||
user_id BIGINT,
|
||||
zone TEXT NOT NULL,
|
||||
last_active TIMESTAMPTZ NOT NULL DEFAULT now(),
|
||||
PRIMARY KEY (guild_id, user_id)
|
||||
)""")
|
||||
pgdb.commit()
|
||||
pgcur.execute("TRUNCATE TABLE userdata")
|
||||
pgdb.commit()
|
||||
|
||||
slcur.execute('''
|
||||
SELECT guild, user, zone, lastactive
|
||||
FROM users
|
||||
''')
|
||||
|
||||
for row in slcur:
|
||||
pgcur.execute('''
|
||||
INSERT INTO userdata (guild_id, user_id, zone, last_active)
|
||||
VALUES (%s, %s, %s, to_timestamp(%s) at time zone 'utc')
|
||||
''', (int(row[0]), int(row[1]), row[2], int(row[3])))
|
||||
pgdb.commit()
|
||||
print(row)
|
Loading…
Reference in a new issue