README.dialects.rst 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. ========================
  2. Developing new Dialects
  3. ========================
  4. .. note::
  5. When studying this file, it's probably a good idea to also
  6. familiarize with the README.unittests.rst file, which discusses
  7. SQLAlchemy's usage and extension of the pytest test runner.
  8. While SQLAlchemy includes many dialects within the core distribution, the
  9. trend for new dialects should be that they are published as external
  10. projects. SQLAlchemy has since version 0.5 featured a "plugin" system
  11. which allows external dialects to be integrated into SQLAlchemy using
  12. standard setuptools entry points. As of version 0.8, this system has
  13. been enhanced, so that a dialect can also be "plugged in" at runtime.
  14. On the testing side, SQLAlchemy as of 0.8 also includes a "dialect
  15. compliance suite" that is usable by third party libraries. There is no
  16. longer a strong need for a new dialect to run through SQLAlchemy's full
  17. testing suite, as a large portion of these tests do not have
  18. dialect-sensitive functionality. The "dialect compliance suite" should
  19. be viewed as the primary target for new dialects, and as it continues
  20. to grow and mature it should become a more thorough and efficient system
  21. of testing new dialects.
  22. Dialect Layout
  23. ===============
  24. The file structure of a dialect is typically similar to the following::
  25. sqlalchemy-<dialect>/
  26. setup.py
  27. setup.cfg
  28. sqlalchemy_<dialect>/
  29. __init__.py
  30. base.py
  31. <dbapi>.py
  32. requirements.py
  33. test/
  34. conftest.py
  35. __init__.py
  36. test_suite.py
  37. test_<dialect_specific_test>.py
  38. ...
  39. An example of this structure can be seen in the Access dialect at
  40. https://github.com/sqlalchemy/sqlalchemy-access .
  41. Key aspects of this file layout include:
  42. * setup.py - should specify setuptools entrypoints, allowing the
  43. dialect to be usable from create_engine(), e.g.::
  44. entry_points={
  45. 'sqlalchemy.dialects': [
  46. 'access = sqlalchemy_access.pyodbc:AccessDialect_pyodbc',
  47. 'access.pyodbc = sqlalchemy_access.pyodbc:AccessDialect_pyodbc',
  48. ]
  49. }
  50. Above, the two entrypoints ``access`` and ``access.pyodbc`` allow URLs to be
  51. used such as::
  52. create_engine("access://user:pw@dsn")
  53. create_engine("access+pyodbc://user:pw@dsn")
  54. * setup.cfg - this file contains the traditional contents such as [egg_info],
  55. and [tool:pytest] directives, but also contains new directives that are used
  56. by SQLAlchemy's testing framework. E.g. for Access::
  57. [egg_info]
  58. tag_build = dev
  59. [tool:pytest]
  60. addopts= --tb native -v -r fxX --maxfail=25 -p no:warnings
  61. python_files=test/*test_*.py
  62. [sqla_testing]
  63. requirement_cls=sqlalchemy_access.requirements:Requirements
  64. profile_file=test/profiles.txt
  65. [db]
  66. default=access+pyodbc://admin@access_test
  67. sqlite=sqlite:///:memory:
  68. Above, the ``[sqla_testing]`` section contains configuration used by
  69. SQLAlchemy's test plugin. The ``[tool:pytest]`` section
  70. include directives to help with these runners. When using pytest
  71. the test/conftest.py file will bootstrap SQLAlchemy's plugin.
  72. * test/conftest.py - This script bootstraps SQLAlchemy's pytest plugin
  73. into the pytest runner. This
  74. script can also be used to install your third party dialect into
  75. SQLAlchemy without using the setuptools entrypoint system; this allows
  76. your dialect to be present without any explicit setup.py step needed.
  77. The other portion invokes SQLAlchemy's pytest plugin::
  78. from sqlalchemy.dialects import registry
  79. registry.register("access", "sqlalchemy_access.pyodbc", "AccessDialect_pyodbc")
  80. registry.register("access.pyodbc", "sqlalchemy_access.pyodbc", "AccessDialect_pyodbc")
  81. from sqlalchemy.testing.plugin.pytestplugin import *
  82. Where above, the ``registry`` module, introduced in SQLAlchemy 0.8, provides
  83. an in-Python means of installing the dialect entrypoints without the use
  84. of setuptools, using the ``registry.register()`` function in a way that
  85. is similar to the ``entry_points`` directive we placed in our ``setup.py``.
  86. * requirements.py - The ``requirements.py`` file is where directives
  87. regarding database and dialect capabilities are set up.
  88. SQLAlchemy's tests are often annotated with decorators that mark
  89. tests as "skip" or "fail" for particular backends. Over time, this
  90. system has been refined such that specific database and DBAPI names
  91. are mentioned less and less, in favor of @requires directives which
  92. state a particular capability. The requirement directive is linked
  93. to target dialects using a ``Requirements`` subclass. The custom
  94. ``Requirements`` subclass is specified in the ``requirements.py`` file
  95. and is made available to SQLAlchemy's test runner using the
  96. ``requirement_cls`` directive inside the ``[sqla_testing]`` section.
  97. For a third-party dialect, the custom ``Requirements`` class can
  98. usually specify a simple yes/no answer for a particular system. For
  99. example, a requirements file that specifies a database that supports
  100. the RETURNING construct but does not support nullable boolean
  101. columns might look like this::
  102. # sqlalchemy_access/requirements.py
  103. from sqlalchemy.testing.requirements import SuiteRequirements
  104. from sqlalchemy.testing import exclusions
  105. class Requirements(SuiteRequirements):
  106. @property
  107. def table_reflection(self):
  108. return exclusions.closed()
  109. @property
  110. def returning(self):
  111. return exclusions.open()
  112. The ``SuiteRequirements`` class in
  113. ``sqlalchemy.testing.requirements`` contains a large number of
  114. requirements rules, which attempt to have reasonable defaults. The
  115. tests will report on those requirements found as they are run.
  116. The requirements system can also be used when running SQLAlchemy's
  117. primary test suite against the external dialect. In this use case,
  118. a ``--dburi`` as well as a ``--requirements`` flag are passed to SQLAlchemy's
  119. test runner so that exclusions specific to the dialect take place::
  120. cd /path/to/sqlalchemy
  121. pytest -v \
  122. --requirements sqlalchemy_access.requirements:Requirements \
  123. --dburi access+pyodbc://admin@access_test
  124. * test_suite.py - Finally, the ``test_suite.py`` module represents a
  125. stub test suite, which pulls in the actual SQLAlchemy test suite.
  126. To pull in the suite as a whole, it can be imported in one step::
  127. # test/test_suite.py
  128. from sqlalchemy.testing.suite import *
  129. That's all that's needed - the ``sqlalchemy.testing.suite`` package
  130. contains an ever expanding series of tests, most of which should be
  131. annotated with specific requirement decorators so that they can be
  132. fully controlled. To specifically modify some of the tests, they can
  133. be imported by name and subclassed::
  134. from sqlalchemy.testing.suite import *
  135. from sqlalchemy.testing.suite import ComponentReflectionTest as _ComponentReflectionTest
  136. class ComponentReflectionTest(_ComponentReflectionTest):
  137. @classmethod
  138. def define_views(cls, metadata, schema):
  139. # bypass the "define_views" section of the
  140. # fixture
  141. return
  142. Going Forward
  143. ==============
  144. The third-party dialect can be distributed like any other Python
  145. module on PyPI. Links to prominent dialects can be featured within
  146. SQLAlchemy's own documentation; contact the developers (see AUTHORS)
  147. for help with this.
  148. While SQLAlchemy includes many dialects built in, it remains to be
  149. seen if the project as a whole might move towards "plugin" model for
  150. all dialects, including all those currently built in. Now that
  151. SQLAlchemy's dialect API is mature and the test suite is not far
  152. behind, it may be that a better maintenance experience can be
  153. delivered by having all dialects separately maintained and released.
  154. As new versions of SQLAlchemy are released, the test suite and
  155. requirements file will receive new tests and changes. The dialect
  156. maintainer would normally keep track of these changes and make
  157. adjustments as needed.