now tracks unused enums correctly

This commit is contained in:
Robert Lechte 2016-08-09 23:44:54 +12:00
parent 4937c5776b
commit 7559a377e1
6 changed files with 46 additions and 17 deletions

View File

@ -46,14 +46,20 @@ def statements_for_changes(
return statements
def get_enum_modifications(tables_from, tables_target):
def get_enum_modifications(
tables_from,
tables_target,
enums_from,
enums_target):
_, _, e_modified, _ = differences(enums_from, enums_target)
_, _, t_modified, _ = differences(tables_from, tables_target)
pre = Statements()
recreate = Statements()
post = Statements()
enums_to_change = {}
enums_to_change = e_modified
for t, v in t_modified.items():
t_before = tables_from[t]
@ -65,7 +71,6 @@ def get_enum_modifications(tables_from, tables_target):
if c.is_enum == before.is_enum and c.dbtypestr == before.dbtypestr and c.enum != before.enum:
pre.append(before.change_enum_to_string_statement(t))
enums_to_change[c.enum.name] = c.enum
post.append(before.change_string_to_enum_statement(t))
for e in enums_to_change.values():
@ -75,7 +80,12 @@ def get_enum_modifications(tables_from, tables_target):
return pre + recreate + post
def get_schema_changes(tables_from, tables_target):
def get_schema_changes(
tables_from,
tables_target,
enums_from,
enums_target):
added, removed, modified, _ = differences(tables_from, tables_target)
statements = Statements()
@ -92,7 +102,7 @@ def get_schema_changes(tables_from, tables_target):
for t, v in added.items():
statements += [c.create_statement for c in v.constraints.values()]
statements += get_enum_modifications(tables_from, tables_target)
statements += get_enum_modifications(tables_from, tables_target, enums_from, enums_target)
for t, v in modified.items():
before = tables_from[t]
@ -126,7 +136,9 @@ class Changes(object):
return partial(
get_schema_changes,
self.i_from.tables,
self.i_target.tables)
self.i_target.tables,
self.i_from.enums,
self.i_target.enums)
elif name in THINGS:
return partial(
statements_for_changes,

View File

@ -44,7 +44,7 @@ class Migration(object):
self.statements.safe = safety_on
def add_all_changes(self):
self.add(self.changes.enums(modifications=False))
self.add(self.changes.enums(creations_only=True, modifications=False))
self.add(self.changes.sequences(creations_only=True))
@ -60,6 +60,8 @@ class Migration(object):
self.add(self.changes.views(creations_only=True))
self.add(self.changes.functions(creations_only=True))
self.add(self.changes.enums(drops_only=True, modifications=False))
@property
def sql(self):
return self.statements.sql

View File

@ -8,7 +8,7 @@ readme = io.open('README.rst').read()
setup(
name='migra',
version='0.1.1470583937',
version='0.1.1470742990',
url='https://github.com/djrobstep/migra',
description='Like diff but for PostgreSQL schemas',
long_description=readme,

View File

@ -2,6 +2,12 @@ create extension pg_trgm;
CREATE TYPE shipping_status AS ENUM ('not shipped', 'shipped');
CREATE TYPE unwanted_enum AS ENUM ('unwanted', 'not wanted');
CREATE TYPE unused_enum AS ENUM ('a', 'b');
CREATE TYPE usage_dropped_enum AS ENUM ('x', 'y');
CREATE TABLE products (
product_no integer PRIMARY KEY,
name varchar(10) not null unique,
@ -18,7 +24,8 @@ create view vvv as select * from products;
CREATE TABLE orders (
order_id serial primary key,
shipping_address text,
status shipping_status
status shipping_status,
status2 usage_dropped_enum
);
CREATE TABLE unwanted (
@ -36,5 +43,3 @@ $$
$$
LANGUAGE PLPGSQL STABLE returns null on null input security definer;
CREATE TYPE unwanted_enum AS ENUM ('unwanted', 'not wanted');

View File

@ -3,6 +3,12 @@ create extension postgis;
CREATE TYPE shipping_status AS ENUM ('not shipped', 'shipped', 'delivered');
CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
CREATE TYPE unused_enum AS ENUM ('a', 'b', 'c');
CREATE TYPE usage_dropped_enum AS ENUM ('x', 'y');
CREATE TABLE products (
product_no serial primary key,
name text,
@ -19,7 +25,8 @@ create index on products(name);
CREATE TABLE orders (
order_id integer primary key unique,
shipping_address text,
status shipping_status
status shipping_status,
status2 text
);
CREATE TABLE order_items (
@ -53,9 +60,6 @@ LANGUAGE PLPGSQL STABLE returns null on null input security invoker;
create view vvv as select 2;
CREATE TYPE bug_status AS ENUM ('new', 'open', 'closed');
CREATE TABLE bug (
id serial,
description text,

View File

@ -1,5 +1,3 @@
drop type "public"."unwanted_enum";
create type "public"."bug_status" as enum ('new', 'open', 'closed');
create sequence "public"."bug_id_seq";
@ -42,10 +40,16 @@ drop type "public"."shipping_status";
create type "public"."shipping_status" as enum ('not shipped', 'shipped', 'delivered');
drop type "public"."unused_enum";
create type "public"."unused_enum" as enum ('a', 'b', 'c');
alter table "public"."orders" alter column "status" set data type shipping_status using "status"::shipping_status;
alter table "public"."orders" alter column "order_id" drop default;
alter table "public"."orders" alter column "status2" set data type text;
alter table "public"."products" drop column "oldcolumn";
alter table "public"."products" add column "newcolumn" text;
@ -112,3 +116,5 @@ $$
$$
language PLPGSQL VOLATILE RETURNS NULL ON NULL INPUT SECURITY DEFINER;
drop type "public"."unwanted_enum";