Skip to content

Add --ignore-newlines option #105

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions migra/changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ def get_selectable_changes(
enums_from,
enums_target,
add_dependents_for_modified=True,
ignore_newlines=False
):
tables_from = od((k, v) for k, v in selectables_from.items() if v.is_table)
tables_target = od((k, v) for k, v in selectables_target.items() if v.is_table)
Expand All @@ -212,7 +213,7 @@ def get_selectable_changes(
tables_from, tables_target
)
added_other, removed_other, modified_other, unmodified_other = differences(
other_from, other_target
other_from, other_target, ignore_newlines=ignore_newlines
)

changed_all = {}
Expand Down Expand Up @@ -277,9 +278,10 @@ def functions(d):


class Changes(object):
def __init__(self, i_from, i_target):
def __init__(self, i_from, i_target, ignore_newlines=False):
self.i_from = i_from
self.i_target = i_target
self.ignore_newlines = ignore_newlines

def __getattr__(self, name):
if name == "non_pk_constraints":
Expand All @@ -303,6 +305,7 @@ def __getattr__(self, name):
od(sorted(self.i_target.selectables.items())),
self.i_from.enums,
self.i_target.enums,
ignore_newlines=self.ignore_newlines
)

elif name in THINGS:
Expand Down
9 changes: 8 additions & 1 deletion migra/command.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,13 @@ def parse_args(args):
default=False,
help="Force UTF-8 encoding for output",
)
parser.add_argument(
"--ignore-newlines",
dest="ignore_newlines",
action="store_true",
default=False,
help="Ignore newline characters when comparing stored functions",
)
parser.add_argument("dburl_from", help="The database you want to migrate.")
parser.add_argument(
"dburl_target", help="The database you want to use as the target."
Expand All @@ -69,7 +76,7 @@ def run(args, out=None, err=None):
if not err:
err = sys.stderr # pragma: no cover
with arg_context(args.dburl_from) as ac0, arg_context(args.dburl_target) as ac1:
m = Migration(ac0, ac1, schema=schema)
m = Migration(ac0, ac1, schema=schema, ignore_newlines=args.ignore_newlines)
if args.unsafe:
m.set_safety(False)
if args.create_extensions_only:
Expand Down
4 changes: 2 additions & 2 deletions migra/migra.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ class Migration(object):
The main class of migra
"""

def __init__(self, x_from, x_target, schema=None):
def __init__(self, x_from, x_target, schema=None, ignore_newlines=False):
self.statements = Statements()
self.changes = Changes(None, None)
self.changes = Changes(None, None, ignore_newlines=ignore_newlines)
self.schema = schema
if isinstance(x_from, DBInspector):
self.changes.i_from = x_from
Expand Down
10 changes: 7 additions & 3 deletions migra/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,18 @@
from collections import OrderedDict as od


def differences(a, b, add_dependencies_for_modifications=True):
def differences(a, b, add_dependencies_for_modifications=True, ignore_newlines=True):
a_keys = set(a.keys())
b_keys = set(b.keys())
keys_added = set(b_keys) - set(a_keys)
keys_removed = set(a_keys) - set(b_keys)
keys_common = set(a_keys) & set(b_keys)
added = od((k, b[k]) for k in sorted(keys_added))
removed = od((k, a[k]) for k in sorted(keys_removed))
modified = od((k, b[k]) for k in sorted(keys_common) if a[k] != b[k])
unmodified = od((k, b[k]) for k in sorted(keys_common) if a[k] == b[k])
modified = od(
(k, b[k])
for k in sorted(keys_common)
if not a[k].is_equal(b[k], ignore_newlines=ignore_newlines)
)
unmodified = od((k, b[k]) for k in sorted(keys_common) if a[k].is_equal(b[k], ignore_newlines=ignore_newlines))
return added, removed, modified, unmodified