utils.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import warnings
  2. import sqlalchemy
  3. def parse_version(v):
  4. """
  5. Take a string version and conver it to a tuple (for easier comparison), e.g.:
  6. "1.2.3" --> (1, 2, 3)
  7. "1.2" --> (1, 2, 0)
  8. "1" --> (1, 0, 0)
  9. """
  10. parts = v.split(".")
  11. # Pad the list to make sure there is three elements so that we get major, minor, point
  12. # comparisons that default to "0" if not given. I.e. "1.2" --> (1, 2, 0)
  13. parts = (parts + 3 * ['0'])[:3]
  14. return tuple(int(x) for x in parts)
  15. def sqlalchemy_version(op, val):
  16. sa_ver = parse_version(sqlalchemy.__version__)
  17. target_ver = parse_version(val)
  18. assert op in ('<', '>', '<=', '>=', '=='), 'op {} not supported'.format(op)
  19. if op == '<':
  20. return sa_ver < target_ver
  21. if op == '>':
  22. return sa_ver > target_ver
  23. if op == '<=':
  24. return sa_ver <= target_ver
  25. if op == '>=':
  26. return sa_ver >= target_ver
  27. return sa_ver == target_ver
  28. def engine_config_warning(config, version, deprecated_config_key, engine_option):
  29. if config[deprecated_config_key] is not None:
  30. warnings.warn(
  31. 'The `{}` config option is deprecated and will be removed in'
  32. ' v{}. Use `SQLALCHEMY_ENGINE_OPTIONS[\'{}\']` instead.'
  33. .format(deprecated_config_key, version, engine_option),
  34. DeprecationWarning
  35. )