initial commit (backend)
This commit is contained in:
@@ -0,0 +1,55 @@
|
||||
import csv
|
||||
import asyncpg
|
||||
import asyncio
|
||||
from config import DB_CONFIG
|
||||
|
||||
async def load_all():
|
||||
pool = await asyncpg.create_pool(**DB_CONFIG)
|
||||
async with pool.acquire() as conn:
|
||||
level_files = [
|
||||
("A1", "words_a1.csv"),
|
||||
("A2", "words_a2.csv"),
|
||||
("B1", "words_b1.csv"),
|
||||
]
|
||||
|
||||
total_added = 0
|
||||
|
||||
for level, fname in level_files:
|
||||
print(f'\n{"="*50}')
|
||||
print(f'Loading {fname} (level {level})...')
|
||||
print(f'{"="*50}')
|
||||
|
||||
# 🔧 ФИКС: удаляем ВСЕ строки уровня, независимо от level_id
|
||||
deleted = await conn.execute(
|
||||
"DELETE FROM level_words WHERE level = $1",
|
||||
level
|
||||
)
|
||||
print(f' Deleted old entries: {deleted}')
|
||||
|
||||
count = 0
|
||||
with open(fname, 'r', encoding='utf-8') as f:
|
||||
reader = csv.DictReader(f)
|
||||
for row in reader:
|
||||
word = row['polish'].strip().lower()
|
||||
if word:
|
||||
await conn.execute(
|
||||
'''INSERT INTO level_words (level_id, word_pl, translation_ru, translation_uk, translation_en, level)
|
||||
VALUES (NULL, $1, $2, $3, $4, $5)
|
||||
ON CONFLICT (level, word_pl) DO NOTHING''',
|
||||
word,
|
||||
row.get('russian', ''),
|
||||
row.get('ukrainian', ''),
|
||||
row.get('english', ''),
|
||||
level
|
||||
)
|
||||
count += 1
|
||||
|
||||
total_added += count
|
||||
print(f' Added {count} words from {fname} (level {level})')
|
||||
|
||||
await pool.close()
|
||||
|
||||
print(f'\n🎉 Готово! Всего загружено {total_added} слов.')
|
||||
|
||||
if __name__ == '__main__':
|
||||
asyncio.run(load_all())
|
||||
Reference in New Issue
Block a user