create index if not exist to prevent duplicate error #180
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When using the PDO method of storage for data, several tables are created if they do not already exist, and for pgsql and sqlite users, a 'name' index is also created, but no check is done to ensure that the index does not already exist; this can result in an error such as the following when called from a singleton:
SQLSTATE[42P07]: Duplicate table: 7 ERROR: relation "name" already existsIn this particular use-case, each /api request to the local application triggers a recording of metrics to the database to be eventually scraped by Prometheus using
CollectorRegistrysingleton, which requires aPDOinstance also in a singleton.(PDOimplementsAdapter, which is a required argument for theCollectorRegistryconstructor.)The now stored metrics must persist between api calls and persist between singletons, so that Prometheus can scrape it all at once, but each individual singleton as a result of these /api calls tries to create the 'name' index, causing the error, since the very first singleton already created these tables/index to store the metrics for future singles to also write to until Prometheus scrapes it, and flushes the data using PDO's wipeStorage().
This small branch aims to fix that by adding a check to both pgsql and sqlite cases.