new file: Dockerfile
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
This commit is contained in:
@@ -0,0 +1,6 @@
|
||||
FROM python:3.13-slim
|
||||
WORKDIR /app/
|
||||
COPY requirements.txt .
|
||||
RUN pip install --no-cache-dir -r requirements.txt
|
||||
COPY . .
|
||||
CMD ["python3", "db_migration_test2.py"]
|
||||
Binary file not shown.
@@ -0,0 +1,38 @@
|
||||
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')
|
||||
@@ -0,0 +1 @@
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
psycopg
|
||||
Reference in New Issue
Block a user