alembic icon indicating copy to clipboard operation
alembic copied to clipboard

Is it possible to print SQL of the last revision in one command?

Open max-block opened this issue 5 years ago • 3 comments

I'm trying to print SQL of the last revision in one command.

I can do it like this: alembic upgrade --sql 5fb648aeb2e7:head But it's necessary to do alembic history first to get the second last id.

There are relative migration identifies. I've tried:

alembic upgrade --sql +1
alembic upgrade --sql +0

, but it doesn't print the last only migration.

How to do it in one command?

max-block avatar Jun 08 '20 12:06 max-block

Well I would assume it should work this way

alembic upgrade --sql "head-1":"head"

but it doesn't. looking to see if this is a simple fix

zzzeek avatar Jun 18 '20 15:06 zzzeek

it's not possible right now. Here's the beginning of it but this would require a lot of work:


diff --git a/alembic/runtime/migration.py b/alembic/runtime/migration.py
index 5c8590d..b64ba7a 100644
--- a/alembic/runtime/migration.py
+++ b/alembic/runtime/migration.py
@@ -419,7 +419,6 @@ class MigrationContext(object):
             if start_from_rev == "base":
                 start_from_rev = None
             elif start_from_rev is not None and self.script:
-
                 start_from_rev = [
                     self.script.get_revision(sfr).revision
                     for sfr in util.to_list(start_from_rev)
diff --git a/alembic/script/revision.py b/alembic/script/revision.py
index 22481a0..7082ada 100644
--- a/alembic/script/revision.py
+++ b/alembic/script/revision.py
@@ -478,8 +478,22 @@ class RevisionMap(object):
         )
 
     def _resolve_revision_number(self, id_):
-        if isinstance(id_, compat.string_types) and "@" in id_:
-            branch_label, id_ = id_.split("@", 1)
+
+        if isinstance(id_, compat.string_types):
+            match = _relative_destination.match(id_)
+
+            if match:
+                # TODO: now need to use this with relative_iterate()
+                # or similar to get the right revision
+                relative = int(match.group(3))
+                id_ = match.group(2)
+                branch_label = match.group(1)
+            else:
+                # TODO: have one regex that does both things
+                if "@" in id_:
+                    branch_label, id_ = id_.split("@", 1)
+                else:
+                    branch_label = None
 
         elif id_ is not None and (
             (
@@ -500,6 +514,7 @@ class RevisionMap(object):
         # ensure map is loaded
         self._revision_map
         if id_ == "heads":
+            # TODO: raise error if relative is present
             if branch_label:
                 return (
                     self.filter_for_lineage(self.heads, branch_label),
@@ -510,10 +525,12 @@ class RevisionMap(object):
         elif id_ == "head":
             current_head = self.get_current_head(branch_label)
             if current_head:
+                # TODO: relative iterate here
                 return (current_head,), branch_label
             else:
                 return (), branch_label
         elif id_ == "base" or id_ is None:
+            # TODO: relative iterate here
             return (), branch_label
         else:
             return util.to_tuple(id_, default=None), branch_label

zzzeek avatar Jun 18 '20 16:06 zzzeek

That would be appreciated! <3

karamfil avatar Oct 10 '23 12:10 karamfil