fix(#193): only create indexes if table does not exist#194
Conversation
dianabarsan
left a comment
There was a problem hiding this comment.
Sweet. One minor change + unit test request.
couch2pg/src/setup.js
Outdated
| // note that `CREATE INDEX IF NOT EXISTS` acquires a lock | ||
| // even if the index exists, so cannot rely on it here, | ||
| // have to actually check if the table exists in a separate query | ||
| const tableExists = await checkTableExists(client); |
There was a problem hiding this comment.
Can return early here instead:
| const tableExists = await checkTableExists(client); | |
| if (await checkTableExists(client)) { | |
| return; | |
| } |
couch2pg/tests/unit/setup.spec.js
Outdated
| end: sinon.stub(), | ||
| }; | ||
|
|
||
| pgClient.query.withArgs(`SELECT 1 FROM v1.whatever LIMIT 1;`).rejects({ |
There was a problem hiding this comment.
Please add a unit test where the table already exists and doesn't get created on startup.
couch2pg/src/setup.js
Outdated
| return true; | ||
| } catch (error) { | ||
| // "Undefined table" error code in PostgreSQL | ||
| if (error.code === '42P01') { |
There was a problem hiding this comment.
I wonder if we should have a library of PG errors somewhere instead of scattered magic strings all over. Do we have other PG errors that we work around in this codebase?
|
looking a bit deeper into postgres locking and there's a simpler way to fix this; using |
dianabarsan
left a comment
There was a problem hiding this comment.
Thanks for spending extra time to read about how locks get triggered in PG! This is absolutely much better!
|
🎉 This PR is included in version 1.3.1 🎉 The release is available on GitHub release Your semantic-release bot 📦🚀 |
closes #193
check if table exists in a separate query before creating indexes