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
Conversation
There was a problem hiding this 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 |
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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( |
There was a problem hiding this comment.
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.
…est_script_production
pushed a new commit. I'm only unsure about the ScriptDirectory ImportError when import at top of the file. Thanks! |
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. |
pushed optional prefixes into signature |
don't panic, I'm transferring this to gerrit |
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. |
Thanks for the update. I will follow on Gerrit |
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
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: