catalog_test.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. package sqlite
  2. import (
  3. "strconv"
  4. "strings"
  5. "testing"
  6. "github.com/kyleconroy/sqlc/internal/sql/ast"
  7. "github.com/kyleconroy/sqlc/internal/sql/catalog"
  8. "github.com/google/go-cmp/cmp"
  9. "github.com/google/go-cmp/cmp/cmpopts"
  10. )
  11. func TestUpdate(t *testing.T) {
  12. p := NewParser()
  13. for i, tc := range []struct {
  14. stmt string
  15. s *catalog.Schema
  16. }{
  17. {
  18. `
  19. CREATE TABLE foo (bar text);
  20. `,
  21. &catalog.Schema{
  22. Name: "main",
  23. Tables: []*catalog.Table{
  24. {
  25. Rel: &ast.TableName{Name: "foo"},
  26. Columns: []*catalog.Column{
  27. {
  28. Name: "bar",
  29. Type: ast.TypeName{Name: "text"},
  30. },
  31. },
  32. },
  33. },
  34. },
  35. },
  36. {
  37. `
  38. CREATE TABLE foo (bar text);
  39. ALTER TABLE foo RENAME TO baz;
  40. `,
  41. &catalog.Schema{
  42. Name: "main",
  43. Tables: []*catalog.Table{
  44. {
  45. Rel: &ast.TableName{Name: "baz"},
  46. Columns: []*catalog.Column{
  47. {
  48. Name: "bar",
  49. Type: ast.TypeName{Name: "text"},
  50. },
  51. },
  52. },
  53. },
  54. },
  55. },
  56. {
  57. `
  58. CREATE TABLE foo (bar text);
  59. ALTER TABLE foo ADD COLUMN baz bool;
  60. `,
  61. &catalog.Schema{
  62. Name: "main",
  63. Tables: []*catalog.Table{
  64. {
  65. Rel: &ast.TableName{Name: "foo"},
  66. Columns: []*catalog.Column{
  67. {
  68. Name: "bar",
  69. Type: ast.TypeName{Name: "text"},
  70. },
  71. {
  72. Name: "baz",
  73. Type: ast.TypeName{Name: "bool"},
  74. },
  75. },
  76. },
  77. },
  78. },
  79. },
  80. {
  81. `
  82. CREATE TABLE foo (bar text);
  83. ALTER TABLE foo RENAME COLUMN bar TO baz;
  84. `,
  85. &catalog.Schema{
  86. Name: "main",
  87. Tables: []*catalog.Table{
  88. {
  89. Rel: &ast.TableName{Name: "foo"},
  90. Columns: []*catalog.Column{
  91. {
  92. Name: "baz",
  93. Type: ast.TypeName{Name: "text"},
  94. },
  95. },
  96. },
  97. },
  98. },
  99. },
  100. {
  101. `
  102. CREATE TABLE foo (bar text);
  103. ALTER TABLE foo RENAME bar TO baz;
  104. `,
  105. &catalog.Schema{
  106. Name: "main",
  107. Tables: []*catalog.Table{
  108. {
  109. Rel: &ast.TableName{Name: "foo"},
  110. Columns: []*catalog.Column{
  111. {
  112. Name: "baz",
  113. Type: ast.TypeName{Name: "text"},
  114. },
  115. },
  116. },
  117. },
  118. },
  119. },
  120. {
  121. `
  122. ATTACH ':memory:' as ns;
  123. CREATE TABLE ns.foo (bar text);
  124. `,
  125. &catalog.Schema{
  126. Name: "ns",
  127. Tables: []*catalog.Table{
  128. {
  129. Rel: &ast.TableName{Schema: "ns", Name: "foo"},
  130. Columns: []*catalog.Column{
  131. {
  132. Name: "bar",
  133. Type: ast.TypeName{Name: "text"},
  134. },
  135. },
  136. },
  137. },
  138. },
  139. },
  140. {
  141. `
  142. ATTACH ':memory:' as ns;
  143. CREATE TABLE ns.foo (bar text);
  144. ALTER TABLE ns.foo RENAME TO baz;
  145. `,
  146. &catalog.Schema{
  147. Name: "ns",
  148. Tables: []*catalog.Table{
  149. {
  150. Rel: &ast.TableName{Schema: "ns", Name: "baz"},
  151. Columns: []*catalog.Column{
  152. {
  153. Name: "bar",
  154. Type: ast.TypeName{Name: "text"},
  155. },
  156. },
  157. },
  158. },
  159. },
  160. },
  161. {
  162. `
  163. ATTACH ':memory:' as ns;
  164. CREATE TABLE ns.foo (bar text);
  165. ALTER TABLE ns.foo ADD COLUMN baz bool;
  166. `,
  167. &catalog.Schema{
  168. Name: "ns",
  169. Tables: []*catalog.Table{
  170. {
  171. Rel: &ast.TableName{Schema: "ns", Name: "foo"},
  172. Columns: []*catalog.Column{
  173. {
  174. Name: "bar",
  175. Type: ast.TypeName{Name: "text"},
  176. },
  177. {
  178. Name: "baz",
  179. Type: ast.TypeName{Name: "bool"},
  180. },
  181. },
  182. },
  183. },
  184. },
  185. },
  186. {
  187. `
  188. ATTACH ':memory:' as ns;
  189. CREATE TABLE ns.foo (bar text);
  190. ALTER TABLE ns.foo RENAME COLUMN bar TO baz;
  191. `,
  192. &catalog.Schema{
  193. Name: "ns",
  194. Tables: []*catalog.Table{
  195. {
  196. Rel: &ast.TableName{Schema: "ns", Name: "foo"},
  197. Columns: []*catalog.Column{
  198. {
  199. Name: "baz",
  200. Type: ast.TypeName{Name: "text"},
  201. },
  202. },
  203. },
  204. },
  205. },
  206. },
  207. {
  208. `
  209. ATTACH ':memory:' as ns;
  210. CREATE TABLE ns.foo (bar text);
  211. ALTER TABLE ns.foo RENAME bar TO baz;
  212. `,
  213. &catalog.Schema{
  214. Name: "ns",
  215. Tables: []*catalog.Table{
  216. {
  217. Rel: &ast.TableName{Schema: "ns", Name: "foo"},
  218. Columns: []*catalog.Column{
  219. {
  220. Name: "baz",
  221. Type: ast.TypeName{Name: "text"},
  222. },
  223. },
  224. },
  225. },
  226. },
  227. },
  228. } {
  229. test := tc
  230. t.Run(strconv.Itoa(i), func(t *testing.T) {
  231. stmts, err := p.Parse(strings.NewReader(test.stmt))
  232. if err != nil {
  233. t.Log(test.stmt)
  234. t.Fatal(err)
  235. }
  236. c := newTestCatalog()
  237. if err := c.Build(stmts); err != nil {
  238. t.Log(test.stmt)
  239. t.Fatal(err)
  240. }
  241. e := newTestCatalog()
  242. if test.s != nil {
  243. var replaced bool
  244. for i := range e.Schemas {
  245. if e.Schemas[i].Name == test.s.Name {
  246. e.Schemas[i] = test.s
  247. replaced = true
  248. break
  249. }
  250. }
  251. if !replaced {
  252. e.Schemas = append(e.Schemas, test.s)
  253. }
  254. }
  255. if diff := cmp.Diff(e, c, cmpopts.EquateEmpty()); diff != "" {
  256. t.Log(test.stmt)
  257. t.Errorf("catalog mismatch:\n%s", diff)
  258. }
  259. })
  260. }
  261. }