-
-
Notifications
You must be signed in to change notification settings - Fork 138
Add support for topological sorting of tables and aggregate function changes #237
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
base: master
Are you sure you want to change the base?
Conversation
# 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" } |
There was a problem hiding this comment.
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
Is there a pre-built docker image available to test? |
@sgtsquiggs Good idea, just pushed one up. Try |
Also added a few more improvements to schemainspect:
|
from collections import OrderedDict as od | ||
from functools import partial | ||
|
||
from networkx import lexicographical_topological_sort, DiGraph |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this use TopologicalSorter
which is part of schemainspect
: https://github.com/djrobstep/schemainspect/blob/066262d6fb4668f874925305a0b7dbb3ac866882/schemainspect/graphlib/__init__.py#L38 ?
Also, since I'm not sure migra/schemainspect are maintained. Would you have any interest opening similar PRs for this and djrobstep/schemainspect#90 against https://github.com/mmkal/pgkit? I ported migra and schemainspect to typescript in that repo, and I intend to continue to maintain them.
This pull request builds on the changes made in djrobstep/schemainspect#90 to add a few enhancements to migra:
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 toInspectedTable
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.Support for changes to aggregate functions
The
get_selectable_changes
function inchanges.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.The
Changes
class has a newcomments
property that returns a partial function for generating SQL statements for changes to comments on database objects. Theadd_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.