import asyncio
from sqlalchemy.ext.asyncio import create_async_engine
from sqlalchemy import text

engine = create_async_engine('mysql+aiomysql://root:@localhost:3306/cems_db')

async def run():
    async with engine.begin() as conn:
        try:
            await conn.execute(text('ALTER TABLE material_tb ADD COLUMN available_stock DECIMAL(15,3) DEFAULT 0.000'))
            print('Added column available_stock')
        except Exception as e:
            print(f'Column might already exist: {e}')
        
        await conn.execute(text('UPDATE material_tb SET available_stock = quantity WHERE available_stock IS NULL OR available_stock = 0'))
        print('Updated available_stock to quantity')

asyncio.run(run())
