This commit is contained in:
Josha Inglis 2024-05-14 11:28:07 +10:00 committed by GitHub
commit 83a4523419
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 462 additions and 523 deletions

View File

@ -3,6 +3,8 @@ from __future__ import unicode_literals
from collections import OrderedDict as od
from functools import partial
from networkx import lexicographical_topological_sort, DiGraph
import schemainspect
from .statements import Statements
@ -21,6 +23,7 @@ THINGS = [
"collations",
"rlspolicies",
"triggers",
"comments",
]
PK = "PRIMARY KEY"
@ -227,11 +230,23 @@ def get_table_changes(
statements += enums_pre
for t, v in added.items():
statements.append(v.create_statement)
if v.rowsecurity:
rls_alter = v.alter_rls_statement
statements.append(rls_alter)
# topologial sort of tables using table.dependents
# this is to ensure that tables are created in the correct order
# so that foreign keys can be created
G = DiGraph()
G.add_nodes_from(tables_target.keys())
for t, v in tables_target.items():
G.add_edges_from((t, d) for d in v.dependents)
tables_sorted = {k: tables_target[k] for k in lexicographical_topological_sort(G) if k in tables_target}
for t, v in tables_sorted.items():
if t in added:
statements.append(v.create_statement)
if v.rowsecurity:
rls_alter = v.alter_rls_statement
statements.append(rls_alter)
statements += enums_post
@ -365,7 +380,6 @@ def get_selectable_differences(
not_replaceable = set()
if add_dependents_for_modified:
for k, m in changed_all.items():
old = selectables_from[k]
@ -478,7 +492,7 @@ def get_selectable_changes(
statements = Statements()
def functions(d):
return {k: v for k, v in d.items() if v.relationtype == "f"}
return {k: v for k, v in d.items() if v.relationtype in ("f", "a")}
if not tables_only:
if not creations_only:
@ -656,6 +670,15 @@ class Changes(object):
modifications=False,
)
@property
def comments(self):
return partial(
statements_for_changes,
self.i_from.comments,
self.i_target.comments,
modifications=False,
)
def __getattr__(self, name):
if name in THINGS:
return partial(

View File

@ -85,6 +85,7 @@ class Migration(object):
self.add(self.changes.extensions(drops_only=True))
def add_all_changes(self, privileges=False):
self.add(self.changes.comments(drops_only=True))
self.add(self.changes.schemas(creations_only=True))
self.add(self.changes.extensions(creations_only=True, modifications=False))
@ -122,6 +123,7 @@ class Migration(object):
self.add(self.changes.triggers(creations_only=True))
self.add(self.changes.collations(drops_only=True))
self.add(self.changes.schemas(drops_only=True))
self.add(self.changes.comments(creations_only=True))
@property
def sql(self):

939
poetry.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -10,12 +10,13 @@ repository = "https://github.com/djrobstep/migra"
homepage = "https://databaseci.com/docs/migra"
[tool.poetry.dependencies]
python = ">=3.7,<4"
python = ">=3.8,<4"
sqlbag = "*"
six = "*"
# schemainspect = {path="../schemainspect", develop=true}
schemainspect = ">=3.1.1663480743"
#schemainspect = { path = "../schemainspect", develop = true }
schemainspect = { git = "https://github.com/joshainglis/schemainspect.git", rev = "eeda5c18d26fce006a826cfe63f8683b4490a321" }
psycopg2-binary = { version="*", optional = true }
networkx = "^3.1"
[tool.poetry.dev-dependencies]
sqlbag = "*"