mirror of
https://github.com/djrobstep/migra.git
synced 2024-10-26 07:00:54 +03:00
fixed/enhanced constraint ordering
This commit is contained in:
parent
ad4f2093d3
commit
dd230660de
@ -3,7 +3,7 @@ from __future__ import unicode_literals
|
||||
from .util import differences
|
||||
from .statements import Statements
|
||||
from functools import partial
|
||||
|
||||
from collections import OrderedDict as od
|
||||
|
||||
THINGS = [
|
||||
'enums',
|
||||
@ -15,6 +15,8 @@ THINGS = [
|
||||
'extensions'
|
||||
]
|
||||
|
||||
PK = 'PRIMARY KEY'
|
||||
|
||||
|
||||
def statements_for_changes(
|
||||
things_from,
|
||||
@ -130,6 +132,22 @@ class Changes(object):
|
||||
self.i_target.tables,
|
||||
self.i_from.enums,
|
||||
self.i_target.enums)
|
||||
elif name == 'non_pk_constraints':
|
||||
a = self.i_from.constraints.items()
|
||||
b = self.i_target.constraints.items()
|
||||
|
||||
a_od = od((k, v) for k, v in a if v.constraint_type != PK)
|
||||
b_od = od((k, v) for k, v in b if v.constraint_type != PK)
|
||||
|
||||
return partial(statements_for_changes, a_od, b_od)
|
||||
elif name == 'pk_constraints':
|
||||
a = self.i_from.constraints.items()
|
||||
b = self.i_target.constraints.items()
|
||||
|
||||
a_od = od((k, v) for k, v in a if v.constraint_type == PK)
|
||||
b_od = od((k, v) for k, v in b if v.constraint_type == PK)
|
||||
|
||||
return partial(statements_for_changes, a_od, b_od)
|
||||
elif name in THINGS:
|
||||
return partial(
|
||||
statements_for_changes,
|
||||
|
@ -44,12 +44,14 @@ class Migration(object):
|
||||
self.statements.safe = safety_on
|
||||
|
||||
def add_all_changes(self):
|
||||
self.add(self.changes.enums(creations_only=True, modifications=False))
|
||||
self.add(self.changes.extensions(creations_only=True))
|
||||
self.add(self.changes.enums(creations_only=True, modifications=False))
|
||||
self.add(self.changes.sequences(creations_only=True))
|
||||
|
||||
self.add(self.changes.constraints(drops_only=True))
|
||||
self.add(self.changes.non_pk_constraints(drops_only=True))
|
||||
self.add(self.changes.pk_constraints(drops_only=True))
|
||||
self.add(self.changes.indexes(drops_only=True))
|
||||
|
||||
self.add(self.changes.views(drops_only=True))
|
||||
self.add(self.changes.functions(drops_only=True))
|
||||
|
||||
@ -63,7 +65,8 @@ class Migration(object):
|
||||
self.add(self.changes.extensions(drops_only=True))
|
||||
|
||||
self.add(self.changes.indexes(creations_only=True))
|
||||
self.add(self.changes.constraints(creations_only=True))
|
||||
self.add(self.changes.pk_constraints(creations_only=True))
|
||||
self.add(self.changes.non_pk_constraints(creations_only=True))
|
||||
|
||||
@property
|
||||
def sql(self):
|
||||
|
2
setup.py
2
setup.py
@ -8,7 +8,7 @@ readme = io.open('README.rst').read()
|
||||
|
||||
setup(
|
||||
name='migra',
|
||||
version='0.1.1470915710',
|
||||
version='0.1.1470919405',
|
||||
url='https://github.com/djrobstep/migra',
|
||||
description='Like diff but for PostgreSQL schemas',
|
||||
long_description=readme,
|
||||
|
@ -8,19 +8,11 @@ CREATE TYPE unused_enum AS ENUM ('a', 'b');
|
||||
|
||||
CREATE TYPE usage_dropped_enum AS ENUM ('x', 'y');
|
||||
|
||||
CREATE TABLE products (
|
||||
product_no integer,
|
||||
name varchar(10) not null unique,
|
||||
price numeric,
|
||||
x integer not null default 7,
|
||||
oldcolumn text,
|
||||
constraint x check (price > 0)
|
||||
CREATE TABLE aunwanted (
|
||||
id serial primary key,
|
||||
name text not null
|
||||
);
|
||||
|
||||
create index on products(price);
|
||||
|
||||
create view vvv as select * from products;
|
||||
|
||||
CREATE TABLE orders (
|
||||
order_id serial primary key,
|
||||
shipping_address text,
|
||||
@ -28,11 +20,25 @@ CREATE TABLE orders (
|
||||
status2 usage_dropped_enum
|
||||
);
|
||||
|
||||
CREATE TABLE unwanted (
|
||||
id serial,
|
||||
name text not null
|
||||
CREATE TABLE products (
|
||||
product_no integer,
|
||||
name varchar(10) not null unique,
|
||||
price numeric,
|
||||
x integer not null default 7 unique,
|
||||
oldcolumn text,
|
||||
constraint x check (price > 0),
|
||||
z integer REFERENCES orders ON DELETE CASCADE,
|
||||
zz integer REFERENCES aunwanted ON DELETE CASCADE
|
||||
);
|
||||
|
||||
create unique index on products(x);
|
||||
|
||||
create unique index on orders(order_id);
|
||||
|
||||
create index on products(price);
|
||||
|
||||
create view vvv as select * from products;
|
||||
|
||||
create or replace function public.changed(i integer, t text[])
|
||||
returns TABLE(a text, c integer) as
|
||||
$$
|
||||
|
@ -1,26 +1,42 @@
|
||||
create type "public"."bug_status" as enum ('new', 'open', 'closed');
|
||||
|
||||
create extension "citext" with schema "public" version '1.1';
|
||||
|
||||
create extension "hstore" with schema "public" version '1.3';
|
||||
|
||||
create type "public"."bug_status" as enum ('new', 'open', 'closed');
|
||||
|
||||
create sequence "public"."bug_id_seq";
|
||||
|
||||
create sequence "public"."products_product_no_seq";
|
||||
|
||||
alter table "public"."products" drop constraint "products_name_key";
|
||||
|
||||
alter table "public"."products" drop constraint "products_x_key";
|
||||
|
||||
alter table "public"."products" drop constraint "products_z_fkey";
|
||||
|
||||
alter table "public"."products" drop constraint "products_zz_fkey";
|
||||
|
||||
alter table "public"."products" drop constraint "x";
|
||||
|
||||
alter table "public"."aunwanted" drop constraint "aunwanted_pkey";
|
||||
|
||||
drop index if exists "public"."aunwanted_pkey";
|
||||
|
||||
drop index if exists "public"."orders_order_id_idx";
|
||||
|
||||
drop index if exists "public"."products_name_key";
|
||||
|
||||
drop index if exists "public"."products_price_idx";
|
||||
|
||||
drop index if exists "public"."products_x_idx";
|
||||
|
||||
drop index if exists "public"."products_x_key";
|
||||
|
||||
drop view if exists "public"."vvv" cascade;
|
||||
|
||||
drop function if exists "public"."changed"(i integer, t text[]) cascade;
|
||||
|
||||
drop table "public"."unwanted";
|
||||
drop table "public"."aunwanted";
|
||||
|
||||
create table "public"."bug" (
|
||||
"id" integer not null default nextval('bug_id_seq'::regclass),
|
||||
@ -56,6 +72,10 @@ alter table "public"."orders" alter column "status2" set data type text;
|
||||
|
||||
alter table "public"."products" drop column "oldcolumn";
|
||||
|
||||
alter table "public"."products" drop column "z";
|
||||
|
||||
alter table "public"."products" drop column "zz";
|
||||
|
||||
alter table "public"."products" add column "newcolumn" text;
|
||||
|
||||
alter table "public"."products" add column "newcolumn2" interval;
|
||||
@ -101,9 +121,9 @@ $$
|
||||
$$
|
||||
language PLPGSQL VOLATILE RETURNS NULL ON NULL INPUT SECURITY DEFINER;
|
||||
|
||||
drop sequence if exists "public"."orders_order_id_seq";
|
||||
drop sequence if exists "public"."aunwanted_id_seq";
|
||||
|
||||
drop sequence if exists "public"."unwanted_id_seq";
|
||||
drop sequence if exists "public"."orders_order_id_seq";
|
||||
|
||||
drop type "public"."unwanted_enum";
|
||||
|
||||
@ -115,14 +135,14 @@ CREATE INDEX products_name_idx ON products USING btree (name);
|
||||
|
||||
CREATE UNIQUE INDEX products_pkey ON products USING btree (product_no);
|
||||
|
||||
alter table "public"."order_items" add constraint "order_items_order_id_fkey" FOREIGN KEY (order_id) REFERENCES orders(order_id) ON DELETE CASCADE;
|
||||
|
||||
alter table "public"."order_items" add constraint "order_items_pkey" PRIMARY KEY using index "order_items_pkey";
|
||||
|
||||
alter table "public"."order_items" add constraint "order_items_product_no_fkey" FOREIGN KEY (product_no) REFERENCES products(product_no) ON DELETE RESTRICT;
|
||||
|
||||
alter table "public"."products" add constraint "products_pkey" PRIMARY KEY using index "products_pkey";
|
||||
|
||||
alter table "public"."order_items" add constraint "order_items_order_id_fkey" FOREIGN KEY (order_id) REFERENCES orders(order_id) ON DELETE CASCADE;
|
||||
|
||||
alter table "public"."order_items" add constraint "order_items_product_no_fkey" FOREIGN KEY (product_no) REFERENCES products(product_no) ON DELETE RESTRICT;
|
||||
|
||||
alter table "public"."products" add constraint "y" CHECK ((price > (0)::numeric));
|
||||
|
||||
alter table "public"."products" add constraint "x" CHECK ((price > (10)::numeric));
|
||||
|
@ -22,7 +22,7 @@ B = 'alter table "public"."products" add column "newcolumn" text;\n\n'
|
||||
A = 'alter table "public"."products" drop column "oldcolumn";\n\n'
|
||||
|
||||
EXPECTED = io.open('tests/FIXTURES/expected.sql').read().strip()
|
||||
EXPECTED2 = EXPECTED.replace(A + B, '')
|
||||
EXPECTED2 = EXPECTED.replace(A, '').replace(B, '')
|
||||
|
||||
|
||||
def test_statements():
|
||||
|
Loading…
Reference in New Issue
Block a user