From c69fe4acf8929856735e5d90adb7f6b6d5ebcd46 Mon Sep 17 00:00:00 2001 From: Roman Podolyaka Date: Sun, 9 Jun 2013 19:07:00 +0300 Subject: Add basic support of unique constraints reflection Inspection API already supports reflection of table indexes information and those also include unique constraints (at least for PostgreSQL and MySQL). But it could be actually useful to distinguish between indexes and plain unique constraints (though both are implemented in the same way internally in RDBMS). This change adds a new method to Inspection API - get_unique_constraints() and implements it for SQLite, PostgreSQL and MySQL dialects. --- test/engine/test_reflection.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) (limited to 'test/engine/test_reflection.py') diff --git a/test/engine/test_reflection.py b/test/engine/test_reflection.py index ac0fa5153..fd9411874 100644 --- a/test/engine/test_reflection.py +++ b/test/engine/test_reflection.py @@ -1,3 +1,5 @@ +import operator + import unicodedata import sqlalchemy as sa from sqlalchemy import schema, events, event, inspect @@ -878,6 +880,41 @@ class ReflectionTest(fixtures.TestBase, ComparesTables): assert set([t2.c.name, t2.c.id]) == set(r2.columns) assert set([t2.c.name]) == set(r3.columns) + @testing.provide_metadata + def test_unique_constraints_reflection(self): + uniques = sorted( + [ + {'name': 'unique_a_b_c', 'column_names': ['a', 'b', 'c']}, + {'name': 'unique_a_c', 'column_names': ['a', 'c']}, + {'name': 'unique_b_c', 'column_names': ['b', 'c']}, + ], + key=operator.itemgetter('name') + ) + + try: + orig_meta = sa.MetaData(bind=testing.db) + table = Table( + 'testtbl', orig_meta, + Column('a', sa.String(20)), + Column('b', sa.String(30)), + Column('c', sa.Integer), + ) + for uc in uniques: + table.append_constraint( + sa.UniqueConstraint(*uc['column_names'], name=uc['name']) + ) + orig_meta.create_all() + + inspector = inspect(testing.db) + reflected = sorted( + inspector.get_unique_constraints('testtbl'), + key=operator.itemgetter('name') + ) + + assert uniques == reflected + finally: + testing.db.execute('drop table if exists testtbl;') + @testing.requires.views @testing.provide_metadata def test_views(self): -- cgit v1.2.1