Creating revision for specific object

12 views
Skip to first unread message

Zac Goldstein

unread,
Feb 4, 2019, 6:39:22 PM2/4/19
to sqlalchemy

Hello, I'm trying to create a revision programatically with the operations also made programatically. I've been trying this hackish approach below, which I now think probably isn't the right way to go due to the internals of AutogenContext, which seems required for rendering. 


Here's what I have so far:



    upgrade_ops
= UpgradeOps([CreateTableOp.from_table(model.__table__)])

    downgrade_ops
= DowngradeOps([DropTableOp.from_table(model.__table__)])

    command_args
= dict(
        message
="create table " + tname,
        autogenerate
=False,
        sql
=False,
        head
="head",
        splice
=False,
        branch_label
=None,
        version_path
=None,
        rev_id
=rev_id(),
        depends_on
=None

   
)

    script_args
= {k:v for k, v in command_args.items() if k != "sql" and k != "autogenerate"}
    migration_script
= MigrationScript(upgrade_ops=upgrade_ops,
        downgrade_ops
=downgrade_ops, **script_args)
    migration_script
._needs_render = True

    ini_path
= os.path.abspath(os.path.join(os.path.dirname(__file__), '../../../alembic.ini'))
    config
= Config(ini_path, ini_section=table['schema'])
    script_directory
=ScriptDirectory.from_config(config)
    revision
= RevisionContext(config, script_directory, command_args)
    revision
._last_autogen_context = SimpleNamespace(imports=set())
    revision
._to_script(migration_script)



My other thought is to use EnvironmentContext and run a full automigration with include_object set to something like

lambda o: o is model.__table__
Doing the full automigration seems overkill, but if it's considerably easier then it's preferable. If I go this route It's not clear to me how to avoid env.py and instead run this other context with the new include_object.


Any help would be appreciated.
Zac

Mike Bayer

unread,
Feb 4, 2019, 9:12:59 PM2/4/19
to sqlal...@googlegroups.com
this should simplify things a bit

from sqlalchemy import Column
from sqlalchemy import Integer
from sqlalchemy import MetaData
from sqlalchemy import Table

from alembic.autogenerate import RevisionContext
from alembic.config import Config
from alembic.operations.ops import CreateTableOp
from alembic.operations.ops import UpgradeOps
from alembic.runtime.environment import EnvironmentContext
from alembic.script import ScriptDirectory
from alembic.util import rev_id

m = MetaData()
t = Table("some_table", m, Column("q", Integer), schema="foo")


command_args = dict(
message="create table " + t.name,
autogenerate=False,
sql=False,
head="head",
splice=False,
branch_label=None,
version_path=None,
rev_id=rev_id(),
depends_on=None,
)

config = Config("alembic.ini")
script_directory = ScriptDirectory.from_config(config)
revision_context = RevisionContext(config, script_directory, command_args)


with EnvironmentContext(
config, script_directory, template_args=revision_context.template_args
) as env:
env.configure(dialect_name="sqlite")
upgrade_ops = UpgradeOps([CreateTableOp.from_table(t)])
downgrade_ops = upgrade_ops.reverse()
revision_context.generated_revisions[0].upgrade_ops = upgrade_ops
revision_context.generated_revisions[0].downgrade_ops = downgrade_ops
revision_context.run_no_autogenerate(
command_args["rev_id"], env.get_context()
)

# generates the file
scripts = [script for script in revision_context.generate_scripts()]
> --
> SQLAlchemy -
> The Python SQL Toolkit and Object Relational Mapper
>
> http://www.sqlalchemy.org/
>
> To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
> ---
> You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
> To post to this group, send email to sqlal...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sqlalchemy.
> For more options, visit https://groups.google.com/d/optout.

Zac Goldstein

unread,
Feb 5, 2019, 2:13:11 AM2/5/19
to sqlal...@googlegroups.com
fantastic, this worked like a charm.

You received this message because you are subscribed to a topic in the Google Groups "sqlalchemy" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/sqlalchemy/4PbFyB1ZVi0/unsubscribe.
To unsubscribe from this group and all its topics, send an email to sqlalchemy+...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages