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

Add support for topological sorting of tables and aggregate function changes #237

Open
wants to merge 15 commits into
base: master
Choose a base branch
from

Conversation

joshainglis
Copy link

This pull request builds on the changes made in djrobstep/schemainspect#90 to add a few enhancements to migra:

  1. Topological sorting of tables
    When generating the SQL statements to create tables, migra now takes into account the full dependency tree between tables. It builds a dependency graph using the new dependents attribute added to InspectedTable in schemainspect. It then sorts the tables topologically based on this graph. This now handles cases where a column is an array of a composite type.

  2. Support for changes to aggregate functions
    The get_selectable_changes function in changes.py was updated to include aggregate functions (relationtype 'a') in addition to regular functions when generating SQL statements for function changes.

This change utilizes the new InspectedAggFunction class and related functionality added in schemainspect to make migra aware of aggregate functions and include them in schema comparisons and migrations.

  1. Support for comments
    The Changes class has a new comments property that returns a partial function for generating SQL statements for changes to comments on database objects. The add_all_changes method was updated to include creating and dropping comments at the appropriate points in the migration process.

This integrates with the new comment inspection capabilities added in schemainspect, allowing migra to include comment changes as part of schema migrations.

With these changes, migra is now able to generate more accurate migration scripts, particularly in cases involving table dependencies, aggregate functions, and object comments. It can produce migrations that run correctly on the first try in more cases than before.

Please let me know if you have any questions or feedback on these changes. I'm happy to discuss further or make revisions as needed.

joshainglis and others added 14 commits June 29, 2022 08:46
# Conflicts:
#	poetry.lock
#	pyproject.toml
# Conflicts:
#	poetry.lock
#	pyproject.toml
Updated pyproject.toml and poetry.lock files to switch from psycopg2-binary to psycopg2 for postgres database functionality. The psycopg2-binary package is a stand-alone package that's meant for easy installation purposes and not recommended for production environments. For production-ready functionality and better maintainability, we switched to psycopg2. Accordingly, references to psycopg2-binary were replaced with psycopg2, and binary files related with psycopg2-binary were removed from poetry.lock.
pyproject.toml Outdated
# schemainspect = {path="../schemainspect", develop=true}
schemainspect = ">=3.1.1663480743"
#schemainspect = { path = "../schemainspect", develop = true }
schemainspect = { git = "https://github.com/joshainglis/schemainspect.git", rev = "79b3fbdb9c22252574578e8f83dcb7f85ebaf69a" }
Copy link
Author

Choose a reason for hiding this comment

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

Will need to update this prior to merging if djrobstep/schemainspect#90 gets merged

@sgtsquiggs
Copy link

Is there a pre-built docker image available to test?

@joshainglis
Copy link
Author

@sgtsquiggs Good idea, just pushed one up. Try docker pull joshainglis/migra:upstream-pr

@joshainglis
Copy link
Author

Also added a few more improvements to schemainspect:

  • include function return type to function identity so functions will be deleted and recreted if the return type is changed.
  • handle an edge case where a table can have all it's columns removed via a DROP CASCADE.

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

2 participants