Files
njts-accounting-core/verify_opening_balances_table.py
2026-02-21 10:51:25 +09:00

73 lines
2.3 KiB
Python
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#!/usr/bin/env python3
"""opening_balances テーブルのデータ確認スクリプト"""
import psycopg
print("="*60)
print("【opening_balances テーブルデータ確認】")
print("="*60)
try:
conn = psycopg.connect('host=localhost user=postgres password=postgres dbname=accounting')
cur = conn.cursor()
# 1. テーブル自体の存在確認
cur.execute("""
SELECT EXISTS (
SELECT 1 FROM information_schema.tables
WHERE table_name = 'opening_balances'
)
""")
table_exists = cur.fetchone()[0]
print(f"\n1⃣ opening_balances テーブルの存在: {'✓ 存在' if table_exists else '❌ 存在しない'}")
if not table_exists:
print(" → テーブルを作成する必要があります")
cur.close()
conn.close()
exit(1)
# 2. 全行数
cur.execute("SELECT COUNT(*) FROM opening_balances")
total_count = cur.fetchone()[0]
print(f"\n2⃣ 全体の行数: {total_count}")
if total_count == 0:
print(" ⚠️ opening_balances テーブルは空です")
else:
# 3. 年度別統計
cur.execute("""
SELECT fiscal_year, COUNT(*) as cnt
FROM opening_balances
GROUP BY fiscal_year
ORDER BY fiscal_year DESC
""")
print(f"\n3⃣ 年度別件数:")
for row in cur.fetchall():
print(f" - {row[0]}年度: {row[1]}")
# 4. 2025年度のデータ最初の5件
print(f"\n4⃣ 2025年度のサンプルデータ最初の5件:")
cur.execute("""
SELECT a.account_code, a.account_name, o.opening_debit, o.opening_credit
FROM opening_balances o
JOIN accounts a ON a.account_id = o.account_id
WHERE o.fiscal_year = 2025
LIMIT 5
""")
rows = cur.fetchall()
if rows:
for row in rows:
print(f" {row[0]:3s} {row[1]:20s} 借方:{row[2]:>12} 貸方:{row[3]:>12}")
else:
print(" → 2025年度のデータがありません")
cur.close()
conn.close()
print("\n" + "="*60)
except Exception as e:
print(f"\n❌ エラー: {e}")
import traceback
traceback.print_exc()