Is there a way to commit migrations before querying indexes? #11834
Unanswered
MikeAlhayek
asked this question in
Q&A
Replies: 1 comment
-
I guess you use DeferredTask when there's schema change |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
I have a module that is called "Teams". This module is responsible of providing team structure, in addition to way to set current team for all user.
This module register the following migration
As you can see, this migration create an index, and updated content definition.
Important This module requires that every user has a default team as all content items filtered based on teams you belong to/have access to. So if you belong to team [A], then you'll see content for users that belong to team [A] and any sub teams of [A]. For this to work property, I have to set the current team for every user when this module is enabled. The logic that I should execute is as follow
TeamIndex
table to see if there is record already exists, and if so pick the first one.UserTeamPart
part by setting theTeamId
property to the default teamId.I thought implementing the
IFeatureEventHandler
would be a perfect place for this logic since I can hook into these events every time the module is installed or enabled and process my logic to ensure that every user has a team. The code will look something like thisThe issue with the code above is, during site-setup, the code is executed in the same sql-transaction as the migration. So the call
_session.Query<User, UserIndex>
will throw an exception as theUserIndex
table won't exists since the sql-transaction is not yet committed.Additionally, since my setup recipe will create default team, I have to execute the above code after the migrations are executed, the recipe finished executing and the code is committed to the database.
Question
How can I ensure the my code above is executed every time a feature is enabled/installed, yet after the migrations are processed?
I thought about calling
_session.SaveChangesAsync()
just before this call_session.Query<ContentItem, TeamIndex>().FirstOrDefaultAsync()
but that did not work. I thought calling_session.SaveChangesAsync()
would first commit all the changes to the database and close the transaction and then process the next set of queries using another transaction. Apparently that isn't what happens here.Beta Was this translation helpful? Give feedback.
All reactions