b3213ab863
new file: Makefile new file: database-migration/Dockerfile new file: database-migration/data.db new file: database-migration/db_migration.py new file: database-migration/migration.py new file: database-migration/requirements.txt new file: docker-compose.migration.yml new file: docker-compose.yml new file: main.py new file: requirements.txt
38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
print('RUNNING!')
|
|
|
|
import sqlite3
|
|
import os
|
|
import psycopg
|
|
|
|
print(os.getcwd())
|
|
print(os.listdir())
|
|
|
|
with open('data.db', 'rb') as f:
|
|
data = f.read()
|
|
print(f'[DEBUG] Begin SQLite database')
|
|
print(data)
|
|
print(f'[DEBUG] End SQLite database')
|
|
|
|
|
|
conn = psycopg.connect(
|
|
host = 'db',
|
|
port = 5432,
|
|
dbname = os.getenv('POSTGRES_DB'),
|
|
user = os.getenv('POSTGRES_USER'),
|
|
password = os.getenv('POSTGRES_PASSWORD')
|
|
)
|
|
|
|
cursor = conn.cursor()
|
|
|
|
with sqlite3.connect('data.db') as sqlite3_conn:
|
|
sqlite_cursor = sqlite3_conn.cursor()
|
|
sqlite_cursor.execute('SELECT COUNT(*) FROM data')
|
|
number_of_users = sqlite_cursor.fetchone()[0]
|
|
for user in range(1, number_of_users + 1):
|
|
print(f'Migrating user {user} ...')
|
|
sqlite_cursor.execute('SELECT (id, email, remail, phone, password) FROM data WHERE id = ?', (user,))
|
|
cursor.execute('INSERT INTO users (id, email, remail, phone, password) VALUES (%s, %s, %s, %s, %s)', tuple(sqlite_cursor.fetchone()))
|
|
cursor.execute('SELECT setval(pg_get_serial_sequence(\'users\', \'id\'), (SELECT MAX(id) FROM users), true)')
|
|
sqlite_cursor.close()
|
|
|
|
print('[INFO] Migration completed') |