Skip to content
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

[MM-60328] include proper search_path check to post-migrate #26

Merged
merged 3 commits into from
Oct 24, 2024

Conversation

isacikgoz
Copy link
Member

Summary

After every migration, we should advise running migration-assist post-migrate command to determine the health of the DB in terms of schema and it's ownership.

Also make index creation optional. Will add documentation accordingly.

Ticket Link

https://mattermost.atlassian.net/browse/MM-60328

@isacikgoz isacikgoz added the 2: Dev Review Requires review by a core committer label Sep 26, 2024
@galtom
Copy link

galtom commented Sep 27, 2024

This "bug" affected me, and after hours of search I found the solution.
Whet the migration finished I tried to start Mattermost, but it is failed because SELECT CURRENT_SCHEMA(); SQL command returned nothing:

mattermost=> SELECT CURRENT_SCHEMA();
 current_schema
----------------

(1 row)

The SEARCH_PATH looked liked this:

mattermost=> SHOW SEARCH_PATH;
     search_path
---------------------
 """$user"", public"
(1 row)

But it is wrong. To make it work, I had to set by hand the it as postgres user:

ALTER ROLE mmuser SET search_path = "$user", public;

After that, this is what the SEARCH_PATH and CURRENT_SCHEMA() looked like:

mattermost=# SELECT CURRENT_SCHEMA();
 current_schema
----------------
 public
(1 row)

mattermost=# SHOW SEARCH_PATH;
   search_path
-----------------
 "$user", public
(1 row)

Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From my understanding, https://mattermost.atlassian.net/browse/MM-60328 was about restoring the schema_search path in case the command failed to run successfully. But this PR just adds a post-migrate check correct?

Can we do something so that the search_path gets restored correctly in case of failure? If not, clearly mention that "command has failed, search_path could not be restored, please reset it manually" or something like that?

@isacikgoz
Copy link
Member Author

@agnivade Since migration-assist itself is not running pgloader, the idea here is to use migration-assist post-migrate as a safety check, after pgloader runs. That's why I made the index creation rely on the CLI flag. There will be a doc update to recommend checking whether they need to look into fixes. But I think new CheckPostgresSchemaOwnership implementation should fix the issue regardless.

if len(schemas) == 0 {
return fmt.Errorf("no value available for search_path")
} else if _, ok := slices.BinarySearch(schemas, schema); !ok {
} else if _, ok := slices.BinarySearch(schemas, fmt.Sprintf("%q, %s", "$user", schema)); !ok {
logger.Printf("could not find the default schema %q in search_path, consider setting it from the postgresql console\n", schema)
err := db.ExecQuery(ctx, fmt.Sprintf("SELECT pg_catalog.set_config('search_path', '\"$user\", %s', false)", schema))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think now we can refactor both these strings into a single declaration above. Because we can use %q to avoid the quote escaping.

@agnivade
Copy link
Member

That's why I made the index creation rely on the CLI flag.

So to clarify: this schema check is only needed to run the index creation? I thought this was there as a sanity check to run Mattermost later?

@isacikgoz
Copy link
Member Author

@agnivade nope, I mean this command will run (at least we'll advise that) after whenever a pgloader execution happens.

Copy link
Member

@agnivade agnivade left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Resolved offline

@isacikgoz isacikgoz added 3: Reviews Complete All reviewers have approved the pull request and removed 2: Dev Review Requires review by a core committer labels Oct 24, 2024
@isacikgoz isacikgoz merged commit 00aa73c into main Oct 24, 2024
1 check passed
@isacikgoz isacikgoz deleted the MM-60328 branch October 24, 2024 14:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3: Reviews Complete All reviewers have approved the pull request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants