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

fix: different paths for env script and migrations #15

Closed
wants to merge 1 commit into from

Conversation

asfaltboy
Copy link

With both of these on the same path alembic causes a deadlock condition trying to create the alembic_version table for the first time.

When I inspected the call path (by pdbing _ensure_version_table), I saw the second call to create the table that happened during the same (first) transaction, originated from the get_revisions call path. Not sure how this could ever work for someone else.

(Pdb) w
  venv/bin/alembic(8)<module>()
-> sys.exit(main())
  venv/lib/python3.8/site-packages/alembic/config.py(577)main()
-> CommandLine(prog=prog).main(argv=argv)
  venv/lib/python3.8/site-packages/alembic/config.py(571)main()
-> self.run_cmd(cfg, options)
  venv/lib/python3.8/site-packages/alembic/config.py(548)run_cmd()
-> fn(
  venv/lib/python3.8/site-packages/alembic/command.py(214)revision()
-> script_directory.run_env()
  venv/lib/python3.8/site-packages/alembic/script/base.py(489)run_env()
-> util.load_python_file(self.dir, "env.py")
  venv/lib/python3.8/site-packages/alembic/util/pyfiles.py(98)load_python_file()
-> module = load_module_py(module_id, path)
  venv/lib/python3.8/site-packages/alembic/util/compat.py(184)load_module_py()
-> spec.loader.exec_module(module)
  <frozen importlib._bootstrap_external>(783)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  london-looker/app/models/orm/migrations/env.py(78)<module>()
-> run_migrations_online()
  london-looker/app/models/orm/migrations/env.py(72)run_migrations_online()
-> context.run_migrations()
  <string>(8)run_migrations()
  venv/lib/python3.8/site-packages/alembic/runtime/environment.py(846)run_migrations()
-> self.get_context().run_migrations(**kw)
  venv/lib/python3.8/site-packages/alembic/runtime/migration.py(510)run_migrations()
-> for step in self._migrations_fn(heads, self):
  venv/lib/python3.8/site-packages/alembic/command.py(190)retrieve_migrations()
-> revision_context.run_autogenerate(rev, context)
  venv/lib/python3.8/site-packages/alembic/autogenerate/api.py(442)run_autogenerate()
-> self._run_environment(rev, migration_context, True)
  venv/lib/python3.8/site-packages/alembic/autogenerate/api.py(454)_run_environment()
-> self.script_directory.get_revisions("heads")
  venv/lib/python3.8/site-packages/alembic/script/base.py(227)get_revisions()
-> return self.revision_map.get_revisions(id_)
  venv/lib/python3.8/site-packages/alembic/script/revision.py(321)get_revisions()
-> resolved_id, branch_label = self._resolve_revision_number(id_)
  venv/lib/python3.8/site-packages/alembic/script/revision.py(501)_resolve_revision_number()
-> self._revision_map
  venv/lib/python3.8/site-packages/alembic/util/langhelpers.py(230)__get__()
-> obj.__dict__[self.__name__] = result = self.fget(obj)
  venv/lib/python3.8/site-packages/alembic/script/revision.py(123)_revision_map()
-> for revision in self._generator():
  venv/lib/python3.8/site-packages/alembic/script/base.py(112)_load_revisions()
-> script = Script._from_filename(self, vers, file_)
  venv/lib/python3.8/site-packages/alembic/script/base.py(906)_from_filename()
-> module = util.load_python_file(dir_, filename)
  venv/lib/python3.8/site-packages/alembic/util/pyfiles.py(98)load_python_file()
-> module = load_module_py(module_id, path)
  venv/lib/python3.8/site-packages/alembic/util/compat.py(184)load_module_py()
-> spec.loader.exec_module(module)
  <frozen importlib._bootstrap_external>(783)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  london-looker/app/models/orm/migrations/env.py(78)<module>()
-> run_migrations_online()
  london-looker/app/models/orm/migrations/env.py(72)run_migrations_online()
-> context.run_migrations()
  <string>(8)run_migrations()
  venv/lib/python3.8/site-packages/alembic/runtime/environment.py(846)run_migrations()
-> self.get_context().run_migrations(**kw)
  venv/lib/python3.8/site-packages/alembic/runtime/migration.py(502)run_migrations()
-> self._ensure_version_table()
> venv/lib/python3.8/site-packages/alembic/runtime/migration.py(443)_ensure_version_table()
-> self._version.create(self.connection, checkfirst=True)

With both of these on the same path alembic causes a deadlock condition trying to create the `alembic_version` table for the first time.

When I inspected the call path (by pdbing `_ensure_version_table`), I saw the second call to create the table that happened during the same (first) transaction, originated from the `get_revisions` call path. Not sure how this could ever work for someone else.

```
(Pdb) w
  venv/bin/alembic(8)<module>()
-> sys.exit(main())
  venv/lib/python3.8/site-packages/alembic/config.py(577)main()
-> CommandLine(prog=prog).main(argv=argv)
  venv/lib/python3.8/site-packages/alembic/config.py(571)main()
-> self.run_cmd(cfg, options)
  venv/lib/python3.8/site-packages/alembic/config.py(548)run_cmd()
-> fn(
  venv/lib/python3.8/site-packages/alembic/command.py(214)revision()
-> script_directory.run_env()
  venv/lib/python3.8/site-packages/alembic/script/base.py(489)run_env()
-> util.load_python_file(self.dir, "env.py")
  venv/lib/python3.8/site-packages/alembic/util/pyfiles.py(98)load_python_file()
-> module = load_module_py(module_id, path)
  venv/lib/python3.8/site-packages/alembic/util/compat.py(184)load_module_py()
-> spec.loader.exec_module(module)
  <frozen importlib._bootstrap_external>(783)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  london-looker/app/models/orm/migrations/env.py(78)<module>()
-> run_migrations_online()
  london-looker/app/models/orm/migrations/env.py(72)run_migrations_online()
-> context.run_migrations()
  <string>(8)run_migrations()
  venv/lib/python3.8/site-packages/alembic/runtime/environment.py(846)run_migrations()
-> self.get_context().run_migrations(**kw)
  venv/lib/python3.8/site-packages/alembic/runtime/migration.py(510)run_migrations()
-> for step in self._migrations_fn(heads, self):
  venv/lib/python3.8/site-packages/alembic/command.py(190)retrieve_migrations()
-> revision_context.run_autogenerate(rev, context)
  venv/lib/python3.8/site-packages/alembic/autogenerate/api.py(442)run_autogenerate()
-> self._run_environment(rev, migration_context, True)
  venv/lib/python3.8/site-packages/alembic/autogenerate/api.py(454)_run_environment()
-> self.script_directory.get_revisions("heads")
  venv/lib/python3.8/site-packages/alembic/script/base.py(227)get_revisions()
-> return self.revision_map.get_revisions(id_)
  venv/lib/python3.8/site-packages/alembic/script/revision.py(321)get_revisions()
-> resolved_id, branch_label = self._resolve_revision_number(id_)
  venv/lib/python3.8/site-packages/alembic/script/revision.py(501)_resolve_revision_number()
-> self._revision_map
  venv/lib/python3.8/site-packages/alembic/util/langhelpers.py(230)__get__()
-> obj.__dict__[self.__name__] = result = self.fget(obj)
  venv/lib/python3.8/site-packages/alembic/script/revision.py(123)_revision_map()
-> for revision in self._generator():
  venv/lib/python3.8/site-packages/alembic/script/base.py(112)_load_revisions()
-> script = Script._from_filename(self, vers, file_)
  venv/lib/python3.8/site-packages/alembic/script/base.py(906)_from_filename()
-> module = util.load_python_file(dir_, filename)
  venv/lib/python3.8/site-packages/alembic/util/pyfiles.py(98)load_python_file()
-> module = load_module_py(module_id, path)
  venv/lib/python3.8/site-packages/alembic/util/compat.py(184)load_module_py()
-> spec.loader.exec_module(module)
  <frozen importlib._bootstrap_external>(783)exec_module()
  <frozen importlib._bootstrap>(219)_call_with_frames_removed()
  london-looker/app/models/orm/migrations/env.py(78)<module>()
-> run_migrations_online()
  london-looker/app/models/orm/migrations/env.py(72)run_migrations_online()
-> context.run_migrations()
  <string>(8)run_migrations()
  venv/lib/python3.8/site-packages/alembic/runtime/environment.py(846)run_migrations()
-> self.get_context().run_migrations(**kw)
  venv/lib/python3.8/site-packages/alembic/runtime/migration.py(502)run_migrations()
-> self._ensure_version_table()
> venv/lib/python3.8/site-packages/alembic/runtime/migration.py(443)_ensure_version_table()
-> self._version.create(self.connection, checkfirst=True)
```
@asfaltboy
Copy link
Author

@leosussan any thoughts on this PR ?

@leosussan
Copy link
Owner

@asfaltboy thank you for submitting this - and for your patience. I'll review in short order.

@asfaltboy asfaltboy closed this Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants