/test/sql/test_identity_column.py

https://bitbucket.org/zzzeek/sqlalchemy · Python · 331 lines · 290 code · 36 blank · 5 comment · 7 complexity · bc4b5ff6b6657c9c41341dbd9fbc71da MD5 · raw file

  1. import re
  2. from sqlalchemy import Column
  3. from sqlalchemy import Identity
  4. from sqlalchemy import Integer
  5. from sqlalchemy import MetaData
  6. from sqlalchemy import Sequence
  7. from sqlalchemy import Table
  8. from sqlalchemy import testing
  9. from sqlalchemy.engine import URL
  10. from sqlalchemy.exc import ArgumentError
  11. from sqlalchemy.schema import CreateTable
  12. from sqlalchemy.testing import assert_raises_message
  13. from sqlalchemy.testing import fixtures
  14. from sqlalchemy.testing import is_
  15. from sqlalchemy.testing import is_not_
  16. class _IdentityDDLFixture(testing.AssertsCompiledSQL):
  17. __backend__ = True
  18. @testing.combinations(
  19. (dict(always=True), "ALWAYS AS IDENTITY"),
  20. (
  21. dict(always=False, start=5),
  22. "BY DEFAULT AS IDENTITY (START WITH 5)",
  23. ),
  24. (
  25. dict(always=True, increment=2),
  26. "ALWAYS AS IDENTITY (INCREMENT BY 2)",
  27. ),
  28. (
  29. dict(increment=2, start=5),
  30. "BY DEFAULT AS IDENTITY (INCREMENT BY 2 START WITH 5)",
  31. ),
  32. (
  33. dict(always=True, increment=2, start=0, minvalue=0),
  34. "ALWAYS AS IDENTITY (INCREMENT BY 2 START WITH 0 MINVALUE 0)",
  35. ),
  36. (
  37. dict(always=False, increment=2, start=1, maxvalue=5),
  38. "BY DEFAULT AS IDENTITY (INCREMENT BY 2 START WITH 1 MAXVALUE 5)",
  39. ),
  40. (
  41. dict(always=True, increment=2, start=1, nomaxvalue=True),
  42. "ALWAYS AS IDENTITY (INCREMENT BY 2 START WITH 1 NO MAXVALUE)",
  43. ),
  44. (
  45. dict(always=False, increment=2, start=0, nominvalue=True),
  46. "BY DEFAULT AS IDENTITY "
  47. "(INCREMENT BY 2 START WITH 0 NO MINVALUE)",
  48. ),
  49. (
  50. dict(always=True, start=1, maxvalue=10, cycle=True),
  51. "ALWAYS AS IDENTITY (START WITH 1 MAXVALUE 10 CYCLE)",
  52. ),
  53. (
  54. dict(always=False, cache=1000, order=True),
  55. "BY DEFAULT AS IDENTITY (CACHE 1000 ORDER)",
  56. ),
  57. (dict(order=True, cycle=True), "BY DEFAULT AS IDENTITY (ORDER CYCLE)"),
  58. (
  59. dict(order=False, cycle=False),
  60. "BY DEFAULT AS IDENTITY (NO ORDER NO CYCLE)",
  61. ),
  62. )
  63. def test_create_ddl(self, identity_args, text):
  64. if getattr(
  65. self, "__dialect__", None
  66. ) != "default_enhanced" and testing.against("oracle"):
  67. text = text.replace("NO MINVALUE", "NOMINVALUE")
  68. text = text.replace("NO MAXVALUE", "NOMAXVALUE")
  69. text = text.replace("NO CYCLE", "NOCYCLE")
  70. text = text.replace("NO ORDER", "NOORDER")
  71. t = Table(
  72. "foo_table",
  73. MetaData(),
  74. Column("foo", Integer(), Identity(**identity_args)),
  75. )
  76. self.assert_compile(
  77. CreateTable(t),
  78. "CREATE TABLE foo_table (foo INTEGER GENERATED %s)" % text,
  79. )
  80. t2 = t.to_metadata(MetaData())
  81. self.assert_compile(
  82. CreateTable(t2),
  83. "CREATE TABLE foo_table (foo INTEGER GENERATED %s)" % text,
  84. )
  85. def test_other_options(self):
  86. t = Table(
  87. "foo_table",
  88. MetaData(),
  89. Column(
  90. "foo",
  91. Integer(),
  92. Identity(always=True, start=3),
  93. nullable=False,
  94. unique=True,
  95. ),
  96. )
  97. self.assert_compile(
  98. CreateTable(t),
  99. "CREATE TABLE foo_table ("
  100. "foo INTEGER GENERATED ALWAYS AS IDENTITY (START "
  101. "WITH 3), UNIQUE (foo))",
  102. )
  103. def test_autoincrement_true(self):
  104. t = Table(
  105. "foo_table",
  106. MetaData(),
  107. Column(
  108. "foo",
  109. Integer(),
  110. Identity(always=True, start=3),
  111. primary_key=True,
  112. autoincrement=True,
  113. ),
  114. )
  115. self.assert_compile(
  116. CreateTable(t),
  117. "CREATE TABLE foo_table ("
  118. "foo INTEGER GENERATED ALWAYS AS IDENTITY (START WITH 3)"
  119. ", PRIMARY KEY (foo))",
  120. )
  121. def test_nullable_kwarg(self):
  122. t = Table(
  123. "t",
  124. MetaData(),
  125. Column("a", Integer(), Identity(), nullable=False),
  126. Column("b", Integer(), Identity(), nullable=True),
  127. Column("c", Integer(), Identity()),
  128. )
  129. is_(t.c.a.nullable, False)
  130. is_(t.c.b.nullable, True)
  131. is_(t.c.c.nullable, False)
  132. nullable = ""
  133. if getattr(
  134. self, "__dialect__", None
  135. ) != "default_enhanced" and testing.against("postgresql"):
  136. nullable = " NULL"
  137. self.assert_compile(
  138. CreateTable(t),
  139. (
  140. "CREATE TABLE t ("
  141. "a INTEGER GENERATED BY DEFAULT AS IDENTITY, "
  142. "b INTEGER GENERATED BY DEFAULT AS IDENTITY%s, "
  143. "c INTEGER GENERATED BY DEFAULT AS IDENTITY"
  144. ")"
  145. )
  146. % nullable,
  147. )
  148. class IdentityDDL(_IdentityDDLFixture, fixtures.TestBase):
  149. # this uses the connection dialect
  150. __requires__ = ("identity_columns_standard",)
  151. def test_on_null(self):
  152. t = Table(
  153. "foo_table",
  154. MetaData(),
  155. Column(
  156. "foo",
  157. Integer(),
  158. Identity(always=False, on_null=True, start=42, order=True),
  159. ),
  160. )
  161. text = " ON NULL" if testing.against("oracle") else ""
  162. self.assert_compile(
  163. CreateTable(t),
  164. (
  165. "CREATE TABLE foo_table (foo INTEGER GENERATED BY DEFAULT"
  166. + text
  167. + " AS IDENTITY (START WITH 42 ORDER))"
  168. ),
  169. )
  170. class DefaultDialectIdentityDDL(_IdentityDDLFixture, fixtures.TestBase):
  171. # this uses the default dialect
  172. __dialect__ = "default_enhanced"
  173. class NotSupportingIdentityDDL(testing.AssertsCompiledSQL, fixtures.TestBase):
  174. def get_dialect(self, dialect):
  175. dd = URL.create(dialect).get_dialect()()
  176. if dialect in {"oracle", "postgresql"}:
  177. dd.supports_identity_columns = False
  178. return dd
  179. @testing.combinations("sqlite", "mysql", "mariadb", "postgresql", "oracle")
  180. def test_identity_is_ignored(self, dialect):
  181. t = Table(
  182. "foo_table",
  183. MetaData(),
  184. Column("foo", Integer(), Identity("always", start=3)),
  185. )
  186. t_exp = Table(
  187. "foo_table",
  188. MetaData(),
  189. Column("foo", Integer(), nullable=False),
  190. )
  191. dialect = self.get_dialect(dialect)
  192. exp = CreateTable(t_exp).compile(dialect=dialect).string
  193. self.assert_compile(
  194. CreateTable(t), re.sub(r"[\n\t]", "", exp), dialect=dialect
  195. )
  196. @testing.combinations(
  197. "sqlite",
  198. "mysql",
  199. "mariadb",
  200. "postgresql",
  201. "oracle",
  202. argnames="dialect",
  203. )
  204. @testing.combinations(True, "auto", argnames="autoincrement")
  205. def test_identity_is_ignored_in_pk(self, dialect, autoincrement):
  206. t = Table(
  207. "foo_table",
  208. MetaData(),
  209. Column(
  210. "foo",
  211. Integer(),
  212. Identity("always", start=3),
  213. primary_key=True,
  214. autoincrement=autoincrement,
  215. ),
  216. )
  217. t_exp = Table(
  218. "foo_table",
  219. MetaData(),
  220. Column(
  221. "foo", Integer(), primary_key=True, autoincrement=autoincrement
  222. ),
  223. )
  224. dialect = self.get_dialect(dialect)
  225. exp = CreateTable(t_exp).compile(dialect=dialect).string
  226. self.assert_compile(
  227. CreateTable(t), re.sub(r"[\n\t]", "", exp), dialect=dialect
  228. )
  229. class IdentityTest(fixtures.TestBase):
  230. def test_server_default_onupdate(self):
  231. text = (
  232. "A column with an Identity object cannot specify a "
  233. "server_default or a server_onupdate argument"
  234. )
  235. def fn(**kwargs):
  236. Table(
  237. "t",
  238. MetaData(),
  239. Column("y", Integer, Identity(), **kwargs),
  240. )
  241. assert_raises_message(ArgumentError, text, fn, server_default="42")
  242. assert_raises_message(ArgumentError, text, fn, server_onupdate="42")
  243. def test_to_metadata(self):
  244. identity1 = Identity("by default", on_null=True, start=123)
  245. m = MetaData()
  246. t = Table(
  247. "t", m, Column("x", Integer), Column("y", Integer, identity1)
  248. )
  249. is_(identity1.column, t.c.y)
  250. # is_(t.c.y.server_onupdate, identity1)
  251. is_(t.c.y.server_default, identity1)
  252. m2 = MetaData()
  253. t2 = t.to_metadata(m2)
  254. identity2 = t2.c.y.server_default
  255. is_not_(identity1, identity2)
  256. is_(identity1.column, t.c.y)
  257. # is_(t.c.y.server_onupdate, identity1)
  258. is_(t.c.y.server_default, identity1)
  259. is_(identity2.column, t2.c.y)
  260. # is_(t2.c.y.server_onupdate, identity2)
  261. is_(t2.c.y.server_default, identity2)
  262. def test_autoincrement_column(self):
  263. t = Table(
  264. "t",
  265. MetaData(),
  266. Column("y", Integer, Identity(), primary_key=True),
  267. )
  268. assert t._autoincrement_column is t.c.y
  269. t2 = Table("t2", MetaData(), Column("y", Integer, Identity()))
  270. assert t2._autoincrement_column is None
  271. def test_identity_and_sequence(self):
  272. def go():
  273. return Table(
  274. "foo_table",
  275. MetaData(),
  276. Column("foo", Integer(), Identity(), Sequence("foo_seq")),
  277. )
  278. assert_raises_message(
  279. ArgumentError,
  280. "An column cannot specify both Identity and Sequence.",
  281. go,
  282. )
  283. def test_identity_autoincrement_false(self):
  284. def go():
  285. return Table(
  286. "foo_table",
  287. MetaData(),
  288. Column("foo", Integer(), Identity(), autoincrement=False),
  289. )
  290. assert_raises_message(
  291. ArgumentError,
  292. "A column with an Identity object cannot specify "
  293. "autoincrement=False",
  294. go,
  295. )