-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmigrate_sqlite_data.py
More file actions
55 lines (42 loc) Β· 1.57 KB
/
migrate_sqlite_data.py
File metadata and controls
55 lines (42 loc) Β· 1.57 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
#!/usr/bin/env python3
"""
Data migration script to transfer SQLite data to PostgreSQL
"""
import os
import sys
import django
import json
from pathlib import Path
# Add the project directory to Python path
BASE_DIR = Path(__file__).resolve().parent
sys.path.append(str(BASE_DIR))
def main():
print("π¦ Starting SQLite to PostgreSQL data migration...")
# Check if SQLite file exists
sqlite_path = BASE_DIR / 'db.sqlite3'
if not sqlite_path.exists():
print("β No SQLite database found at db.sqlite3")
return
# Temporarily use SQLite to dump data
print("π Dumping data from SQLite...")
os.environ['DATABASE_URL'] = '' # Force SQLite usage
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'scouting_backend.settings')
django.setup()
from django.core.management import call_command
# Dump data
with open('data_dump.json', 'w') as f:
call_command('dumpdata', '--natural-foreign', '--natural-primary', stdout=f)
print("β
Data dumped to data_dump.json")
# Now switch to PostgreSQL and load data
database_url = input("Enter your Supabase DATABASE_URL: ")
os.environ['DATABASE_URL'] = database_url
# Reload Django with PostgreSQL
from importlib import reload
from django.conf import settings
reload(settings)
print("π Loading data into PostgreSQL...")
call_command('loaddata', 'data_dump.json')
print("β
Migration complete!")
print("ποΈ You can now delete db.sqlite3 and data_dump.json")
if __name__ == '__main__':
main()