]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- add a new section "Run Multiple Alembic Environments from one .ini file",
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 23 Jan 2017 19:24:31 +0000 (14:24 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 23 Jan 2017 19:24:31 +0000 (14:24 -0500)
clarify what the --name flag does and how this differs from
"multiple bases".

Change-Id: If65a8a11802db18bc2f42ffea360c70cbc9113bb

docs/build/branches.rst
docs/build/cookbook.rst
docs/build/tutorial.rst
tests/test_postgresql.py

index acc07893839da261af88e3aa51f272775315bced..e69db1bb1aa98c1337e3d127883c1db462a183a4 100644 (file)
@@ -509,6 +509,13 @@ before the head::
 Working with Multiple Bases
 ---------------------------
 
+.. note::  The multiple base feature is intended to allow for multiple Alembic
+   versioning lineages which **share the same alembic_version table**.  This is
+   so that individual revisions within the lineages can have cross-dependencies
+   on each other.   For the simpler case where one project has multiple,
+   **completely independent** revision lineages that refer to **separate**
+   alembic_version tables, see the example in :ref:`multiple_environments`.
+
 We've seen in the previous section that ``alembic upgrade`` is fine
 if we have multiple heads, ``alembic revision`` allows us to tell it which
 "head" we'd like to associate our new revision file with, and branch labels
index e6e04e4cbd4d2023165b9646d8f61dd067cf4455..26932e02f72338eaff5404a2ef50c2b57deb4e27 100644 (file)
@@ -830,3 +830,54 @@ Then define ``include_object`` as::
         return not object.info.get('is_view', False)
 
 Finally, in ``env.py`` pass your ``include_object`` as a keyword argument to :meth:`.EnvironmentContext.configure`.
+
+.. _multiple_environments:
+
+Run Multiple Alembic Environments from one .ini file
+====================================================
+
+Long before Alembic had the "multiple bases" feature described in :ref:`multiple_bases`,
+projects had a need to maintain more than one Alembic version history in a single
+project, where these version histories are completely independent of each other
+and each refer to their own alembic_version table, either across multiple databases,
+schemas, or namespaces.  A simple approach was added to support this, the
+``--name`` flag on the commandline.
+
+First, one would create an alembic.ini file of this form::
+
+    [DEFAULT]
+    # all defaults shared between environments go here
+
+    sqlalchemy.url = postgresql://scott:tiger@hostname/mydatabase
+
+
+    [schema1]
+    # path to env.py and migration scripts for schema1
+    script_location = myproject/revisions/schema1
+
+    [schema2]
+    # path to env.py and migration scripts for schema2
+    script_location = myproject/revisions/schema2
+
+    [schema3]
+    # path to env.py and migration scripts for schema3
+    script_location = myproject/revisions/db2
+
+    # this schema uses a different database URL as well
+    sqlalchemy.url = postgresql://scott:tiger@hostname/myotherdatabase
+
+
+Above, in the ``[DEFAULT]`` section we set up a default database URL.
+Then we create three sections corresponding to different revision lineages
+in our project.   Each of these directories would have its own ``env.py``
+and set of versioning files.   Then when we run the ``alembic`` command,
+we simply give it the name of the configuration we want to use::
+
+    alembic --name schema2 revision -m "new rev for schema 2" --autogenerate
+
+Above, the ``alembic`` command makes use of the configuration in ``[schema2]``,
+populated with defaults from the ``[DEFAULT]`` section.
+
+The above approach can be automated by creating a custom front-end to the
+Alembic commandline as well.
+
index 03fd758d3b184b1b8d8c994ac27ffe4fc79078c9..fbbc5a4e120ae9dda8994638b3057e389e1caf54 100644 (file)
@@ -186,7 +186,10 @@ with the path to the Alembic script location.
 This file contains the following features:
 
 * ``[alembic]`` - this is the section read by Alembic to determine configuration.  Alembic
-  itself does not directly read any other areas of the file.
+  itself does not directly read any other areas of the file.    The name "alembic" can
+  be customized using the ``--name`` commandline flag; see :ref:`multiple_environments`
+  for a basic example of this.
+
 * ``script_location`` - this is the location of the Alembic environment.   It is normally
   specified as a filesystem location, either relative or absolute.  If the location is
   a relative path, it's interpreted as relative to the current directory.
index 21479a06d15b39b61e43516dbac0474f18e8f12a..a27deebc35c4f4f7ea1bdac2eb5d537046417850 100644 (file)
@@ -66,6 +66,12 @@ class PostgresqlOpTest(TestBase):
             'ALTER TABLE t ALTER COLUMN c TYPE INTEGER USING c::integer'
         )
 
+    def test_col_w_pk_is_serial(self):
+        context = op_fixture("postgresql")
+        op.add_column("some_table", Column('q', Integer, primary_key=True))
+        context.assert_(
+            'ALTER TABLE some_table ADD COLUMN q SERIAL NOT NULL'
+        )
 
 class PGOfflineEnumTest(TestBase):