orms.slt 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. # Copyright 2015 - 2019 The Cockroach Authors. All rights reserved.
  2. # Copyright Materialize, Inc. and contributors. All rights reserved.
  3. #
  4. # Use of this software is governed by the Business Source License
  5. # included in the LICENSE file at the root of this repository.
  6. #
  7. # As of the Change Date specified in that file, in accordance with
  8. # the Business Source License, use of this software will be governed
  9. # by the Apache License, Version 2.0.
  10. #
  11. # This file is derived from the logic test suite in CockroachDB. The
  12. # original file was retrieved on June 10, 2019 from:
  13. #
  14. # https://github.com/cockroachdb/cockroach/blob/d2f7fbf5dd1fc1a099bbad790a2e1f7c60a66cc3/pkg/sql/logictest/testdata/logic_test/orms
  15. #
  16. # The original source code is subject to the terms of the Apache
  17. # 2.0 license, a copy of which can be found in the LICENSE file at the
  18. # root of this repository.
  19. # not supported yet
  20. halt
  21. mode cockroach
  22. ## This test file contains various complex queries that ORMs issue during
  23. ## startup or general use.
  24. ## 12151
  25. statement ok
  26. CREATE TABLE a (id int UNIQUE, name string)
  27. query TTTBOI
  28. SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod
  29. FROM pg_attribute a
  30. LEFT JOIN pg_attrdef d
  31. ON a.attrelid = d.adrelid
  32. AND a.attnum = d.adnum
  33. WHERE a.attrelid = 'a'::regclass
  34. AND a.attnum > 0 AND NOT a.attisdropped
  35. ORDER BY a.attnum
  36. ----
  37. id bigint NULL false 20 -1
  38. name text NULL false 25 -1
  39. rowid bigint unique_rowid() true 20 -1
  40. # materialize#12115
  41. # Skipped until database-issues#7510 is solved
  42. #query TT
  43. #SELECT t.typname enum_name, array_agg(e.enumlabel ORDER BY enumsortorder) enum_value
  44. # FROM pg_type t
  45. # JOIN pg_enum e ON t.oid = e.enumtypid
  46. # JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
  47. # WHERE n.nspname = 'public'
  48. # GROUP BY 1
  49. #----
  50. ## 12207
  51. statement ok
  52. CREATE TABLE customers (
  53. name STRING PRIMARY KEY,
  54. id INT,
  55. INDEX (id)
  56. )
  57. statement ok
  58. INSERT INTO customers VALUES ('jordan', 12), ('cuong', 13)
  59. query TBBTTTT colnames
  60. SELECT i.relname AS name,
  61. ix.indisprimary AS PRIMARY,
  62. ix.indisunique AS UNIQUE,
  63. ix.indkey AS indkey,
  64. array_agg(a.attnum) AS column_indexes,
  65. array_agg(a.attname) AS column_names,
  66. pg_get_indexdef(ix.indexrelid) AS definition
  67. FROM pg_class t,
  68. pg_class i,
  69. pg_index ix,
  70. pg_attribute a
  71. WHERE t.oid = ix.indrelid
  72. AND i.oid = ix.indexrelid
  73. AND a.attrelid = t.oid
  74. AND t.relkind = 'r'
  75. AND t.relname = 'customers' -- this query is run once for each table
  76. GROUP BY i.relname,
  77. ix.indexrelid,
  78. ix.indisprimary,
  79. ix.indisunique,
  80. ix.indkey
  81. ORDER BY i.relname
  82. ----
  83. name primary unique indkey column_indexes column_names definition
  84. customers_id_idx false false 2 {1,2} {name,id} CREATE INDEX customers_id_idx ON test.public.customers (id ASC)
  85. primary true true 1 {1,2} {name,id} CREATE UNIQUE INDEX "primary" ON test.public.customers (name ASC)
  86. query TT colnames
  87. SELECT a.attname, format_type(a.atttypid, a.atttypmod) AS data_type
  88. FROM pg_index i
  89. JOIN pg_attribute a ON a.attrelid = i.indrelid
  90. AND a.attnum = ANY(i.indkey)
  91. WHERE i.indrelid = '"a"'::regclass
  92. AND i.indisprimary
  93. ----
  94. attname data_type
  95. rowid bigint
  96. statement ok
  97. CREATE TABLE b (id INT, a_id INT, FOREIGN KEY (a_id) REFERENCES a (id))
  98. # ActiveRecord query for foreign keys
  99. # https://github.com/rails/rails/blob/355a2fcf/activerecord/lib/active_record/connection_adapters/postgresql/schema_statements.rb#L583
  100. query TTTTTT
  101. SELECT t2.oid::regclass::text AS to_table, a1.attname AS column, a2.attname AS primary_key, c.conname AS name, c.confupdtype AS on_update, c.confdeltype AS on_delete
  102. FROM pg_constraint c
  103. JOIN pg_class t1 ON c.conrelid = t1.oid
  104. JOIN pg_class t2 ON c.confrelid = t2.oid
  105. JOIN pg_attribute a1 ON a1.attnum = c.conkey[1] AND a1.attrelid = t1.oid
  106. JOIN pg_attribute a2 ON a2.attnum = c.confkey[1] AND a2.attrelid = t2.oid
  107. JOIN pg_namespace t3 ON c.connamespace = t3.oid
  108. WHERE c.contype = 'f'
  109. AND t1.relname ='b'
  110. AND t3.nspname = ANY (current_schemas(false))
  111. ORDER BY c.conname
  112. ----
  113. a a_id id fk_a_id_ref_a a a
  114. # Default value columns in Rails produce these kinds of queries:
  115. query O
  116. SELECT 'decimal(18,2)'::regtype::oid
  117. ----
  118. 1700
  119. # NOTE: Before 19.2, this returned 25 (oid.T_text), but due to updates to the
  120. # type system to more correctly handle OIDs, this now returns 1043
  121. # (oid.T_varchar), which is what PG returns.
  122. query O
  123. SELECT 'character varying'::regtype::oid
  124. ----
  125. 1043
  126. statement ok
  127. CREATE INDEX b_idx ON b(a_id);
  128. # ActiveRecord 4.2.x query for checking if an index exists
  129. # Relies on OID IN tuple support
  130. query I
  131. SELECT count(*)
  132. FROM pg_class t
  133. INNER JOIN pg_index d ON t.oid = d.indrelid
  134. INNER JOIN pg_class i ON d.indexrelid = i.oid
  135. WHERE i.relkind = 'i'
  136. AND i.relname = 'b_idx'
  137. AND t.relname = 'b'
  138. AND i.relnamespace IN (SELECT oid FROM pg_namespace WHERE nspname = ANY (current_schemas(false)))
  139. ----
  140. 1