Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generate_revision_from_script to autogenerate.api #35

Closed
wants to merge 3 commits into from

Conversation

tysonholub
Copy link

allows a revision to be generated against a provided migration_script (it seemed more appropriate to define a new function rather than tweak the render_python_code helper)

Ex:

from alembic.autogenerate import api
from alembic.config import Config
from alembic.operations import ops
import sqlalchemy as sa
import os

config = Config(os.environ['ALEMBIC_CONFIG'])

migration_script = ops.MigrationScript(
    rev_id=None,
    message='add test table',
    upgrade_ops=ops.UpgradeOps(
        ops=[
            ops.CreateTableOp(
                'test_table',
                [
                    sa.Column('id', sa.Integer(), primary_key=True),
                    sa.Column('name', sa.String(50), nullable=False)
                ]
            ),
        ]
    ),
    downgrade_ops=ops.DowngradeOps(
        ops=[
            ops.DropTableOp('test_table')
        ]
    ),
)

api.generate_revision_from_script(config, migration_script) 

Copy link
Owner

@zzzeek zzzeek left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs a unit test in test_script_production. It would be a new test class that has just one test to start with, runs the function then uses os.access(fname, os.F_OK) to make sure the file was written. setup and teardown can likely be:

class ProgammaticGenerationTest(TestBase):

    def setUp(self):
        staging_env()
        self.cfg = _no_sql_testing_config(
            directives="\nrevision_environment=true\n"
        )

    def tearDown(self):
        clear_staging_env()


    def test_gen_revision_from_script(self):
    	migration_script = ...

    	rev = generate_revision_from_script(self.cfg, migration_script)

    	assert os.access(rev.<get_the_filename>, os.F.OK)

'user_module_prefix': None,
}, autogenerate=False)

revision_context.generated_revisions[0].upgrade_ops = migration_script.upgrade_ops
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we say:

revision_context.generated_revisions[0] = migration_script

?

@@ -181,6 +181,41 @@ def _render_migration_diffs(context, template_args):
)


def generate_revision_from_script(config, migration_script):
from ..script import ScriptDirectory
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

imports at the top of file...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I import at the top of the file I get this ImportError

Traceback (most recent call last):
  File "migrate_test.py", line 1, in <module>
    from alembic.autogenerate import api
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/__init__.py", line 9, in <module>
    from . import context  # noqa
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/context.py", line 1, in <module>
    from .runtime.environment import EnvironmentContext
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/runtime/environment.py", line 2, in <module>
    from .migration import MigrationContext
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/runtime/migration.py", line 11, in <module>
    from .. import ddl, util
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/ddl/__init__.py", line 1, in <module>
    from . import postgresql, mysql, sqlite, mssql, oracle  # pragma: no cover
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/ddl/mysql.py", line 12, in <module>
    from ..autogenerate import compare
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/autogenerate/__init__.py", line 1, in <module>
    from .api import ( # noqa
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/autogenerate/api.py", line 5, in <module>
    from ..script import ScriptDirectory
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/script/__init__.py", line 1, in <module>
    from .base import ScriptDirectory, Script  # noqa
  File "/home/vagrant/.pyenv/versions/dtools/lib/python2.7/site-packages/alembic/script/base.py", line 8, in <module>
    from ..runtime import migration
ImportError: cannot import name migration

@@ -181,6 +181,41 @@ def _render_migration_diffs(context, template_args):
)


def generate_revision_from_script(config, migration_script):
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

docstring w/ params

revision_context = RevisionContext(
config, script_directory, command_args)

revision_context._last_autogen_context = AutogenContext(
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

look at render_python_code(), these prefixes are all configurable.

I'd prefer if render_python_code and this function could somehow derive from more common elements but this is a good start for now.

@tysonholub
Copy link
Author

pushed a new commit. I'm only unsure about the ScriptDirectory ImportError when import at top of the file. Thanks!

@zzzeek
Copy link
Owner

zzzeek commented Jan 23, 2017

the import is not a big deal but suggests the organization of the modules here is working aginast what you want to do. id have to take a look. can we make those "prefix" options available to change in the function def ? I can take it after that.

@tysonholub
Copy link
Author

pushed optional prefixes into signature

@zzzeek
Copy link
Owner

zzzeek commented Jan 30, 2017

don't panic, I'm transferring this to gerrit

@zzzeek
Copy link
Owner

zzzeek commented Jan 30, 2017

Dear contributor -

This pull request is being moved to Gerrit, at https://gerrit.sqlalchemy.org/303, where it may be tested and reviewed more closely. As such, the pull request itself is being marked "closed" or "declined", however your contribution is merely being moved to our central review system. Please register at https://gerrit.sqlalchemy.org#/register/ to send and receive comments regarding this item.

@zzzeek zzzeek closed this Jan 30, 2017
@tysonholub
Copy link
Author

Thanks for the update. I will follow on Gerrit

zzzeek added a commit that referenced this pull request Feb 22, 2017
This allows programmatic use of command.revision() to
specify an ad-hoc process_revision_directives callable,
rather than having it placed via the env.py script.

Co-authored-by: Tyson Holub
Change-Id: Ief1c11fd2a6f10e712851145d6d190a3b167817c
Pull-request: #35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants