Spaces:
Sleeping
Sleeping
File size: 10,168 Bytes
93cd57d |
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
"""
Скрипт для добавления объектов недвижимости из pars_samolet.sql в базу данных Render
"""
import psycopg2
import re
import sqlite3
import uuid
import sys
from datetime import datetime
# Данные подключения к БД на Render
DB_CONFIG = {
'host': 'dpg-d5ht8vi4d50c739akh2g-a.virginia-postgres.render.com',
'port': 5432,
'database': 'lead_exchange_bk',
'user': 'lead_exchange_bk_user',
'password': '8m2gtTRBW0iAr7nY2Aadzz0VcZBEVKYM'
}
# ID администратора (который мы создали ранее)
ADMIN_USER_ID = None # Будет получен из БД
def get_admin_user_id(conn):
"""Получить ID администратора из БД"""
cursor = conn.cursor()
# Получаем всех администраторов
cursor.execute("""
SELECT user_id, email, first_name, last_name
FROM users
WHERE role = 'ADMIN'
ORDER BY created_at DESC
""")
admins = cursor.fetchall()
if not admins:
raise Exception("Admin user not found in database. Please run add_admin_user.py first.")
if len(admins) == 1:
admin_id, email, first_name, last_name = admins[0]
print(f" Found admin: {email} ({first_name} {last_name})")
return str(admin_id)
# Если несколько админов, показываем их и выбираем последнего созданного (самый новый)
print(f" Found {len(admins)} admin users:")
for admin_id, email, first_name, last_name in admins:
print(f" - {email} ({first_name} {last_name}) - ID: {admin_id}")
# Используем самого нового администратора (первый в списке, т.к. ORDER BY created_at DESC)
selected_admin = admins[0]
print(f" ✅ Using: {selected_admin[1]}")
return str(selected_admin[0])
def parse_sql_file():
"""Парсинг SQL файла с объектами недвижимости"""
print("Reading pars_samolet.sql...")
with open('pars_samolet.sql', 'r', encoding='utf-8') as f:
sql_content = f.read()
# Извлекаем CREATE TABLE
create_match = re.search(r'CREATE TABLE[^;]+;', sql_content, re.DOTALL)
if not create_match:
raise ValueError("CREATE TABLE not found")
create_stmt = create_match.group(0)
# Извлекаем все INSERT
insert_pattern = r'INSERT INTO mytable\([^)]+\) VALUES\s*\([^;]+\);'
inserts = re.findall(insert_pattern, sql_content, re.DOTALL)
print(f"Found {len(inserts)} INSERT statements")
# Создаем временную базу для парсинга
conn = sqlite3.connect(':memory:')
cursor = conn.cursor()
cursor.execute(create_stmt)
# Выполняем INSERT
successful = 0
for i, insert in enumerate(inserts):
try:
cursor.execute(insert)
successful += 1
except sqlite3.Error as e:
print(f"Warning: Could not parse INSERT #{i+1}: {e}")
continue
print(f"Successfully parsed {successful}/{len(inserts)} records")
# Получаем данные
cursor.execute('SELECT * FROM mytable')
rows = cursor.fetchall()
# Получаем имена колонок
cursor.execute("PRAGMA table_info(mytable)")
columns = [col[1] for col in cursor.fetchall()]
# Создаем список словарей
objects = [dict(zip(columns, row)) for row in rows]
conn.close()
return objects
def map_property_type(old_type):
"""Маппинг типов недвижимости"""
mapping = {
'Квартира': 'APARTMENT',
'Дом': 'HOUSE',
'Коммерческая': 'COMMERCIAL',
'Участок': 'LAND'
}
return mapping.get(old_type, 'APARTMENT')
def map_status(old_status):
"""Маппинг статусов"""
mapping = {
'Доступно': 'PUBLISHED',
'Продано': 'SOLD',
'Архив': 'ARCHIVED'
}
return mapping.get(old_status, 'PUBLISHED')
def insert_properties(objects, owner_user_id, auto_confirm=False):
"""Вставка объектов недвижимости в БД"""
print(f"\nConnecting to database at {DB_CONFIG['host']}...")
try:
conn = psycopg2.connect(**DB_CONFIG)
cursor = conn.cursor()
print("Connected successfully!")
# Проверяем, есть ли уже данные
cursor.execute("SELECT COUNT(*) FROM properties")
existing_count = cursor.fetchone()[0]
print(f"Current properties in database: {existing_count}")
if existing_count > 0:
if auto_confirm:
print(f"\nAuto-confirm enabled: Deleting {existing_count} existing properties...")
cursor.execute("DELETE FROM properties")
conn.commit()
print("Deleted existing properties")
else:
try:
response = input(f"\nDatabase already has {existing_count} properties. Delete them? (yes/y/no/n): ")
if response.lower() in ['yes', 'y']:
cursor.execute("DELETE FROM properties")
conn.commit()
print("Deleted existing properties")
except EOFError:
print("\n⚠️ Input interrupted. Keeping existing properties.")
print("Run with --yes flag to auto-confirm or run interactively.")
# Вставляем объекты
inserted = 0
failed = 0
insert_query = """
INSERT INTO properties (
property_id, title, description, address, property_type,
area, price, rooms, status, owner_user_id, created_user_id,
created_at, updated_at
) VALUES (
%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s
)
"""
print(f"\nInserting {len(objects)} properties...")
for i, obj in enumerate(objects):
try:
# Подготавливаем данные
property_id = obj.get('property_id', str(uuid.uuid4()))
title = obj.get('title', '')[:255] # Ограничиваем длину
description = obj.get('description', '')
address = obj.get('address', '')
property_type = map_property_type(obj.get('property_type', 'Квартира'))
area = float(obj['area']) if obj.get('area') else None
price = int(obj['price']) if obj.get('price') else None
rooms = int(obj['rooms']) if obj.get('rooms') else None
status = map_status(obj.get('status', 'Доступно'))
# Даты
created_at = obj.get('created_at', datetime.now().isoformat())
updated_at = obj.get('updated_at', datetime.now().isoformat())
cursor.execute(insert_query, (
property_id, title, description, address, property_type,
area, price, rooms, status, owner_user_id, owner_user_id,
created_at, updated_at
))
inserted += 1
if (i + 1) % 50 == 0:
print(f" Inserted {i + 1}/{len(objects)}...")
except Exception as e:
failed += 1
print(f" Failed to insert property {obj.get('property_id', 'unknown')}: {e}")
continue
# Коммитим изменения
conn.commit()
print(f"\n✅ Successfully inserted {inserted} properties")
if failed > 0:
print(f"⚠️ Failed to insert {failed} properties")
# Проверяем финальное количество
cursor.execute("SELECT COUNT(*) FROM properties")
final_count = cursor.fetchone()[0]
print(f"\nTotal properties in database: {final_count}")
cursor.close()
conn.close()
except psycopg2.Error as e:
print(f"❌ Database error: {e}")
raise
except Exception as e:
print(f"❌ Error: {e}")
raise
def main():
print("=" * 60)
print("Adding Properties to Database")
print("=" * 60)
# Проверяем параметры командной строки
auto_confirm = '--yes' in sys.argv or '-y' in sys.argv
if auto_confirm:
print("🤖 Auto-confirm mode enabled")
# Парсим SQL файл
objects = parse_sql_file()
if not objects:
print("No objects found in pars_samolet.sql")
return
print(f"\nParsed {len(objects)} properties from file")
print(f"Sample property: {objects[0].get('title', 'N/A')}")
# Подключаемся к БД и получаем ID администратора
print("\nGetting admin user ID...")
try:
conn = psycopg2.connect(**DB_CONFIG)
admin_id = get_admin_user_id(conn)
conn.close()
print(f"Admin user ID: {admin_id}")
except Exception as e:
print(f"❌ Error: {e}")
return
# Подтверждение
if not auto_confirm:
print(f"\nReady to insert {len(objects)} properties into database")
print(f"Database: {DB_CONFIG['host']}/{DB_CONFIG['database']}")
try:
response = input("\nProceed? (yes/y/no/n): ")
if response.lower() not in ['yes', 'y']:
print("Cancelled by user")
return
except EOFError:
print("\n❌ Error: EOF when reading input")
print("Run with --yes flag to auto-confirm: python add_properties.py --yes")
return
else:
print(f"\n✅ Auto-confirming insertion of {len(objects)} properties")
# Вставляем объекты
insert_properties(objects, admin_id, auto_confirm)
print("\n" + "=" * 60)
print("✅ Done!")
print("=" * 60)
if __name__ == '__main__':
main()
|