config.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. # testing/config.py
  2. # Copyright (C) 2005-2017 the SQLAlchemy authors and contributors
  3. # <see AUTHORS file>
  4. #
  5. # This module is part of SQLAlchemy and is released under
  6. # the MIT License: http://www.opensource.org/licenses/mit-license.php
  7. """NOTE: copied/adapted from SQLAlchemy master for backwards compatibility;
  8. this should be removable when Alembic targets SQLAlchemy 1.0.0
  9. """
  10. import collections
  11. requirements = None
  12. db = None
  13. db_url = None
  14. db_opts = None
  15. file_config = None
  16. test_schema = None
  17. test_schema_2 = None
  18. _current = None
  19. class Config(object):
  20. def __init__(self, db, db_opts, options, file_config):
  21. self.db = db
  22. self.db_opts = db_opts
  23. self.options = options
  24. self.file_config = file_config
  25. self.test_schema = "test_schema"
  26. self.test_schema_2 = "test_schema_2"
  27. _stack = collections.deque()
  28. _configs = {}
  29. @classmethod
  30. def register(cls, db, db_opts, options, file_config):
  31. """add a config as one of the global configs.
  32. If there are no configs set up yet, this config also
  33. gets set as the "_current".
  34. """
  35. cfg = Config(db, db_opts, options, file_config)
  36. cls._configs[cfg.db.name] = cfg
  37. cls._configs[(cfg.db.name, cfg.db.dialect)] = cfg
  38. cls._configs[cfg.db] = cfg
  39. return cfg
  40. @classmethod
  41. def set_as_current(cls, config):
  42. global db, _current, db_url, test_schema, test_schema_2, db_opts
  43. _current = config
  44. db_url = config.db.url
  45. db_opts = config.db_opts
  46. test_schema = config.test_schema
  47. test_schema_2 = config.test_schema_2
  48. db = config.db
  49. @classmethod
  50. def push_engine(cls, db):
  51. assert _current, "Can't push without a default Config set up"
  52. cls.push(
  53. Config(
  54. db, _current.db_opts, _current.options, _current.file_config)
  55. )
  56. @classmethod
  57. def push(cls, config):
  58. cls._stack.append(_current)
  59. cls.set_as_current(config)
  60. @classmethod
  61. def reset(cls):
  62. if cls._stack:
  63. cls.set_as_current(cls._stack[0])
  64. cls._stack.clear()
  65. @classmethod
  66. def all_configs(cls):
  67. for cfg in set(cls._configs.values()):
  68. yield cfg
  69. @classmethod
  70. def all_dbs(cls):
  71. for cfg in cls.all_configs():
  72. yield cfg.db