feat: migrate configuration to .env environment variables

This commit implements a complete migration from JSON-based configuration
to .env environment variables, providing better security and flexibility.

Key Changes:
- Add python-dotenv dependency for environment variable support
- Create config/env_loader.py with type conversion utilities
- Add from_env() class methods to all config dataclasses
- Update ConfigLoader to prioritize environment variables
- Add save_to_env() method for .env file management
- Implement database connection factory pattern
- Add base DAO and connection classes for better abstraction
- Support both SQL Server and MySQL with unified interface
- Create migration script (scripts/migrate_to_env.py)
- Update GUI to read/write .env files
- Add comprehensive migration documentation

New Files:
- config/env_loader.py - Environment variable loader
- db/base_connection.py - Base database connection interface
- db/base_dao.py - Base DAO with common utilities
- db/connection_factory.py - Factory for creating connections
- db/mysql_connection.py - MySQL-specific connection
- db/sqlserver_connection.py - SQL Server-specific connection
- db/table_name_converter.py - SQL dialect converter
- scripts/migrate_to_env.py - Configuration migration tool
- docs/ENV_MIGRATION.md - Complete migration guide
- .env.example - Environment variable template

Testing:
- Verified MySQL connection (8.0.44)
- Tested all DAO operations
- Confirmed 150 tables accessible
- Validated configuration loading

Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
This commit is contained in:
Misaka
2026-02-09 22:39:14 +08:00
parent 04b99292ad
commit aaa46ef282
22 changed files with 2545 additions and 563 deletions

View File

@@ -6,31 +6,14 @@ from the [productionContractData].[26年压力表合同数据] table.
"""
from typing import List, Dict, Any
from db.base_dao import BaseDAO
from db.connection import get_connection
from config.schema import DatabaseType
class ProductionContractDataDAO:
class ProductionContractDataDAO(BaseDAO):
"""Data Access Object for production contract data queries"""
def __init__(self):
self.db = None
def __enter__(self):
"""Enter context manager and establish database connection"""
self.db = get_connection()
self.db.connect()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
"""Exit context manager and close database connection"""
if self.db:
self.db.disconnect()
def close(self):
"""Close database connection"""
if self.db:
self.db.disconnect()
def query_by_总排号(self, 总排号_list: List[str]) -> List[Dict[str, Any]]:
"""
Query production contract data by 总排号 list.
@@ -50,13 +33,27 @@ class ProductionContractDataDAO:
for i in range(0, len(总排号_list), batch_size):
batch = 总排号_list[i:i + batch_size]
placeholders = ','.join(['?' for _ in batch])
sql = f"""
SELECT [总排号], [生产订单号], [序号], [订单号], [客户名称], [产品型号]
FROM [productionContractData].[26年压力表合同数据]
WHERE [总排号] IN ({placeholders})
ORDER BY [序号]
"""
placeholder = self._get_placeholder()
placeholders = ','.join([placeholder for _ in batch])
# 根据数据库类型选择表名
table_name = self._convert_sql('[productionContractData].[26年压力表合同数据]')
# 根据数据库类型选择列名格式
if self._db_type == DatabaseType.MYSQL:
sql = f"""
SELECT 总排号, 生产订单号, 序号, 订单号, 客户名称, 产品型号
FROM {table_name}
WHERE 总排号 IN ({placeholders})
ORDER BY 序号
"""
else:
sql = f"""
SELECT [总排号], [生产订单号], [序号], [订单号], [客户名称], [产品型号]
FROM {table_name}
WHERE [总排号] IN ({placeholders})
ORDER BY [序号]
"""
with get_connection() as db:
results = db.execute_query(sql, tuple(batch))