Fixed database initialization
This commit is contained in:
@@ -10,10 +10,9 @@ logging.config.dictConfig(LOGGING_CONFIG)
|
||||
logger = logging.getLogger("database")
|
||||
|
||||
BASE_DIR = Path(__file__).parent.resolve()
|
||||
DATA_DIR = BASE_DIR / "dbs"
|
||||
DATA_DIR.mkdir(parents=True, exist_ok=True)
|
||||
BASE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
|
||||
DATABASE_URL = f"sqlite+aiosqlite:///{DATA_DIR / 'stcs.db'}"
|
||||
DATABASE_URL = f"sqlite+aiosqlite:///{BASE_DIR / 'stcs.db'}"
|
||||
|
||||
async_engine = create_async_engine(
|
||||
DATABASE_URL,
|
||||
@@ -39,7 +38,7 @@ async_session = async_sessionmaker(
|
||||
async def init_db():
|
||||
try:
|
||||
async with async_engine.begin() as conn:
|
||||
await conn.run_sync(Base.metadata.create_all)
|
||||
await conn.run_sync(lambda sync_conn: Base.metadata.create_all(bind=sync_conn, checkfirst=True))
|
||||
logger.info("Database initialized.")
|
||||
except Exception as e:
|
||||
logger.error("Database initialization failed: %s", e)
|
||||
@@ -154,8 +154,8 @@ class UserDeals(Base):
|
||||
order_quantity = Column(Float, nullable=True)
|
||||
martingale_factor = Column(Float, nullable=True)
|
||||
max_bets_in_series = Column(Integer, nullable=True)
|
||||
take_profit_percent = Column(Integer, nullable=True)
|
||||
stop_loss_percent = Column(Integer, nullable=True)
|
||||
take_profit_percent = Column(Float, nullable=True)
|
||||
stop_loss_percent = Column(Float, nullable=True)
|
||||
trigger_price = Column(Float, nullable=True)
|
||||
current_series = Column(Integer, nullable=True)
|
||||
commission_fee = Column(String, nullable=True)
|
||||
|
||||
@@ -86,7 +86,7 @@ async def set_user_api(tg_id: int, api_key: str, api_secret: str) -> bool:
|
||||
else:
|
||||
# Creating new record
|
||||
user_api = UserApi(
|
||||
user=user, api_key=api_key, api_secret=api_secret
|
||||
user_id=user.id, api_key=api_key, api_secret=api_secret
|
||||
)
|
||||
session.add(user_api)
|
||||
|
||||
@@ -141,7 +141,7 @@ async def set_user_symbol(tg_id: int, symbol: str) -> bool:
|
||||
# Creating new record
|
||||
user_symbol = UserSymbol(
|
||||
symbol=symbol,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_symbol)
|
||||
|
||||
@@ -197,7 +197,7 @@ async def create_user_additional_settings(tg_id: int) -> None:
|
||||
|
||||
# Create the user additional settings
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
trade_mode="Long", # Default value
|
||||
switch_side="По направлению",
|
||||
side="Buy",
|
||||
@@ -267,7 +267,7 @@ async def set_trade_mode(tg_id: int, trade_mode: str) -> bool:
|
||||
# Creating new record
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
trade_mode=trade_mode,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_additional_settings)
|
||||
|
||||
@@ -306,7 +306,7 @@ async def set_margin_type(tg_id: int, margin_type: str) -> bool:
|
||||
# Creating new record
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
margin_type=margin_type,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_additional_settings)
|
||||
|
||||
@@ -345,7 +345,7 @@ async def set_switch_side(tg_id: int, switch_side: str) -> bool:
|
||||
# Creating new record
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
switch_side=switch_side,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_additional_settings)
|
||||
|
||||
@@ -384,7 +384,7 @@ async def set_side(tg_id: int, side: str) -> bool:
|
||||
# Creating new record
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
side=side,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_additional_settings)
|
||||
|
||||
@@ -423,7 +423,7 @@ async def set_leverage(tg_id: int, leverage: str) -> bool:
|
||||
# Creating new record
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
leverage=leverage,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_additional_settings)
|
||||
|
||||
@@ -462,7 +462,7 @@ async def set_order_quantity(tg_id: int, order_quantity: float) -> bool:
|
||||
# Creating new record
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
order_quantity=order_quantity,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_additional_settings)
|
||||
|
||||
@@ -503,7 +503,7 @@ async def set_martingale_factor(tg_id: int, martingale_factor: float) -> bool:
|
||||
# Creating new record
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
martingale_factor=martingale_factor,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_additional_settings)
|
||||
|
||||
@@ -546,7 +546,7 @@ async def set_max_bets_in_series(tg_id: int, max_bets_in_series: int) -> bool:
|
||||
# Creating new record
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
max_bets_in_series=max_bets_in_series,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_additional_settings)
|
||||
|
||||
@@ -587,7 +587,7 @@ async def set_trigger_price(tg_id: int, trigger_price: float) -> bool:
|
||||
# Creating new record
|
||||
user_additional_settings = UserAdditionalSettings(
|
||||
trigger_price=trigger_price,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_additional_settings)
|
||||
|
||||
@@ -627,7 +627,7 @@ async def create_user_risk_management(tg_id: int) -> None:
|
||||
|
||||
# Create the user risk management
|
||||
user_risk_management = UserRiskManagement(
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
take_profit_percent=1.0,
|
||||
stop_loss_percent=1.0,
|
||||
commission_fee="Yes_commission_fee",
|
||||
@@ -692,7 +692,7 @@ async def set_take_profit_percent(tg_id: int, take_profit_percent: float) -> boo
|
||||
# Creating new record
|
||||
user_risk_management = UserRiskManagement(
|
||||
take_profit_percent=take_profit_percent,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_risk_management)
|
||||
|
||||
@@ -733,7 +733,7 @@ async def set_stop_loss_percent(tg_id: int, stop_loss_percent: float) -> bool:
|
||||
# Creating new record
|
||||
user_risk_management = UserRiskManagement(
|
||||
stop_loss_percent=stop_loss_percent,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_risk_management)
|
||||
|
||||
@@ -774,7 +774,7 @@ async def set_commission_fee(tg_id: int, commission_fee: str) -> bool:
|
||||
# Creating new record
|
||||
user_risk_management = UserRiskManagement(
|
||||
commission_fee=commission_fee,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_risk_management)
|
||||
|
||||
@@ -815,7 +815,7 @@ async def set_commission_place(tg_id: int, commission_place: str) -> bool:
|
||||
# Creating new record
|
||||
user_risk_management = UserRiskManagement(
|
||||
commission_place=commission_place,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_risk_management)
|
||||
|
||||
@@ -855,7 +855,7 @@ async def create_user_conditional_settings(tg_id: int) -> None:
|
||||
|
||||
# Create the user conditional settings
|
||||
user_conditional_settings = UserConditionalSettings(
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
timer_start=0,
|
||||
timer_end=0,
|
||||
)
|
||||
@@ -920,7 +920,7 @@ async def set_start_timer(tg_id: int, timer_start: int) -> bool:
|
||||
# Creating new record
|
||||
user_conditional_settings = UserConditionalSettings(
|
||||
timer_start=timer_start,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_conditional_settings)
|
||||
|
||||
@@ -959,7 +959,7 @@ async def set_stop_timer(tg_id: int, timer_end: int) -> bool:
|
||||
# Creating new record
|
||||
user_conditional_settings = UserConditionalSettings(
|
||||
timer_end=timer_end,
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
)
|
||||
session.add(user_conditional_settings)
|
||||
|
||||
@@ -1051,7 +1051,7 @@ async def set_user_deal(
|
||||
else:
|
||||
# Creating new record
|
||||
new_deal = UserDeals(
|
||||
user=user,
|
||||
user_id=user.id,
|
||||
symbol=symbol,
|
||||
current_step=current_step,
|
||||
current_series=current_series,
|
||||
|
||||
Reference in New Issue
Block a user