a
    i                     @   s   U d dl mZ d dlmZmZmZ d dlmZmZ d dl	m
Z
 d dlmZ d dlmZ d dlmZ eejdd	Zeed
< e
eeddZeeeddddZddddZddddZddddZeedf dddZdS )    )AsyncGenerator)deletetextupdate)AsyncEnginecreate_async_engine)sessionmaker)SQLModel)AsyncSession)settingsF)echoengine)class_Zexpire_on_commitN)
table_namecolumn_name
column_sqlreturnc                    sV   |  d| dI d H }dd | D }||v r6d S |  d| d| I d H  d S )NzPRAGMA table_info('z')c                 S   s   h | ]}t |d  qS )   )str).0row r   <C:\Users\jasmi\Projects\line-hihaho-voting\backend\app\db.py	<setcomp>       z)_add_column_if_missing.<locals>.<setcomp>zALTER TABLE z ADD COLUMN )Zexec_driver_sqlZfetchall)connr   r   r   rowsZexisting_columnsr   r   r   _add_column_if_missing   s
    r   )r   c                    s  t tjdsd S t| dddI d H  t| dddI d H  t| dddI d H  t| d	d
dI d H  t| d	ddI d H  t| d	ddI d H  t| d	ddI d H  t| d	ddI d H  t| d	ddI d H  | tdI d H  | tdI d H  | tdI d H  d S )NZsqlitepollwinner_sourcezwinner_source TEXT DEFAULT ''Zconfig_jsonzconfig_json TEXT DEFAULT '{}'runtime_jsonzruntime_json TEXT DEFAULT '{}'ZlineeventrecordZraw_jsonzraw_json TEXT DEFAULT ''room_idzroom_id TEXT DEFAULT ''Zparticipant_idzparticipant_id TEXT DEFAULT ''Zline_user_id_hashz!line_user_id_hash TEXT DEFAULT ''Zreply_statuszreply_status TEXT DEFAULT ''Zreply_errorzreply_error TEXT DEFAULT ''zRCREATE INDEX IF NOT EXISTS ix_lineeventrecord_room_id ON lineeventrecord (room_id)z`CREATE INDEX IF NOT EXISTS ix_lineeventrecord_participant_id ON lineeventrecord (participant_id)zfCREATE INDEX IF NOT EXISTS ix_lineeventrecord_line_user_id_hash ON lineeventrecord (line_user_id_hash))r   r   database_url
startswithr   Zexecuter   )r   r   r   r   _run_sqlite_migrations   s,    

r$   c               	      sn   ddl m}  t 4 I d H 8}|tjjI d H  t|I d H  W d   I d H  qj1 I d H s`0    Y  d S )Nr   )entities)	Z
app.modelsr%   r   beginrun_syncr	   metadataZ
create_allr$   )r%   r   r   r   r   init_db9   s    r)   c               
      s   ddl m} m}m}m}m}m}m} t 4 I d H }|	t
| I d H  |	t
|I d H  |	t
|I d H  |	t
|I d H  |	t
|I d H  |	t|j|jd dd ddI d H  | I d H  W d   I d H  q1 I d H s0    Y  d S )Nr   )AudienceSession	JoinTokenLineEventRecordParticipantPoll
PollStatusVote z{})statusZwinner_optionr   Zdeadline_atr    )Zapp.models.entitiesr*   r+   r,   r-   r.   r/   r0   async_session_factoryexecr   r   valuesZdraftcommit)r*   r+   r,   r-   r.   r/   r0   sessionr   r   r   reset_runtime_stateA   s"    $
	r8   c               	   C  sD   t  4 I d H } | V  W d   I d H  q@1 I d H s60    Y  d S )N)r3   )r7   r   r   r   get_sessionV   s    r9   )collections.abcr   Z
sqlalchemyr   r   r   Zsqlalchemy.ext.asyncior   r   Zsqlalchemy.ormr   Zsqlmodelr	   Zsqlmodel.ext.asyncio.sessionr
   
app.configr   r"   r   __annotations__r3   r   r   r$   r)   r8   r9   r   r   r   r   <module>   s(   