comparison sat/memory/sqla.py @ 3583:16ade4ad63f3 sqlalchemy

core (memory/sqla_mapping): fix some technical debt: a first Alembic revision file is joined to migrate existing databases to new mapping. This patch: - set `nullable=False` in various places - drop legacy `message_types` table, and use `Enum` instead - use naming convention, and get rid of anonymous constraint (this is the main reason why the revision file is so long)
author Goffi <goffi@goffi.org>
date Sun, 27 Jun 2021 00:15:40 +0200
parents 71516731d0aa
children 7510648e8e3a
comparison
equal deleted inserted replaced
3582:71516731d0aa 3583:16ade4ad63f3
147 log.info("Database is now up-to-date") 147 log.info("Database is now up-to-date")
148 148
149 @aio 149 @aio
150 async def initialise(self) -> None: 150 async def initialise(self) -> None:
151 log.info(_("Connecting database")) 151 log.info(_("Connecting database"))
152
152 db_config = sqla_config.getDbConfig() 153 db_config = sqla_config.getDbConfig()
153 engine = create_async_engine( 154 engine = create_async_engine(
154 db_config["url"], 155 db_config["url"],
155 future=True 156 future=True
156 ) 157 )
157 self.session = sessionmaker( 158
158 engine, expire_on_commit=False, class_=AsyncSession
159 )
160 new_base = not db_config["path"].exists() 159 new_base = not db_config["path"].exists()
161 if new_base: 160 if new_base:
162 log.info(_("The database is new, creating the tables")) 161 log.info(_("The database is new, creating the tables"))
163 await self.createDB(engine, db_config) 162 await self.createDB(engine, db_config)
164 else: 163 else:
165 await self.checkAndUpdateDB(engine, db_config) 164 await self.checkAndUpdateDB(engine, db_config)
165
166 self.session = sessionmaker(
167 engine, expire_on_commit=False, class_=AsyncSession
168 )
166 169
167 async with self.session() as session: 170 async with self.session() as session:
168 result = await session.execute(select(Profile)) 171 result = await session.execute(select(Profile))
169 for p in result.scalars(): 172 for p in result.scalars():
170 self.profiles[p.name] = p.id 173 self.profiles[p.name] = p.id