File size: 1,822 Bytes
61d29fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#!/usr/bin/env python3
"""
Create OpenStates database schema using Django migrations.

This creates all the tables needed to load the OpenStates PostgreSQL dump.
"""

import os
import sys
import django
from pathlib import Path

# Setup Django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'openstates_schema_settings')

# Create a minimal Django settings module
SETTINGS_CONTENT = """
import os

# Database
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql',
        'NAME': 'openstates',
        'USER': 'postgres',
        'PASSWORD': 'postgres',
        'HOST': 'localhost',
        'PORT': '5433',
    }
}

# Required Django settings
SECRET_KEY = 'temporary-key-for-schema-creation'
INSTALLED_APPS = [
    'django.contrib.contenttypes',
    'openstates.data',
]
USE_TZ = True
"""

# Write settings file
settings_file = Path(__file__).parent / 'openstates_schema_settings.py'
settings_file.write_text(SETTINGS_CONTENT)

# Setup Django
django.setup()

# Import Django management
from django.core.management import call_command

print("🔧 Creating OpenStates database schema...")
print("=" * 60)

try:
    # Run migrations to create all tables
    print("\n📝 Running Django migrations...")
    call_command('migrate', '--noinput', verbosity=2)
    
    print("\n✅ Schema created successfully!")
    print("\nNext step: Load the data with:")
    print("  docker exec openstates-db pg_restore -U postgres -d openstates \\")
    print("    --data-only --no-owner --no-acl /dumps/2026-04-public.pgdump")
    
except Exception as e:
    print(f"\n❌ Error: {e}")
    print("\nTry installing openstates-core:")
    print("  pip install openstates-core psycopg2-binary Django==3.2.14")
    sys.exit(1)
finally:
    # Clean up
    if settings_file.exists():
        settings_file.unlink()