-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathsqlite_ecto_test.exs
1310 lines (1060 loc) · 53.6 KB
/
sqlite_ecto_test.exs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
defmodule Sqlite.Ecto2.Test do
use ExUnit.Case, async: true
# IMPORTANT: This is closely modeled on Ecto's postgres_test.exs file.
# We strive to avoid structural differences between that file and this one.
alias Ecto.Integration.Post
alias Ecto.Integration.TestRepo
alias Ecto.Migration.Table
alias Sqlite.DbConnection.Query
alias Sqlite.Ecto2.Connection, as: SQL
import Ecto.Query
describe "storage_up" do
test "fails with :already_up on second call" do
tmp = [database: tempfilename()]
assert Sqlite.Ecto2.storage_up(tmp) == :ok
assert File.exists? tmp[:database]
assert Sqlite.Ecto2.storage_up(tmp) == {:error, :already_up}
File.rm(tmp[:database])
end
test "fails with helpful error message if no database specified" do
assert_raise ArgumentError,
"""
No SQLite database path specified. Please check the configuration for your Repo.
Your config/*.exs file should have something like this in it:
config :my_app, MyApp.Repo,
adapter: Sqlite.Ecto2,
database: "/path/to/sqlite/database"
Options provided were:
[mumble: "no database here"]
""",
fn -> Sqlite.Ecto2.storage_up([mumble: "no database here"]) == :ok end
end
end
test "storage down (twice)" do
tmp = [database: tempfilename()]
assert Sqlite.Ecto2.storage_up(tmp) == :ok
assert Sqlite.Ecto2.storage_down(tmp) == :ok
refute File.exists? tmp[:database]
assert Sqlite.Ecto2.storage_down(tmp) == {:error, :already_down}
end
test "storage up creates directory" do
dir = "/tmp/my_sqlite_ecto_directory/"
File.rm_rf! dir
tmp = [database: dir <> tempfilename()]
:ok = Sqlite.Ecto2.storage_up(tmp)
assert File.exists?(dir <> "tmp/") && File.dir?(dir <> "tmp/")
end
# return a unique temporary filename
defp tempfilename do
1..10
|> Enum.map(fn(_) -> :rand.uniform(10) - 1 end)
|> Enum.join
|> (fn(name) -> "/tmp/test_" <> name <> ".db" end).()
end
import Ecto.Query
alias Ecto.Queryable
defmodule Schema do
use Ecto.Schema
schema "schema" do
field :x, :integer
field :y, :integer
field :z, :integer
has_many :comments, Sqlite.Ecto2.Test.Schema2,
references: :x,
foreign_key: :z
has_one :permalink, Sqlite.Ecto2.Test.Schema3,
references: :y,
foreign_key: :id
end
end
defmodule SchemaWithArray do
use Ecto.Schema
schema "schema" do
field :x, :integer
field :y, :integer
field :z, :integer
field :w, {:array, :integer}
end
end
defmodule Schema2 do
use Ecto.Schema
schema "schema2" do
belongs_to :post, Sqlite.Ecto2.Test.Schema,
references: :x,
foreign_key: :z
end
end
defmodule Schema3 do
use Ecto.Schema
schema "schema3" do
field :list1, {:array, :string}
field :list2, {:array, :integer}
field :binary, :binary
end
end
defp normalize(query, operation \\ :all, counter \\ 0) do
{query, _params, _key} = Ecto.Query.Planner.prepare(query, operation, Sqlite.Ecto2, counter)
{query, _} = Ecto.Query.Planner.normalize(query, operation, Sqlite.Ecto2, counter)
query
end
defp all(query), do: query |> SQL.all |> IO.iodata_to_binary()
defp update_all(query), do: query |> SQL.update_all |> IO.iodata_to_binary()
defp delete_all(query), do: query |> SQL.delete_all |> IO.iodata_to_binary()
defp execute_ddl(query), do: query |> SQL.execute_ddl |> Enum.map(&IO.iodata_to_binary/1)
defp insert(prefx, table, header, rows, on_conflict, returning) do
IO.iodata_to_binary SQL.insert(prefx, table, header, rows, on_conflict, returning)
end
defp update(prefx, table, fields, filter, returning) do
IO.iodata_to_binary SQL.update(prefx, table, fields, filter, returning)
end
defp delete(prefx, table, filter, returning) do
IO.iodata_to_binary SQL.delete(prefx, table, filter, returning)
end
test "from" do
query = Schema |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0}
end
test "from without schema" do
query = "posts" |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT p0."x" FROM "posts" AS p0}
query = "posts" |> select([:x]) |> normalize
assert all(query) == ~s{SELECT p0."x" FROM "posts" AS p0}
assert_raise Ecto.QueryError, ~r"SQLite does not support selecting all fields from \"posts\" without a schema", fn ->
all from(p in "posts", select: p) |> normalize()
end
end
test "from with subquery" do
query = subquery("posts" |> select([r], %{x: r.x, y: r.y})) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM (SELECT p0."x" AS "x", p0."y" AS "y" FROM "posts" AS p0) AS s0}
query = subquery("posts" |> select([r], %{x: r.x, z: r.y})) |> select([r], r) |> normalize
assert all(query) == ~s{SELECT s0."x", s0."z" FROM (SELECT p0."x" AS "x", p0."y" AS "z" FROM "posts" AS p0) AS s0}
end
test "select" do
query = Schema |> select([r], {r.x, r.y}) |> normalize
assert all(query) == ~s{SELECT s0."x", s0."y" FROM "schema" AS s0}
query = Schema |> select([r], [r.x, r.y]) |> normalize
assert all(query) == ~s{SELECT s0."x", s0."y" FROM "schema" AS s0}
query = Schema |> select([r], struct(r, [:x, :y])) |> normalize
assert all(query) == ~s{SELECT s0."x", s0."y" FROM "schema" AS s0}
end
test "aggregates" do
query = Schema |> select([r], count(r.x)) |> normalize
assert all(query) == ~s{SELECT count(s0."x") FROM "schema" AS s0}
query = Schema |> select([r], count(r.x, :distinct)) |> normalize
assert all(query) == ~s{SELECT count(DISTINCT s0."x") FROM "schema" AS s0}
end
test "distinct" do
assert_raise ArgumentError, "DISTINCT with multiple columns is not supported by SQLite", fn ->
query = Schema |> distinct([r], r.x) |> select([r], {r.x, r.y}) |> normalize
all(query)
end
assert_raise ArgumentError, "DISTINCT with multiple columns is not supported by SQLite", fn ->
query = Schema |> distinct([r], desc: r.x) |> select([r], {r.x, r.y}) |> normalize
all(query)
end
assert_raise ArgumentError, "DISTINCT with multiple columns is not supported by SQLite", fn ->
query = Schema |> distinct([r], 2) |> select([r], r.x) |> normalize
all(query)
end
assert_raise ArgumentError, "DISTINCT with multiple columns is not supported by SQLite", fn ->
query = Schema |> distinct([r], [r.x, r.y]) |> select([r], {r.x, r.y}) |> normalize
all(query)
end
query = Schema |> distinct([r], true) |> select([r], {r.x, r.y}) |> normalize
assert all(query) == ~s{SELECT DISTINCT s0."x", s0."y" FROM "schema" AS s0}
query = Schema |> distinct([r], false) |> select([r], {r.x, r.y}) |> normalize
assert all(query) == ~s{SELECT s0."x", s0."y" FROM "schema" AS s0}
query = Schema |> distinct(true) |> select([r], {r.x, r.y}) |> normalize
assert all(query) == ~s{SELECT DISTINCT s0."x", s0."y" FROM "schema" AS s0}
query = Schema |> distinct(false) |> select([r], {r.x, r.y}) |> normalize
assert all(query) == ~s{SELECT s0."x", s0."y" FROM "schema" AS s0}
end
test "distinct with order by" do
assert_raise ArgumentError, "DISTINCT with multiple columns is not supported by SQLite", fn ->
query = Schema |> order_by([r], [r.y]) |> distinct([r], desc: r.x) |> select([r], r.x) |> normalize
all(query)
end
end
test "where" do
query = Schema |> where([r], r.x == 42) |> where([r], r.y != 43) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 WHERE (s0."x" = 42) AND (s0."y" != 43)}
end
test "or_where" do
query = Schema |> or_where([r], r.x == 42) |> or_where([r], r.y != 43) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 WHERE (s0."x" = 42) OR (s0."y" != 43)}
query = Schema |> or_where([r], r.x == 42) |> or_where([r], r.y != 43) |> where([r], r.z == 44) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 WHERE ((s0."x" = 42) OR (s0."y" != 43)) AND (s0."z" = 44)}
end
test "order by" do
query = Schema |> order_by([r], r.x) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 ORDER BY s0."x"}
query = Schema |> order_by([r], [r.x, r.y]) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 ORDER BY s0."x", s0."y"}
query = Schema |> order_by([r], [asc: r.x, desc: r.y]) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 ORDER BY s0."x", s0."y" DESC}
query = Schema |> order_by([r], []) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0}
end
test "limit and offset" do
query = Schema |> limit([r], 3) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 LIMIT 3}
query = Schema |> offset([r], 5) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 OFFSET 5}
query = Schema |> offset([r], 5) |> limit([r], 3) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 LIMIT 3 OFFSET 5}
end
test "lock" do
assert_raise ArgumentError, "locks are not supported by SQLite", fn ->
query = Schema |> lock("FOR SHARE NOWAIT") |> select([], 0) |> normalize
all(query)
end
end
test "string escape" do
query = "schema" |> where(foo: "'\\ ") |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM \"schema\" AS s0 WHERE (s0.\"foo\" = '''\\ ')}
query = "schema" |> where(foo: "'") |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = '''')}
end
test "binary ops" do
query = Schema |> select([r], r.x == 2) |> normalize
assert all(query) == ~s{SELECT s0."x" = 2 FROM "schema" AS s0}
query = Schema |> select([r], r.x != 2) |> normalize
assert all(query) == ~s{SELECT s0."x" != 2 FROM "schema" AS s0}
query = Schema |> select([r], r.x <= 2) |> normalize
assert all(query) == ~s{SELECT s0."x" <= 2 FROM "schema" AS s0}
query = Schema |> select([r], r.x >= 2) |> normalize
assert all(query) == ~s{SELECT s0."x" >= 2 FROM "schema" AS s0}
query = Schema |> select([r], r.x < 2) |> normalize
assert all(query) == ~s{SELECT s0."x" < 2 FROM "schema" AS s0}
query = Schema |> select([r], r.x > 2) |> normalize
assert all(query) == ~s{SELECT s0."x" > 2 FROM "schema" AS s0}
end
test "is_nil" do
query = Schema |> select([r], is_nil(r.x)) |> normalize
assert all(query) == ~s{SELECT s0."x" IS NULL FROM "schema" AS s0}
query = Schema |> select([r], not is_nil(r.x)) |> normalize
assert all(query) == ~s{SELECT NOT (s0."x" IS NULL) FROM "schema" AS s0}
end
test "fragments" do
query = Schema |> select([r], fragment("ltrim(?)", r.x)) |> normalize
assert all(query) == ~s{SELECT ltrim(s0."x") FROM "schema" AS s0}
value = 13
query = Schema |> select([r], fragment("ltrim(?, ?)", r.x, ^value)) |> normalize
assert all(query) == ~s{SELECT ltrim(s0."x", ?1) FROM "schema" AS s0}
query = Schema |> select([], fragment(title: 2)) |> normalize
assert_raise Ecto.QueryError, ~r"SQLite adapter does not support keyword or interpolated fragments", fn ->
all(query)
end
end
test "literals" do
query = "schema" |> where(foo: true) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = 1)}
query = "schema" |> where(foo: false) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = 0)}
query = "schema" |> where(foo: "abc") |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = 'abc')}
query = "schema" |> where(foo: <<0, ?a, ?b, ?c>>) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = X'00616263')}
query = "schema" |> where(foo: 123) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = 123)}
query = "schema" |> where(foo: 123.0) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 WHERE (s0."foo" = 123.0)}
end
test "tagged type" do
query = Schema |> select([], type(^"601d74e4-a8d3-4b6e-8365-eddb4c893327", Ecto.UUID)) |> normalize
assert all(query) == ~s{SELECT CAST (?1 AS TEXT) FROM "schema" AS s0}
assert_raise ArgumentError, "Array type is not supported by SQLite", fn ->
query = Schema |> select([], type(^[1, 2, 3], {:array, :integer})) |> normalize
all(query)
end
end
test "nested expressions" do
z = 123
query = from(r in Schema, []) |> select([r], r.x > 0 and (r.y > ^(-z)) or true) |> normalize
assert all(query) == ~s{SELECT ((s0."x" > 0) AND (s0."y" > ?1)) OR 1 FROM "schema" AS s0}
end
test "in expression" do
query = Schema |> select([e], 1 in []) |> normalize
assert all(query) == ~s{SELECT 1 IN () FROM "schema" AS s0}
query = Schema |> select([e], 1 in [1, e.x, 3]) |> normalize
assert all(query) == ~s{SELECT 1 IN (1,s0."x",3) FROM "schema" AS s0}
query = Schema |> select([e], 1 in ^[]) |> normalize
assert all(query) == ~s{SELECT 1 IN () FROM "schema" AS s0}
query = Schema |> select([e], 1 in ^[1, 2, 3]) |> normalize
assert all(query) == ~s{SELECT 1 IN (?1,?2,?3) FROM "schema" AS s0}
query = Schema |> select([e], 1 in [1, ^2, 3]) |> normalize
assert all(query) == ~s{SELECT 1 IN (1,?1,3) FROM "schema" AS s0}
query = Schema |> select([e], ^1 in [1, ^2, 3]) |> normalize
assert all(query) == ~s{SELECT ?1 IN (1,?2,3) FROM "schema" AS s0}
query = Schema |> select([e], ^1 in ^[1, 2, 3]) |> normalize
assert all(query) == ~s{SELECT ?1 IN (?2,?3,?4) FROM "schema" AS s0}
# query = Schema |> select([e], 1 in e.w) |> normalize
# assert all(query) == ~s{SELECT 1 = ANY(s0."w") FROM "schema" AS s0}
# This assertion omitted because we can't support array values.
query = Schema |> select([e], 1 in fragment("foo")) |> normalize
assert all(query) == ~s{SELECT 1 IN (foo) FROM "schema" AS s0}
end
test "having" do
query = Schema |> having([p], p.x == p.x) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 HAVING (s0."x" = s0."x")}
query = Schema |> having([p], p.x == p.x) |> having([p], p.y == p.y) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 HAVING (s0."x" = s0."x") AND (s0."y" = s0."y")}
end
test "or_having" do
query = Schema |> or_having([p], p.x == p.x) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 HAVING (s0."x" = s0."x")}
query = Schema |> or_having([p], p.x == p.x) |> or_having([p], p.y == p.y) |> select([], true) |> normalize
assert all(query) == ~s{SELECT 1 FROM "schema" AS s0 HAVING (s0."x" = s0."x") OR (s0."y" = s0."y")}
end
test "group by" do
query = Schema |> group_by([r], r.x) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 GROUP BY s0."x"}
query = Schema |> group_by([r], 2) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 GROUP BY 2}
query = Schema |> group_by([r], [r.x, r.y]) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0 GROUP BY s0."x", s0."y"}
query = Schema |> group_by([r], []) |> select([r], r.x) |> normalize
assert all(query) == ~s{SELECT s0."x" FROM "schema" AS s0}
end
test "arrays and sigils" do
assert_raise ArgumentError, "Array values are not supported by SQLite", fn ->
query = Schema |> select([], fragment("?", [1, 2, 3])) |> normalize
all(query)
end
assert_raise ArgumentError, "Array values are not supported by SQLite", fn ->
query = Schema |> select([], fragment("?", ~w(abc def))) |> normalize
all(query)
end
end
test "interpolated values" do
query = "schema"
|> select([m], {m.id, ^true})
|> join(:inner, [], Schema2, fragment("?", ^true))
|> join(:inner, [], Schema2, fragment("?", ^false))
|> where([], fragment("?", ^true))
|> where([], fragment("?", ^false))
|> having([], fragment("?", ^true))
|> having([], fragment("?", ^false))
|> group_by([], fragment("?", ^1))
|> group_by([], fragment("?", ^2))
|> order_by([], fragment("?", ^3))
|> order_by([], ^:x)
|> limit([], ^4)
|> offset([], ^5)
|> normalize
result = remove_newlines """
SELECT s0."id", ?1 FROM "schema" AS s0 INNER JOIN "schema2" AS s1 ON ?2
INNER JOIN "schema2" AS s2 ON ?3 WHERE (?4) AND (?5)
GROUP BY ?6, ?7 HAVING (?8) AND (?9)
ORDER BY ?10, s0."x" LIMIT ?11 OFFSET ?12
"""
assert all(query) == result
end
test "fragments and types" do
query =
normalize from(e in "schema",
where: fragment("extract(? from ?) = ?", ^"month", e.start_time, type(^"4", :integer)),
where: fragment("extract(? from ?) = ?", ^"year", e.start_time, type(^"2015", :integer)),
select: true)
result =
"SELECT 1 FROM \"schema\" AS s0 " <>
"WHERE (extract(?1 from s0.\"start_time\") = ?2) " <>
"AND (extract(?3 from s0.\"start_time\") = ?4)"
assert all(query) == String.trim(result)
end
test "fragments allow ? to be escaped with backslash" do
query =
normalize from(e in "schema",
where: fragment("? = \"query\\?\"", e.start_time),
select: true)
result =
"SELECT 1 FROM \"schema\" AS s0 " <>
"WHERE (s0.\"start_time\" = \"query?\")"
assert all(query) == String.trim(result)
end
## *_all
test "update all" do
query = from(m in Schema, update: [set: [x: 0]]) |> normalize(:update_all)
assert update_all(query) ==
~s{UPDATE "schema" SET "x" = 0}
query = from(m in Schema, update: [set: [x: 0], inc: [y: 1, z: -3]]) |> normalize(:update_all)
assert update_all(query) ==
~s{UPDATE "schema" SET "x" = 0, "y" = "schema"."y" + 1, "z" = "schema"."z" + -3}
query = from(e in Schema, where: e.x == 123, update: [set: [x: 0]]) |> normalize(:update_all)
assert update_all(query) ==
~s{UPDATE "schema" SET "x" = 0 WHERE ("schema"."x" = 123)}
query = from(m in Schema, update: [set: [x: ^0]]) |> normalize(:update_all)
assert update_all(query) ==
~s{UPDATE "schema" SET "x" = ?1}
assert_raise ArgumentError, "JOINS are not supported on UPDATE statements by SQLite", fn ->
query = Schema |> join(:inner, [p], q in Schema2, p.x == q.z)
|> update([_], set: [x: 0]) |> normalize(:update_all)
update_all(query)
end
assert_raise ArgumentError, "JOINS are not supported on UPDATE statements by SQLite", fn ->
query = from(e in Schema, where: e.x == 123, update: [set: [x: 0]],
join: q in Schema2, on: e.x == q.z) |> normalize(:update_all)
update_all(query)
end
end
test "update all with returning" do
query = from(m in Schema, update: [set: [x: 0]]) |> select([m], m) |> normalize(:update_all)
assert update_all(query) ==
~s{UPDATE "schema" SET "x" = 0 ;--RETURNING ON UPDATE "schema","id","x","y","z"}
# diff SQLite syntax
end
test "update all array ops" do
assert_raise ArgumentError, "Array operations are not supported by SQLite", fn ->
query = from(m in SchemaWithArray, update: [push: [w: 0]]) |> normalize(:update_all)
update_all(query)
end
assert_raise ArgumentError, "Array operations are not supported by SQLite", fn ->
query = from(m in SchemaWithArray, update: [pull: [w: 0]]) |> normalize(:update_all)
update_all(query)
end
end
test "update all with prefix" do # new don't know what to expect
query = from(m in Schema, update: [set: [x: 0]]) |> normalize(:update_all)
assert update_all(%{query | prefix: "prefix"}) ==
~s{UPDATE "prefix"."schema" SET "x" = 0}
end
test "delete all" do
query = Schema |> Queryable.to_query |> normalize
assert delete_all(query) == ~s{DELETE FROM "schema"}
query = from(e in Schema, where: e.x == 123) |> normalize
assert delete_all(query) ==
~s{DELETE FROM "schema" WHERE ("schema"."x" = 123)}
assert_raise ArgumentError, "JOINS are not supported on DELETE statements by SQLite", fn ->
query = Schema |> join(:inner, [p], q in Schema2, p.x == q.z) |> normalize
delete_all(query)
end
assert_raise ArgumentError, "JOINS are not supported on DELETE statements by SQLite", fn ->
query = from(e in Schema, where: e.x == 123, join: q in Schema2, on: e.x == q.z) |> normalize
delete_all(query)
end
assert_raise ArgumentError, "JOINS are not supported on DELETE statements by SQLite", fn ->
query = from(e in Schema, where: e.x == 123, join: assoc(e, :comments), join: assoc(e, :permalink)) |> normalize
delete_all(query)
end
end
test "delete all with returning" do
query = Schema |> Queryable.to_query |> select([m], m) |> normalize
assert delete_all(query) == ~s{DELETE FROM "schema" ;--RETURNING ON DELETE "schema","id","x","y","z"}
end
test "delete all with prefix" do
query = Schema |> Queryable.to_query |> normalize
assert delete_all(%{query | prefix: "prefix"}) == ~s{DELETE FROM "prefix"."schema"}
end
## Joins
test "join" do
query = Schema |> join(:inner, [p], q in Schema2, p.x == q.z) |> select([], true) |> normalize
assert all(query) ==
~s{SELECT 1 FROM "schema" AS s0 INNER JOIN "schema2" AS s1 ON s0."x" = s1."z"}
query = Schema |> join(:inner, [p], q in Schema2, p.x == q.z)
|> join(:inner, [], Schema, true) |> select([], true) |> normalize
assert all(query) ==
~s{SELECT 1 FROM "schema" AS s0 INNER JOIN "schema2" AS s1 ON s0."x" = s1."z" } <>
~s{INNER JOIN "schema" AS s2 ON 1}
end
test "join with nothing bound" do
query = Schema |> join(:inner, [], q in Schema2, q.z == q.z) |> select([], true) |> normalize
assert all(query) ==
~s{SELECT 1 FROM "schema" AS s0 INNER JOIN "schema2" AS s1 ON s1."z" = s1."z"}
end
test "join without schema" do
query = "posts" |> join(:inner, [p], q in "comments", p.x == q.z) |> select([], true) |> normalize
assert all(query) ==
~s{SELECT 1 FROM "posts" AS p0 INNER JOIN "comments" AS c1 ON p0."x" = c1."z"}
end
test "join with subquery" do
posts = subquery("posts" |> where(title: ^"hello") |> select([r], %{x: r.x, y: r.y}))
query = "comments" |> join(:inner, [c], p in subquery(posts), true) |> select([_, p], p.x) |> normalize
assert all(query) ==
~s{SELECT s1."x" FROM "comments" AS c0 } <>
~s{INNER JOIN (SELECT p0."x" AS "x", p0."y" AS "y" FROM "posts" AS p0 WHERE (p0."title" = ?1)) AS s1 ON 1}
posts = subquery("posts" |> where(title: ^"hello") |> select([r], %{x: r.x, z: r.y}))
query = "comments" |> join(:inner, [c], p in subquery(posts), true) |> select([_, p], p) |> normalize
assert all(query) ==
~s{SELECT s1."x", s1."z" FROM "comments" AS c0 } <>
~s{INNER JOIN (SELECT p0."x" AS "x", p0."y" AS "z" FROM "posts" AS p0 WHERE (p0."title" = ?1)) AS s1 ON 1}
end
test "join with prefix" do
query = Schema |> join(:inner, [p], q in Schema2, p.x == q.z) |> select([], true) |> normalize
assert all(%{query | prefix: "prefix"}) ==
~s{SELECT 1 FROM "prefix"."schema" AS s0 INNER JOIN "prefix"."schema2" AS s1 ON s0."x" = s1."z"}
end
test "join with fragment" do
query = Schema
|> join(:inner, [p], q in fragment("SELECT * FROM schema2 AS s2 WHERE s2.id = ? AND s2.field = ?", p.x, ^10))
|> select([p], {p.id, ^0})
|> where([p], p.id > 0 and p.id < ^100)
|> normalize
assert all(query) ==
~s{SELECT s0."id", ?1 FROM "schema" AS s0 INNER JOIN } <>
~s{(SELECT * FROM schema2 AS s2 WHERE s2.id = s0."x" AND s2.field = ?2) AS f1 ON 1 } <>
~s{WHERE ((s0."id" > 0) AND (s0."id" < ?3))}
end
test "join with fragment and on defined" do
query = Schema
|> join(:inner, [p], q in fragment("SELECT * FROM schema2"), q.id == p.id)
|> select([p], {p.id, ^0})
|> normalize
assert all(query) ==
~s{SELECT s0."id", ?1 FROM "schema" AS s0 INNER JOIN } <>
~s{(SELECT * FROM schema2) AS f1 ON f1."id" = s0."id"}
end
test "join with query interpolation" do
inner = Ecto.Queryable.to_query(Schema2)
query = from(p in Schema, left_join: c in ^inner, select: {p.id, c.id}) |> normalize()
assert all(query) ==
"SELECT s0.\"id\", s1.\"id\" FROM \"schema\" AS s0 LEFT JOIN \"schema2\" AS s1 ON 1"
end
test "lateral join with fragment" do
assert_raise ArgumentError, "join `:inner_lateral` not supported by SQLite", fn ->
query = Schema
|> join(:inner_lateral, [p], q in fragment("SELECT * FROM schema2 AS s2 WHERE s2.id = ? AND s2.field = ?", p.x, ^10))
|> select([p, q], {p.id, q.z})
|> where([p], p.id > 0 and p.id < ^100)
|> normalize
all(query)
end
end
test "cross join" do
query = from(p in Schema, cross_join: c in Schema2, select: {p.id, c.id}) |> normalize()
assert all(query) ==
"SELECT s0.\"id\", s1.\"id\" FROM \"schema\" AS s0 CROSS JOIN \"schema2\" AS s1"
end
test "join produces correct bindings" do
query = from(p in Schema, join: c in Schema2, on: true)
query = from(p in query, join: c in Schema2, on: true, select: {p.id, c.id})
query = normalize(query)
assert all(query) ==
"SELECT s0.\"id\", s2.\"id\" FROM \"schema\" AS s0 INNER JOIN \"schema2\" AS s1 ON 1 INNER JOIN \"schema2\" AS s2 ON 1"
end
describe "query interpolation parameters" do
test "self join on subquery" do
subquery = select(Schema, [r], %{x: r.x, y: r.y})
query = subquery |> join(:inner, [c], p in subquery(subquery), true) |> normalize
assert all(query) ==
~s{SELECT s0."x", s0."y" FROM "schema" AS s0 INNER JOIN } <>
~s{(SELECT s0."x" AS "x", s0."y" AS "y" FROM "schema" AS s0) } <>
~s{AS s1 ON 1}
end
test "self join on subquery with fragment" do
subquery = select(Schema, [r], %{string: fragment("downcase(?)", ^"string")})
query = subquery |> join(:inner, [c], p in subquery(subquery), true) |> normalize
assert all(query) ==
~s{SELECT downcase(?1) FROM "schema" AS s0 INNER JOIN } <>
~s{(SELECT downcase(?2) AS "string" FROM "schema" AS s0) } <>
~s{AS s1 ON 1}
end
test "join on subquery with simple select" do
subquery = select(Schema, [r], %{x: ^999, w: ^888})
query = Schema
|> select([r], %{y: ^666})
|> join(:inner, [c], p in subquery(subquery), true)
|> where([a, b], a.x == ^111)
|> normalize
assert all(query) ==
~s{SELECT ?1 FROM "schema" AS s0 INNER JOIN } <>
~s{(SELECT ?2 AS "x", ?3 AS "w" FROM "schema" AS s0) AS s1 ON 1 } <>
~s{WHERE (s0."x" = ?4)}
end
end
## Associations
test "association join belongs_to" do
query = Schema2 |> join(:inner, [c], p in assoc(c, :post)) |> select([], true) |> normalize
assert all(query) ==
"SELECT 1 FROM \"schema2\" AS s0 INNER JOIN \"schema\" AS s1 ON s1.\"x\" = s0.\"z\""
end
test "association join has_many" do
query = Schema |> join(:inner, [p], c in assoc(p, :comments)) |> select([], true) |> normalize
assert all(query) ==
"SELECT 1 FROM \"schema\" AS s0 INNER JOIN \"schema2\" AS s1 ON s1.\"z\" = s0.\"x\""
end
test "association join has_one" do
query = Schema |> join(:inner, [p], pp in assoc(p, :permalink)) |> select([], true) |> normalize
assert all(query) ==
"SELECT 1 FROM \"schema\" AS s0 INNER JOIN \"schema3\" AS s1 ON s1.\"id\" = s0.\"y\""
end
# Schema based
test "insert" do
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {:raise, [], []}, [:id])
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES (?1,?2) ;--RETURNING ON INSERT "schema","id"}
assert_raise ArgumentError, "Cell-wise default values are not supported on INSERT statements by SQLite", fn ->
insert(nil, "schema", [:x, :y], [[:x, :y], [nil, :z]], {:raise, [], []}, [:id])
end
query = insert(nil, "schema", [], [[]], {:raise, [], []}, [:id])
assert query == ~s{INSERT INTO "schema" DEFAULT VALUES ;--RETURNING ON INSERT "schema","id"}
query = insert(nil, "schema", [], [[]], {:raise, [], []}, [])
assert query == ~s{INSERT INTO "schema" DEFAULT VALUES}
query = insert("prefix", "schema", [], [[]], {:raise, [], []}, [:id])
assert query == ~s{INSERT INTO "prefix"."schema" DEFAULT VALUES ;--RETURNING ON INSERT "prefix"."schema","id"}
query = insert("prefix", "schema", [], [[]], {:raise, [], []}, [])
assert query == ~s{INSERT INTO "prefix"."schema" DEFAULT VALUES}
end
test "insert with on conflict" do
# These tests are adapted from the Postgres Adaptor
# For :nothing
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {:nothing, [], []}, [])
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES (?1,?2) ON CONFLICT DO NOTHING}
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {:nothing, [], [:x, :y]}, [])
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES (?1,?2) ON CONFLICT ("x","y") DO NOTHING}
# For :update
update = from("schema", update: [set: [z: "foo"]]) |> normalize(:update_all)
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {update, [], [:x, :y]}, [:z])
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES (?1,?2) ON CONFLICT ("x","y") DO UPDATE SET "z" = 'foo' ;--RETURNING ON INSERT "schema","z"}
update = from("schema", update: [set: [z: ^"foo"]], where: [w: true]) |> normalize(:update_all, 2)
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {update, [], [:x, :y]}, [:z])
assert query = ~s{INSERT INTO "schema" ("x","y") VALUES (?1,?2) ON CONFLICT ("x","y") DO UPDATE SET "z" = ?3 WHERE ("schema"."w" = 1) ;--RETURNING ON INSERT "schema","z"}
update = normalize(from("schema", update: [set: [z: "foo"]]), :update_all)
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {update, [], [:x, :y]}, [:z])
assert query = ~s{INSERT INTO "schema" ("x","y") VALUES (?1,?2) ON CONFLICT ("x","y") DO UPDATE SET "z" = 'foo' ;--RETURNING ON INSERT "schema","z"}
update = normalize(from("schema", update: [set: [z: ^"foo"]], where: [w: true]), :update_all, 2)
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {update, [], [:x, :y]}, [:z])
assert query = ~s{INSERT INTO "schema" ("x","y") VALUES (?1,?2) ON CONFLICT ("x","y") DO UPDATE SET "z" = ?3 WHERE ("schema"."w" = 1) ;--RETURNING ON INSERT "schema","z"}
# For :replace_all
assert_raise ArgumentError, "Upsert in SQLite requires :conflict_target", fn ->
conflict_target = []
insert(nil, "schema", [:x, :y], [[:x, :y]], {:replace_all, [], conflict_target}, [])
end
assert_raise ArgumentError, "Upsert in SQLite does not support ON CONSTRAINT", fn ->
insert(nil, "schema", [:x, :y], [[:x, :y]], {:replace_all, [], {:constraint, :foo}}, [])
end
query = insert(nil, "schema", [:x, :y], [[:x, :y]], {:replace_all, [], [:id]}, [])
assert query == ~s{INSERT INTO "schema" ("x","y") VALUES (?1,?2) ON CONFLICT ("id") DO UPDATE SET "x" = EXCLUDED."x","y" = EXCLUDED."y"}
end
test "update" do
query = update(nil, "schema", [:x, :y], [:id], [])
assert query == ~s{UPDATE "schema" SET "x" = ?1, "y" = ?2 WHERE "id" = ?3}
query = update(nil, "schema", [:x, :y], [:id], [:z])
assert query == ~s{UPDATE "schema" SET "x" = ?1, "y" = ?2 WHERE "id" = ?3 ;--RETURNING ON UPDATE "schema","z"}
query = update("prefix", "schema", [:x, :y], [:id], [:x, :z])
assert query == ~s{UPDATE "prefix"."schema" SET "x" = ?1, "y" = ?2 WHERE "id" = ?3 ;--RETURNING ON UPDATE "prefix"."schema","x","z"}
query = update("prefix", "schema", [:x, :y], [:id], [])
assert query == ~s{UPDATE "prefix"."schema" SET "x" = ?1, "y" = ?2 WHERE "id" = ?3}
end
test "delete" do
query = delete(nil, "schema", [:x, :y], [])
assert query == ~s{DELETE FROM "schema" WHERE "x" = ?1 AND "y" = ?2}
query = delete(nil, "schema", [:x, :y], [:z])
assert query == ~s{DELETE FROM "schema" WHERE "x" = ?1 AND "y" = ?2 ;--RETURNING ON DELETE "schema","z"}
query = delete("prefix", "schema", [:x, :y], [:z])
assert query == ~s{DELETE FROM "prefix"."schema" WHERE "x" = ?1 AND "y" = ?2 ;--RETURNING ON DELETE "prefix"."schema","z"}
query = delete(nil, "schema", [:x, :y], [])
assert query == ~s{DELETE FROM "schema" WHERE "x" = ?1 AND "y" = ?2}
query = delete("prefix", "schema", [:x, :y], [])
assert query == ~s{DELETE FROM "prefix"."schema" WHERE "x" = ?1 AND "y" = ?2}
end
# DDL
alias Ecto.Migration.Reference
import Ecto.Migration, only: [table: 1, table: 2, index: 2, index: 3,
constraint: 2, constraint: 3]
test "executing a string during migration" do
assert execute_ddl("example") == ["example"]
end
test "keyword list during migration" do
assert_raise ArgumentError, "SQLite adapter does not support keyword lists in execute", fn ->
execute_ddl(testing: false)
end
end
test "create table" do
create = {:create, table(:posts),
[{:add, :name, :string, [default: "Untitled", size: 20, null: false]},
{:add, :price, :numeric, [precision: 8, scale: 2, default: {:fragment, "expr"}]},
{:add, :on_hand, :integer, [default: 0, null: true]},
{:add, :is_active, :boolean, [default: true]}]}
assert execute_ddl(create) == ["""
CREATE TABLE "posts" ("name" TEXT DEFAULT 'Untitled' NOT NULL,
"price" NUMERIC DEFAULT expr,
"on_hand" INTEGER DEFAULT 0,
"is_active" BOOLEAN DEFAULT 1)
""" |> remove_newlines]
end
test "create table invalid default" do
create = {:create, table(:posts),
[{:add, :name, :string, [default: :atoms_not_allowed]}]}
assert_raise ArgumentError, ~r"unknown default `:atoms_not_allowed` for type `:string`", fn ->
execute_ddl(create)
end
end
test "create table array type" do
create = {:create, table(:posts),
[{:add, :name, {:array, :numeric}, []}]}
assert execute_ddl(create) == ["""
CREATE TABLE "posts" ("name" JSON)
""" |> remove_newlines()]
end
test "create table illegal options" do
create = {:create, table(:posts, options: [allowed: :not]),
[{:add, :name, :string}]}
assert_raise ArgumentError, ~r"SQLite adapter does not support keyword lists in :options", fn ->
execute_ddl(create)
end
end
test "create table if not exists" do
create = {:create_if_not_exists, table(:posts),
[{:add, :id, :serial, [primary_key: true]},
{:add, :title, :string, []},
{:add, :price, :decimal, [precision: 10, scale: 2]},
{:add, :created_at, :datetime, []}]}
query = execute_ddl(create)
assert query == ["""
CREATE TABLE IF NOT EXISTS "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT,
"title" TEXT,
"price" DECIMAL(10,2),
"created_at" DATETIME)
""" |> remove_newlines]
end
test "create table with prefix" do
create = {:create, table(:posts, prefix: :foo),
[{:add, :category_0, %Reference{table: :categories}, []}]}
assert execute_ddl(create) == ["""
CREATE TABLE "foo"."posts"
("category_0" INTEGER CONSTRAINT "posts_category_0_fkey" REFERENCES "foo"."categories"("id"))
""" |> remove_newlines]
end
test "create table with comment on columns and table" do
create = {:create, table(:posts, comment: "comment"),
[
{:add, :category_0, %Reference{table: :categories}, [comment: "column comment"]},
{:add, :created_at, :timestamp, []},
{:add, :updated_at, :timestamp, [comment: "column comment 2"]}
]}
assert execute_ddl(create) == [remove_newlines("""
CREATE TABLE "posts"
("category_0" INTEGER CONSTRAINT "posts_category_0_fkey" REFERENCES "categories"("id"), "created_at" TIMESTAMP, "updated_at" TIMESTAMP)
""")]
# NOTE: Comments are not supported by SQLite. DDL query generator will ignore them.
end
test "create table with comment on table" do
create = {:create, table(:posts, comment: "table comment"),
[{:add, :category_0, %Reference{table: :categories}, []}]}
assert execute_ddl(create) == [remove_newlines("""
CREATE TABLE "posts"
("category_0" INTEGER CONSTRAINT "posts_category_0_fkey" REFERENCES "categories"("id"))
""")]
# NOTE: Comments are not supported by SQLite. DDL query generator will ignore them.
end
test "create table with comment on columns" do
create = {:create, table(:posts),
[
{:add, :category_0, %Reference{table: :categories}, [comment: "column comment"]},
{:add, :created_at, :timestamp, []},
{:add, :updated_at, :timestamp, [comment: "column comment 2"]}
]}
assert execute_ddl(create) == [remove_newlines("""
CREATE TABLE "posts"
("category_0" INTEGER CONSTRAINT "posts_category_0_fkey" REFERENCES "categories"("id"), "created_at" TIMESTAMP, "updated_at" TIMESTAMP)
""")]
# NOTE: Comments are not supported by SQLite. DDL query generator will ignore them.
end
test "create table with references" do
create = {:create, table(:posts),
[{:add, :id, :serial, [primary_key: true]},
{:add, :category_0, %Reference{table: :categories}, []},
{:add, :category_1, %Reference{table: :categories, name: :foo_bar}, []},
{:add, :category_2, %Reference{table: :categories, on_delete: :nothing}, []},
{:add, :category_3, %Reference{table: :categories, on_delete: :delete_all}, [null: false]},
{:add, :category_4, %Reference{table: :categories, on_delete: :nilify_all}, []},
{:add, :category_5, %Reference{table: :categories, on_update: :nothing}, []},
{:add, :category_6, %Reference{table: :categories, on_update: :update_all}, [null: false]},
{:add, :category_7, %Reference{table: :categories, on_update: :nilify_all}, []},
{:add, :category_8, %Reference{table: :categories, on_delete: :nilify_all, on_update: :update_all}, [null: false]}]}
assert execute_ddl(create) == ["""
CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT,
"category_0" INTEGER CONSTRAINT "posts_category_0_fkey" REFERENCES "categories"("id"),
"category_1" INTEGER CONSTRAINT "foo_bar" REFERENCES "categories"("id"),
"category_2" INTEGER CONSTRAINT "posts_category_2_fkey" REFERENCES "categories"("id"),
"category_3" INTEGER NOT NULL CONSTRAINT "posts_category_3_fkey" REFERENCES "categories"("id") ON DELETE CASCADE,
"category_4" INTEGER CONSTRAINT "posts_category_4_fkey" REFERENCES "categories"("id") ON DELETE SET NULL,
"category_5" INTEGER CONSTRAINT "posts_category_5_fkey" REFERENCES "categories"("id"),
"category_6" INTEGER NOT NULL CONSTRAINT "posts_category_6_fkey" REFERENCES "categories"("id") ON UPDATE CASCADE,
"category_7" INTEGER CONSTRAINT "posts_category_7_fkey" REFERENCES "categories"("id") ON UPDATE SET NULL,
"category_8" INTEGER NOT NULL CONSTRAINT "posts_category_8_fkey" REFERENCES "categories"("id") ON DELETE SET NULL ON UPDATE CASCADE)
""" |> remove_newlines]
end
test "create table with references including prefixes" do
create = {:create, table(:posts, prefix: :foo),
[{:add, :id, :serial, [primary_key: true]},
{:add, :category_0, %Reference{table: :categories}, []},
{:add, :category_1, %Reference{table: :categories, name: :foo_bar}, []},
{:add, :category_2, %Reference{table: :categories, on_delete: :nothing}, []},
{:add, :category_3, %Reference{table: :categories, on_delete: :delete_all}, [null: false]},
{:add, :category_4, %Reference{table: :categories, on_delete: :nilify_all}, []}]}
assert execute_ddl(create) == ["""
CREATE TABLE "foo"."posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT,
"category_0" INTEGER CONSTRAINT "posts_category_0_fkey" REFERENCES "foo"."categories"("id"),
"category_1" INTEGER CONSTRAINT "foo_bar" REFERENCES "foo"."categories"("id"),
"category_2" INTEGER CONSTRAINT "posts_category_2_fkey" REFERENCES "foo"."categories"("id"),
"category_3" INTEGER NOT NULL CONSTRAINT "posts_category_3_fkey" REFERENCES "foo"."categories"("id") ON DELETE CASCADE,
"category_4" INTEGER CONSTRAINT "posts_category_4_fkey" REFERENCES "foo"."categories"("id") ON DELETE SET NULL)
""" |> remove_newlines]
end
test "create table with options" do
create = {:create, table(:posts, options: "WITHOUT ROWID"),
[{:add, :id, :serial, [primary_key: true]},
{:add, :created_at, :datetime, []}]}
assert execute_ddl(create) ==
[~s|CREATE TABLE "posts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT, "created_at" DATETIME) WITHOUT ROWID|]
end
test "create table with composite key" do