mirror of
https://github.com/djrobstep/migra.git
synced 2024-09-11 13:56:05 +03:00
inheritance fix
This commit is contained in:
parent
6e44704025
commit
3e664b3031
@ -243,7 +243,13 @@ def get_table_changes(
|
|||||||
if v.parent_table != before.parent_table:
|
if v.parent_table != before.parent_table:
|
||||||
statements += v.attach_detach_statements(before)
|
statements += v.attach_detach_statements(before)
|
||||||
|
|
||||||
for t, v in modified.items():
|
modified_order = list(modified.keys())
|
||||||
|
|
||||||
|
modified_order.sort(key=lambda x: modified[x].is_inheritance_child_table)
|
||||||
|
|
||||||
|
for t in modified_order:
|
||||||
|
v = modified[t]
|
||||||
|
|
||||||
before = tables_from[t]
|
before = tables_from[t]
|
||||||
|
|
||||||
if not v.is_alterable:
|
if not v.is_alterable:
|
||||||
@ -257,10 +263,19 @@ def get_table_changes(
|
|||||||
|
|
||||||
# there's no way to alter a table into/out of generated state
|
# there's no way to alter a table into/out of generated state
|
||||||
# so you gotta drop/recreate
|
# so you gotta drop/recreate
|
||||||
if c.is_generated != c_before.is_generated:
|
|
||||||
|
generated_status_changed = c.is_generated != c_before.is_generated
|
||||||
|
|
||||||
|
inheritance_status_changed = c.is_inherited != c_before.is_inherited
|
||||||
|
|
||||||
|
if inheritance_status_changed or generated_status_changed:
|
||||||
del c_modified[k]
|
del c_modified[k]
|
||||||
c_added[k] = c
|
|
||||||
c_removed[k] = c_before
|
if not c_before.is_inherited:
|
||||||
|
c_removed[k] = c_before
|
||||||
|
|
||||||
|
if not c.is_inherited:
|
||||||
|
c_added[k] = c
|
||||||
|
|
||||||
for k, c in c_removed.items():
|
for k, c in c_removed.items():
|
||||||
alter = v.alter_table_statement(c.drop_column_clause)
|
alter = v.alter_table_statement(c.drop_column_clause)
|
||||||
@ -269,7 +284,8 @@ def get_table_changes(
|
|||||||
alter = v.alter_table_statement(c.add_column_clause)
|
alter = v.alter_table_statement(c.add_column_clause)
|
||||||
statements.append(alter)
|
statements.append(alter)
|
||||||
for k, c in c_modified.items():
|
for k, c in c_modified.items():
|
||||||
statements += c.alter_table_statements(before.columns[k], t)
|
c_before = before.columns[k]
|
||||||
|
statements += c.alter_table_statements(c_before, t)
|
||||||
|
|
||||||
if v.rowsecurity != before.rowsecurity:
|
if v.rowsecurity != before.rowsecurity:
|
||||||
rls_alter = v.alter_rls_statement
|
rls_alter = v.alter_rls_statement
|
||||||
|
@ -1,8 +1,9 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
from schemainspect import DBInspector, get_inspector
|
|
||||||
from sqlbag import raw_execute
|
from sqlbag import raw_execute
|
||||||
|
|
||||||
|
from schemainspect import DBInspector, get_inspector
|
||||||
|
|
||||||
from .changes import Changes
|
from .changes import Changes
|
||||||
from .statements import Statements
|
from .statements import Statements
|
||||||
|
|
||||||
|
@ -14,7 +14,7 @@ python = "*"
|
|||||||
sqlbag = "*"
|
sqlbag = "*"
|
||||||
six = "*"
|
six = "*"
|
||||||
# schemainspect = {path="../schemainspect"}
|
# schemainspect = {path="../schemainspect"}
|
||||||
schemainspect = ">=0.1.1602274168"
|
schemainspect = ">=0.1.1603009162"
|
||||||
psycopg2-binary = { version="*", optional = true }
|
psycopg2-binary = { version="*", optional = true }
|
||||||
|
|
||||||
[tool.poetry.dev-dependencies]
|
[tool.poetry.dev-dependencies]
|
||||||
|
4
tests/FIXTURES/inherit2/a.sql
Normal file
4
tests/FIXTURES/inherit2/a.sql
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
|
||||||
|
create table timestamp_base (created_at timestamp default now(), e integer);
|
||||||
|
|
||||||
|
create table a (a1 integer, a2 integer) inherits (timestamp_base);
|
0
tests/FIXTURES/inherit2/additions.sql
Normal file
0
tests/FIXTURES/inherit2/additions.sql
Normal file
8
tests/FIXTURES/inherit2/b.sql
Normal file
8
tests/FIXTURES/inherit2/b.sql
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
|
||||||
|
create table timestamp_base (created_at timestamp default now());
|
||||||
|
|
||||||
|
create table a (a1 integer, a2 integer) inherits (timestamp_base);
|
||||||
|
|
||||||
|
alter table a drop column a2;
|
||||||
|
|
||||||
|
alter table a add column e integer;
|
7
tests/FIXTURES/inherit2/expected.sql
Normal file
7
tests/FIXTURES/inherit2/expected.sql
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
alter table "public"."timestamp_base" drop column "e";
|
||||||
|
|
||||||
|
alter table "public"."a" drop column "a2";
|
||||||
|
|
||||||
|
alter table "public"."a" add column "e" integer;
|
||||||
|
|
||||||
|
|
0
tests/FIXTURES/inherit2/expected2.sql
Normal file
0
tests/FIXTURES/inherit2/expected2.sql
Normal file
@ -1,14 +1,22 @@
|
|||||||
from __future__ import unicode_literals
|
from __future__ import unicode_literals
|
||||||
|
|
||||||
import io
|
import io
|
||||||
|
from difflib import ndiff as difflib_diff
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
# import yaml
|
||||||
from pytest import raises
|
from pytest import raises
|
||||||
from schemainspect import get_inspector
|
|
||||||
from sqlbag import S, load_sql_from_file, temporary_database
|
from sqlbag import S, load_sql_from_file, temporary_database
|
||||||
|
|
||||||
from migra import Migration, Statements, UnsafeMigrationException
|
from migra import Migration, Statements, UnsafeMigrationException
|
||||||
from migra.command import parse_args, run
|
from migra.command import parse_args, run
|
||||||
|
from schemainspect import get_inspector
|
||||||
|
|
||||||
|
|
||||||
|
def textdiff(a, b):
|
||||||
|
cd = difflib_diff(a.splitlines(), b.splitlines())
|
||||||
|
return "\n" + "\n".join(cd) + "\n"
|
||||||
|
|
||||||
|
|
||||||
SQL = """select 1;
|
SQL = """select 1;
|
||||||
|
|
||||||
@ -50,11 +58,6 @@ def test_identitycols():
|
|||||||
do_fixture_test(FIXTURE_NAME)
|
do_fixture_test(FIXTURE_NAME)
|
||||||
|
|
||||||
|
|
||||||
def test_inherit():
|
|
||||||
for FIXTURE_NAME in ["inherit"]:
|
|
||||||
do_fixture_test(FIXTURE_NAME)
|
|
||||||
|
|
||||||
|
|
||||||
def test_collations():
|
def test_collations():
|
||||||
for FIXTURE_NAME in ["collations"]:
|
for FIXTURE_NAME in ["collations"]:
|
||||||
do_fixture_test(FIXTURE_NAME)
|
do_fixture_test(FIXTURE_NAME)
|
||||||
@ -80,7 +83,16 @@ def test_singleschema_ext():
|
|||||||
do_fixture_test(FIXTURE_NAME, create_extensions_only=True)
|
do_fixture_test(FIXTURE_NAME, create_extensions_only=True)
|
||||||
|
|
||||||
|
|
||||||
fixtures = "everything privileges enumdefaults enumdeps extversions seq".split()
|
fixtures = """\
|
||||||
|
everything
|
||||||
|
privileges
|
||||||
|
enumdefaults
|
||||||
|
enumdeps
|
||||||
|
extversions
|
||||||
|
seq
|
||||||
|
inherit
|
||||||
|
inherit2
|
||||||
|
""".split()
|
||||||
|
|
||||||
# fixtures = [(_, ) for _ in fixtures]
|
# fixtures = [(_, ) for _ in fixtures]
|
||||||
|
|
||||||
@ -199,6 +211,13 @@ def do_fixture_test(
|
|||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
m.add_all_changes(privileges=with_privileges)
|
m.add_all_changes(privileges=with_privileges)
|
||||||
|
|
||||||
|
# y0 = yaml.safe_dump(m.changes.i_from._as_dicts())
|
||||||
|
# y1 = yaml.safe_dump(m.changes.i_target._as_dicts())
|
||||||
|
|
||||||
|
# print(textdiff(y0, y1))
|
||||||
|
# print(m.statements)
|
||||||
|
|
||||||
assert m.changes.i_from == m.changes.i_target
|
assert m.changes.i_from == m.changes.i_target
|
||||||
assert not m.statements # no further statements to apply
|
assert not m.statements # no further statements to apply
|
||||||
assert m.sql == ""
|
assert m.sql == ""
|
||||||
|
Loading…
Reference in New Issue
Block a user