From cf751243372044e33aa2a5450a9996d64232aa37 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Sun, 25 Aug 2019 18:49:19 -0400 Subject: [PATCH] Recipe for ignoring connection tables that aren't in the metadata Change-Id: Ic4e4feb8fa1407eaa69166d06043afaba0824a50 --- docs/build/cookbook.rst | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/docs/build/cookbook.rst b/docs/build/cookbook.rst index f7a20696..0d034af4 100644 --- a/docs/build/cookbook.rst +++ b/docs/build/cookbook.rst @@ -900,6 +900,39 @@ With the above rewriter, it generates as:: # ### end Alembic commands ### +Don't generate any DROP TABLE directives with autogenerate +========================================================== + +When running autogenerate against a database that has existing tables outside +of the application's autogenerated metadata, it may be desirable to prevent +autogenerate from considering any of those existing tables to be dropped. +This will prevent autogenerate from detecting tables removed from the +local metadata as well however this is only a small caveat. + +The most direct way to achieve this using the +:paramref:`.EnvironmentContext.configure.include_object` hook. There is no +need to hardcode a fixed "whitelist" of table names; the hook gives enough +information in the given arguments to determine if a particular table name is +not part of the local :class:`.MetaData` being autogenerated, by checking first +that the type of object is ``"table"``, then that ``reflected`` is ``True``, +indicating this table name is from the local database connection, not the +:class:`.MetaData`, and finally that ``compare_to`` is ``None``, indicating +autogenerate is not comparing this :class:`.Table` to any :class:`.Table` in +the local :class:`.MetaData` collection:: + + # in env.py + + def include_object(object, name, type_, reflected, compare_to): + if type_ == "table" and reflected and compare_to is None: + return False + else: + return True + + + context.configure( + # ... + include_object = include_object + ) Don't emit CREATE TABLE statements for Views ============================================ -- 2.47.2