WIP: Check if table already exists before creating it#158
WIP: Check if table already exists before creating it#158lopuhin wants to merge 2 commits intoscrapinghub:masterfrom
Conversation
If we have SQLALCHEMYBACKEND_DROP_ALL_TABLES = False, then without this fix we will fail to start db and strategy workers with an existing database.
|
This check is not enough to prevent errors during concurrent table creation, at least with postgres. Although this looks more like a sqlalchemy bug: |
|
@lopuhin concurrent table creation is something we should avoid doing. Such a behavior isn't expected by both DB servers and clients. I recommend to redesign your application to avoid doing this. Concurrent truncation of the table should work fine, btw. |
| if model.__table__.name in inspector.get_table_names(): | ||
| model.__table__.drop(bind=b.engine) | ||
| model.__table__.create(bind=b.engine) | ||
| model.__table__.create(bind=b.engine, checkfirst=True) |
There was a problem hiding this comment.
This is happening because SQLA has non-unified behavior between various engines. For example that code works well for sqlite, without need of checkfirst=True. However, I still think it makes sense to merge it.
|
@sibiryakov thanks, I'll try to avoid concurrent table creation, that makes sense! |
|
I would like to test that behavior. What if we'll be testing our backends with |
|
Yep, I'll add the tests (found them now). |
If we have SQLALCHEMYBACKEND_DROP_ALL_TABLES = False, then without this fix we will fail to start db and strategy workers with an existing database, because the tables are already there, but we try to create them.
Potentially, creating the table without checking for existance could cause troubles even with SQLALCHEMYBACKEND_DROP_ALL_TABLES = True, when several workers would try to create required tables concurrently, but I never tried to reproduce this.