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

postgresql to aurora db migration fix #392

Merged

Conversation

ole4ryb
Copy link

@ole4ryb ole4ryb commented Feb 6, 2023

No description provided.

@CLAassistant
Copy link

CLAassistant commented Feb 6, 2023

CLA assistant check
All committers have signed the CLA.

@joe-crawford joe-crawford self-requested a review February 6, 2023 15:58
Copy link
Contributor

@joe-crawford joe-crawford left a comment

Choose a reason for hiding this comment

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

I think there are a few issues, could you have a look at the below?

  • The triggers are used to enforce the constraints (foreign keys etc.) in the DB so it can definitely cause a problem deleting without disabling them, I've tried on a test DB and got:
DELETE FROM core.breadcrumb_tree
[2023-02-06 21:19:04] [23503] ERROR: update or delete on table "breadcrumb_tree" violates foreign key constraint "fksiu83nftgdvb7kdvaik9fghsj" on table "data_type"
[2023-02-06 21:19:04] Detail: Key (id)=(8f7aed00-1a0d-4f47-9c9b-3b4230a5ad6e) is still referenced from table "data_type".
  • Deleting the schema entry will mean it runs again on already migrated DBs which isn't ideal as BreadcrumbTrees have been a sensitive area in the past, it would be best to update the checksum by using the flyway CLI (flyway repair) to get the updated checksum for the file.
  • Looks like a typo in the version number (5.1.3)

@joe-crawford
Copy link
Contributor

If old database data won't be imported into Aurora, maybe you could use a conditional to exclude this rebuild/delete of the BreadcrumbTrees from running on Aurora at all?

@joe-crawford
Copy link
Contributor

Thanks @oleksiiRybak, I haven't tested this myself yet but I have some comments.

@joe-crawford
Copy link
Contributor

joe-crawford commented Feb 7, 2023

If you do need a solution for Aurora and Postgres DBs with existing data in the future, I think what should work is using the information tables to look up the names of the constraints from foreign keys etc., and then use dynamic (plpg)SQL to disable/enable them by name, which should avoid the superuser issue.

@ole4ryb
Copy link
Author

ole4ryb commented Feb 8, 2023

@joe-crawford I've done some further changes, please review them...

Copy link
Contributor

@joe-crawford joe-crawford left a comment

Choose a reason for hiding this comment

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

Using session_replication_role looks good if that works on Aurora too.

However the migration will fail on existing Postgres DBs due to the index already being created, could you change the DELETE flyway entry to UPDATE with the checksum?

Could you also have a look at the formatting comment.

Thanks 👍

Comment on lines 11 to 14
FROM core.flyway_schema_history
WHERE version = '5.1.3' AND
description = 'delete invalid breadcrumb trees';
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you use the flyway CLI to calculate the checksum and turn this into an update like the one at the top of the file? This will make sure the migration doesn't run on previously migrated systems, which is safer.

Copy link
Author

Choose a reason for hiding this comment

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

@joe-crawford I've added an additional protection in case we run create index in a db which already has it and similarly with the temporary valid_breadcrumb_trees_temp so it shouldn't cause any problems so I think it is OK to go with delete in this case.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think it should still be UPDATE as rerunning the migrations in general isn't really safe, and if it fails it stops the system from starting. In this case it will still cause problems on existing Postgres DBs because the temporary table is dropped per session so it will still try to insert rows.

Copy link
Author

Choose a reason for hiding this comment

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

ok, I will look into that. thanks

Copy link
Contributor

Choose a reason for hiding this comment

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

From memory you have to install the flyway CLI, run flyway repair and then that updates the value in the flyway_schema_migration table with the new checksum.

Copy link
Author

Choose a reason for hiding this comment

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

thank you, looking into that...

Comment on lines +18 to +37
DO
$$
BEGIN
IF EXISTS (SELECT aurora_version()) THEN
SET session_replication_role = replica;
DELETE FROM core.breadcrumb_tree;
SET session_replication_role = DEFAULT;
ELSE
ALTER TABLE core.breadcrumb_tree DISABLE TRIGGER ALL;
DELETE FROM core.breadcrumb_tree;
ALTER TABLE core.breadcrumb_tree ENABLE TRIGGER ALL;
END IF;

exception
when undefined_function then
ALTER TABLE core.breadcrumb_tree DISABLE TRIGGER ALL;
DELETE FROM core.breadcrumb_tree;
ALTER TABLE core.breadcrumb_tree ENABLE TRIGGER ALL;
END
$$;
Copy link
Contributor

Choose a reason for hiding this comment

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

This looks good to me and I have tested this on Postgres.

@joe-crawford
Copy link
Contributor

Great, this looks good to me.

Have tested on a Postgres instance with existing data and a new blank Postgres database, both successful.

@joe-crawford joe-crawford merged commit 30a19bd into MauroDataMapper:develop Feb 9, 2023
@ole4ryb ole4ryb deleted the postgres-to-auroradb-migration branch February 10, 2023 11:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants