-
Notifications
You must be signed in to change notification settings - Fork 2.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multi-tenancy with different schema write in wrong schema for indexes #14036
Comments
With Sql Server but by using different SqlException: The INSERT statement conflicted with the FOREIGN KEY constraint "FK_ContentItemIndex". The conflict occurred in database "oc-dev", table "tenant1.Document", column 'Id'. The INSERT statement conflicted with the FOREIGN KEY constraint "FK_AutoroutePartIndex". The conflict occurred in database "oc-dev", table "tenant1.Document", column 'Id'. The statement has been terminated. The statement has been terminated. I will look at it this night |
Okay, when we differentiate by the table prefix we differentiate the FK, for example
But if we only differentiate by the schema we still have
Same remark for primary keys, the table prefix is used but not the schema. Not sure we can include the schema in the above keys, maybe open an issue in the YesSql repo. In the meantime, while using schemas you still need to differentiate tenants with table prefixes. But then, by using schemas we lose the table prefix conflict checking when creating a tenant. If nothing need to be done on YesSql side we may need to update our tenant validator. |
The index names should be unique. I would have expected the second index creation to fail, or was it just ignored and logged. Is this statements accurate though @jeanbaptistedalle ?
I don't think the record can be created in the wrong table. |
The fix should be done in YesSql because it already prefixes the index names that is passed with the table prefix. It should use the schema too (then). If it was not adding the table prefix then it would have meant that this is the responsibility of the called to make the name unique. It does not. Example: CreateIndex/AlterIndex("IDX_INDEXUSERS") => SCHEMA1_TENANT1_IDX_INDEXUSERS Once the change is done, we can call CreateIndex again in a new migration step. We could do it only if a schema is set. Though I assume that calling it again with an existing name (in case the schema is not used) would be no-op as it did not break today. Note that we don't expect users to have schemas without prefixes and multiple tenants otherwise nothing would work as this issue demonstrates. We might just want to clean things up correctly for the case when both schemas and table prefixes are used (delete old indexes). |
As I understood the problem would not be related to the inner indexes The problem would not be the table name including the schema, but the foreign key (maybe also the primary key) that includes the table prefix (if it exists) but not the
|
@jtkech I think we understand the issue. The following works But What I think he suggests, if to change the command the creates the SQL constraint/Index not sql index table by prepending the schema "if it specified" to the name. So the command will change from In the picture below, you'll notice we do not prefix the SQL index with the site name since we only have so when a schema is used, we'll change Note that we have to do the same thing with the primary key also since there is no prefix. However, we may have a problem when trying to create a primary key index with the new name while another primary key already exists.. but this may not matter since no one really use schema with no prefix since this is the first time we get a report of an issue. |
Okay cool then, but as I remember the exception was explicitly mentioning the foreign key FK, if I have time I will try to change the |
Hummm, just doing some googling and this is what I found. So the same table name and constraint name is unique to the schema.
What we are doing should work. Maybe the issue is that we are not using the schema value is some queues. I have to try to reproduce the issue |
Just did a quick test, after starting the app, it first works with any tenant but then when switching to another one it saves in the right Document table but populate the IndexTable of the previous tenant. Look at the beginning of this command where we can see both [schema1] and [schema2].
When I will have time I'll try to debug directly into YesSql locally, too lazy for now ;) |
In the meantime just saw that some commands are cached.
And then used like this.
So looks like the problem is that the |
The solution would be to use
By allowing |
So the cache is the reason some documents are stored in the wrong index table. Good catch @jtkech |
So the name of the index is fine, and the issue is only with caching, should be easy to fix |
Sorry, I was off, but it depends, sometimes, when the same documentId already exists in the other schema, it does not fail, but the index in the tenant2 is wrong as some data are missing and it lead to others problems (like the user not visible in the User pages as it uses the UserIndex). @jtkech Wow good job, I don't even known that there was cache on that ! |
Sorry for the issue state, miss click on close with comment button ... |
@jtkech is this something you can fix soon? We are looking to release YesSql 3.3.1 very soon to fix another issue for OC 1.7.1. If you are able to push a PR on YesSql to fix this, that would be amazing. |
Okay I will look at it asap. |
Describe the bug
We discovered a bug when using a multi-tenant version of OrchardCore with the same database for each tenant but different schema. For now, I don't know if the issue is only on postgresql or on other kind of database too.
To Reproduce
Steps to reproduce the behavior:
PostgresException: 23503: insert or update on table "ContentItemIndex" violates foreign key constraint "fk_contentitemindex" DETAIL: Detail redacted as it may contain sensitive data. Specify 'Include Error Detail' in the connection string to include this information.
We think that, somewhere, there is a mismatch between the schema, maybe with "OrchardCore.Data.IndexServiceCollectionExtensions.AddIndexProvider" but we're not sure that's the rootcause for now.
Expected behavior
Each tenant successfully save the Content Item in Document and in Indexes in the correct schema.
Screenshots
If applicable, add screenshots to help explain your problem.
The text was updated successfully, but these errors were encountered: