1use crate::binder;
2use crate::offsets::token_from_offset;
3use crate::resolve;
4use rowan::{TextRange, TextSize};
5use smallvec::{SmallVec, smallvec};
6use squawk_syntax::{
7 SyntaxKind,
8 ast::{self, AstNode},
9};
10
11pub fn goto_definition(file: ast::SourceFile, offset: TextSize) -> SmallVec<[TextRange; 1]> {
12 let Some(token) = token_from_offset(&file, offset) else {
13 return smallvec![];
14 };
15 let Some(parent) = token.parent() else {
16 return smallvec![];
17 };
18
19 if (token.kind() == SyntaxKind::WHEN_KW && parent.kind() == SyntaxKind::WHEN_CLAUSE)
21 || (token.kind() == SyntaxKind::ELSE_KW && parent.kind() == SyntaxKind::ELSE_CLAUSE)
22 || (token.kind() == SyntaxKind::END_KW && parent.kind() == SyntaxKind::CASE_EXPR)
23 {
24 for parent in token.parent_ancestors() {
25 if let Some(case_expr) = ast::CaseExpr::cast(parent)
26 && let Some(case_token) = case_expr.case_token()
27 {
28 return smallvec![case_token.text_range()];
29 }
30 }
31 }
32
33 if ast::Commit::can_cast(parent.kind()) {
35 if let Some(begin_range) = find_preceding_begin(&file, token.text_range().start()) {
36 return smallvec![begin_range];
37 }
38 }
39
40 if ast::Rollback::can_cast(parent.kind()) {
42 if let Some(begin_range) = find_preceding_begin(&file, token.text_range().start()) {
43 return smallvec![begin_range];
44 }
45 }
46
47 if ast::Begin::can_cast(parent.kind()) {
49 if let Some(end_range) = find_following_commit_or_rollback(&file, token.text_range().end())
50 {
51 return smallvec![end_range];
52 }
53 }
54
55 if let Some(name) = ast::Name::cast(parent.clone()) {
56 return smallvec![name.syntax().text_range()];
57 }
58
59 if let Some(name_ref) = ast::NameRef::cast(parent.clone()) {
60 let binder_output = binder::bind(&file);
61 let root = file.syntax();
62 if let Some(ptrs) = resolve::resolve_name_ref(&binder_output, root, &name_ref) {
63 return ptrs
64 .iter()
65 .map(|ptr| ptr.to_node(file.syntax()).text_range())
66 .collect();
67 }
68 }
69
70 smallvec![]
71}
72
73fn find_preceding_begin(file: &ast::SourceFile, before: TextSize) -> Option<TextRange> {
74 let mut last_begin: Option<TextRange> = None;
75 for stmt in file.stmts() {
76 if let ast::Stmt::Begin(begin) = stmt {
77 let range = begin.syntax().text_range();
78 if range.end() <= before {
79 last_begin = Some(range);
80 }
81 }
82 }
83 last_begin
84}
85
86fn find_following_commit_or_rollback(file: &ast::SourceFile, after: TextSize) -> Option<TextRange> {
87 for stmt in file.stmts() {
88 let range = match &stmt {
89 ast::Stmt::Commit(commit) => commit.syntax().text_range(),
90 ast::Stmt::Rollback(rollback) => rollback.syntax().text_range(),
91 _ => continue,
92 };
93 if range.start() >= after {
94 return Some(range);
95 }
96 }
97 None
98}
99
100#[cfg(test)]
101mod test {
102 use crate::goto_definition::goto_definition;
103 use crate::test_utils::fixture;
104 use annotate_snippets::{AnnotationKind, Level, Renderer, Snippet, renderer::DecorStyle};
105 use insta::assert_snapshot;
106 use log::info;
107 use squawk_syntax::ast;
108
109 #[track_caller]
110 fn goto(sql: &str) -> String {
111 goto_(sql).expect("should always find a definition")
112 }
113
114 #[track_caller]
115 fn goto_(sql: &str) -> Option<String> {
116 info!("starting");
117 let (mut offset, sql) = fixture(sql);
118 offset = offset.checked_sub(1.into()).unwrap_or_default();
121 let parse = ast::SourceFile::parse(&sql);
122 assert_eq!(parse.errors(), vec![]);
123 let file: ast::SourceFile = parse.tree();
124 let results = goto_definition(file, offset);
125 if !results.is_empty() {
126 let offset: usize = offset.into();
127 let mut snippet = Snippet::source(&sql).fold(true);
128
129 for (i, result) in results.iter().enumerate() {
130 snippet = snippet.annotation(
131 AnnotationKind::Context
132 .span((*result).into())
133 .label(format!("{}. destination", i + 2)),
134 );
135 }
136
137 snippet = snippet.annotation(
138 AnnotationKind::Context
139 .span(offset..offset + 1)
140 .label("1. source"),
141 );
142
143 let group = Level::INFO.primary_title("definition").element(snippet);
144 let renderer = Renderer::plain().decor_style(DecorStyle::Unicode);
145 return Some(
146 renderer
147 .render(&[group])
148 .to_string()
149 .replace("info: definition", ""),
151 );
152 }
153 None
154 }
155
156 fn goto_not_found(sql: &str) {
157 assert!(goto_(sql).is_none(), "Should not find a definition");
158 }
159
160 #[test]
161 fn goto_case_when() {
162 assert_snapshot!(goto("
163select case when$0 x > 1 then 1 else 2 end;
164"), @r"
165 ╭▸
166 2 │ select case when x > 1 then 1 else 2 end;
167 │ ┬─── ─ 1. source
168 │ │
169 ╰╴ 2. destination
170 ");
171 }
172
173 #[test]
174 fn goto_case_else() {
175 assert_snapshot!(goto("
176select case when x > 1 then 1 else$0 2 end;
177"), @r"
178 ╭▸
179 2 │ select case when x > 1 then 1 else 2 end;
180 ╰╴ ──── 2. destination ─ 1. source
181 ");
182 }
183
184 #[test]
185 fn goto_case_end() {
186 assert_snapshot!(goto("
187select case when x > 1 then 1 else 2 end$0;
188"), @r"
189 ╭▸
190 2 │ select case when x > 1 then 1 else 2 end;
191 ╰╴ ──── 2. destination ─ 1. source
192 ");
193 }
194
195 #[test]
196 fn goto_case_end_trailing_semi() {
197 assert_snapshot!(goto("
198select case when x > 1 then 1 else 2 end;$0
199"), @r"
200 ╭▸
201 2 │ select case when x > 1 then 1 else 2 end;
202 ╰╴ ──── 2. destination ─ 1. source
203 ");
204 }
205
206 #[test]
207 fn goto_case_then_not_found() {
208 goto_not_found(
209 "
210select case when x > 1 then$0 1 else 2 end;
211",
212 )
213 }
214
215 #[test]
216 fn rollback_to_begin() {
217 assert_snapshot!(goto(
218 "
219begin;
220select 1;
221rollback$0;
222",
223 ), @r"
224 ╭▸
225 2 │ begin;
226 │ ───── 2. destination
227 3 │ select 1;
228 4 │ rollback;
229 ╰╴ ─ 1. source
230 ");
231 }
232
233 #[test]
234 fn goto_drop_table() {
235 assert_snapshot!(goto("
236create table t();
237drop table t$0;
238"), @r"
239 ╭▸
240 2 │ create table t();
241 │ ─ 2. destination
242 3 │ drop table t;
243 ╰╴ ─ 1. source
244 ");
245 }
246
247 #[test]
248 fn goto_definition_prefers_previous_token() {
249 assert_snapshot!(goto("
250create table t(a int);
251select t.$0a from t;
252"), @r"
253 ╭▸
254 2 │ create table t(a int);
255 │ ─ 2. destination
256 3 │ select t.a from t;
257 ╰╴ ─ 1. source
258 ");
259
260 assert_snapshot!(goto("
261create type ty as (a int, b int);
262with t as (select '(1,2)'::ty c)
263select (c)$0.a from t;
264"), @r"
265 ╭▸
266 3 │ with t as (select '(1,2)'::ty c)
267 │ ─ 2. destination
268 4 │ select (c).a from t;
269 ╰╴ ─ 1. source
270 ");
271 assert_snapshot!(goto("
272create function f() returns int as 'select 1' language sql;
273select f($0);
274"), @r"
275 ╭▸
276 2 │ create function f() returns int as 'select 1' language sql;
277 │ ─ 2. destination
278 3 │ select f();
279 ╰╴ ─ 1. source
280 ");
281
282 assert_snapshot!(goto("
283with t as (select array[1,2,3]::int[] c)
284select c[$01] from t;
285"), @r"
286 ╭▸
287 2 │ with t as (select array[1,2,3]::int[] c)
288 │ ─ 2. destination
289 3 │ select c[1] from t;
290 ╰╴ ─ 1. source
291 ");
292
293 assert_snapshot!(goto("
294with t as (select array[1,2,3]::int[] c, 1 b)
295select c[b]$0 from t;
296"), @r"
297 ╭▸
298 2 │ with t as (select array[1,2,3]::int[] c, 1 b)
299 │ ─ 2. destination
300 3 │ select c[b] from t;
301 ╰╴ ─ 1. source
302 ");
303 }
304
305 #[test]
306 fn goto_with_table_star() {
307 assert_snapshot!(goto("
308with t as (select 1 a)
309select t$0.* from t;
310"), @r"
311 ╭▸
312 2 │ with t as (select 1 a)
313 │ ─ 2. destination
314 3 │ select t.* from t;
315 ╰╴ ─ 1. source
316 ");
317 }
318
319 #[test]
320 fn goto_drop_sequence() {
321 assert_snapshot!(goto("
322create sequence s;
323drop sequence s$0;
324"), @r"
325 ╭▸
326 2 │ create sequence s;
327 │ ─ 2. destination
328 3 │ drop sequence s;
329 ╰╴ ─ 1. source
330 ");
331 }
332
333 #[test]
334 fn goto_create_sequence_owned_by() {
335 assert_snapshot!(goto("
336create table t(c serial);
337create sequence s
338 owned by t.c$0;
339"), @r"
340 ╭▸
341 2 │ create table t(c serial);
342 │ ─ 2. destination
343 3 │ create sequence s
344 4 │ owned by t.c;
345 ╰╴ ─ 1. source
346 ");
347 }
348
349 #[test]
350 fn goto_drop_tablespace() {
351 assert_snapshot!(goto("
352create tablespace ts location '/tmp/ts';
353drop tablespace ts$0;
354"), @r"
355 ╭▸
356 2 │ create tablespace ts location '/tmp/ts';
357 │ ── 2. destination
358 3 │ drop tablespace ts;
359 ╰╴ ─ 1. source
360 ");
361 }
362
363 #[test]
364 fn goto_create_table_tablespace() {
365 assert_snapshot!(goto("
366create tablespace bar location '/tmp/ts';
367create table t (a int) tablespace b$0ar;
368"), @r"
369 ╭▸
370 2 │ create tablespace bar location '/tmp/ts';
371 │ ─── 2. destination
372 3 │ create table t (a int) tablespace bar;
373 ╰╴ ─ 1. source
374 ");
375 }
376
377 #[test]
378 fn goto_drop_database() {
379 assert_snapshot!(goto("
380create database mydb;
381drop database my$0db;
382"), @r"
383 ╭▸
384 2 │ create database mydb;
385 │ ──── 2. destination
386 3 │ drop database mydb;
387 ╰╴ ─ 1. source
388 ");
389 }
390
391 #[test]
392 fn goto_drop_database_defined_after() {
393 assert_snapshot!(goto("
394drop database my$0db;
395create database mydb;
396"), @r"
397 ╭▸
398 2 │ drop database mydb;
399 │ ─ 1. source
400 3 │ create database mydb;
401 ╰╴ ──── 2. destination
402 ");
403 }
404
405 #[test]
406 fn goto_database_definition_returns_self() {
407 assert_snapshot!(goto("
408create database my$0db;
409"), @r"
410 ╭▸
411 2 │ create database mydb;
412 │ ┬┬──
413 │ ││
414 │ │1. source
415 ╰╴ 2. destination
416 ");
417 }
418
419 #[test]
420 fn goto_drop_server() {
421 assert_snapshot!(goto("
422create server myserver foreign data wrapper fdw;
423drop server my$0server;
424"), @r"
425 ╭▸
426 2 │ create server myserver foreign data wrapper fdw;
427 │ ──────── 2. destination
428 3 │ drop server myserver;
429 ╰╴ ─ 1. source
430 ");
431 }
432
433 #[test]
434 fn goto_drop_server_defined_after() {
435 assert_snapshot!(goto("
436drop server my$0server;
437create server myserver foreign data wrapper fdw;
438"), @r"
439 ╭▸
440 2 │ drop server myserver;
441 │ ─ 1. source
442 3 │ create server myserver foreign data wrapper fdw;
443 ╰╴ ──────── 2. destination
444 ");
445 }
446
447 #[test]
448 fn goto_alter_server() {
449 assert_snapshot!(goto("
450create server myserver foreign data wrapper fdw;
451alter server my$0server options (add foo 'bar');
452"), @r"
453 ╭▸
454 2 │ create server myserver foreign data wrapper fdw;
455 │ ──────── 2. destination
456 3 │ alter server myserver options (add foo 'bar');
457 ╰╴ ─ 1. source
458 ");
459 }
460
461 #[test]
462 fn goto_server_definition_returns_self() {
463 assert_snapshot!(goto("
464create server my$0server foreign data wrapper fdw;
465"), @r"
466 ╭▸
467 2 │ create server myserver foreign data wrapper fdw;
468 │ ┬┬──────
469 │ ││
470 │ │1. source
471 ╰╴ 2. destination
472 ");
473 }
474
475 #[test]
476 fn goto_drop_sequence_with_schema() {
477 assert_snapshot!(goto("
478create sequence foo.s;
479drop sequence foo.s$0;
480"), @r"
481 ╭▸
482 2 │ create sequence foo.s;
483 │ ─ 2. destination
484 3 │ drop sequence foo.s;
485 ╰╴ ─ 1. source
486 ");
487 }
488
489 #[test]
490 fn goto_drop_table_with_schema() {
491 assert_snapshot!(goto("
492create table public.t();
493drop table t$0;
494"), @r"
495 ╭▸
496 2 │ create table public.t();
497 │ ─ 2. destination
498 3 │ drop table t;
499 ╰╴ ─ 1. source
500 ");
501
502 assert_snapshot!(goto("
503create table foo.t();
504drop table foo.t$0;
505"), @r"
506 ╭▸
507 2 │ create table foo.t();
508 │ ─ 2. destination
509 3 │ drop table foo.t;
510 ╰╴ ─ 1. source
511 ");
512
513 goto_not_found(
514 "
515-- defaults to public schema
516create table t();
517drop table foo.t$0;
518",
519 );
520 }
521
522 #[test]
523 fn goto_drop_temp_table() {
524 assert_snapshot!(goto("
525create temp table t();
526drop table t$0;
527"), @r"
528 ╭▸
529 2 │ create temp table t();
530 │ ─ 2. destination
531 3 │ drop table t;
532 ╰╴ ─ 1. source
533 ");
534 }
535
536 #[test]
537 fn goto_drop_temporary_table() {
538 assert_snapshot!(goto("
539create temporary table t();
540drop table t$0;
541"), @r"
542 ╭▸
543 2 │ create temporary table t();
544 │ ─ 2. destination
545 3 │ drop table t;
546 ╰╴ ─ 1. source
547 ");
548 }
549
550 #[test]
551 fn goto_drop_temp_table_with_pg_temp_schema() {
552 assert_snapshot!(goto("
553create temp table t();
554drop table pg_temp.t$0;
555"), @r"
556 ╭▸
557 2 │ create temp table t();
558 │ ─ 2. destination
559 3 │ drop table pg_temp.t;
560 ╰╴ ─ 1. source
561 ");
562 }
563
564 #[test]
565 fn goto_table_definition_returns_self() {
566 assert_snapshot!(goto("
567create table t$0(x bigint, y bigint);
568"), @r"
569 ╭▸
570 2 │ create table t(x bigint, y bigint);
571 │ ┬
572 │ │
573 │ 2. destination
574 ╰╴ 1. source
575 ");
576 }
577
578 #[test]
579 fn goto_foreign_table_column() {
580 assert_snapshot!(goto("
581create foreign table ft(a int)
582 server s;
583
584select a$0 from ft;
585"), @r"
586 ╭▸
587 2 │ create foreign table ft(a int)
588 │ ─ 2. destination
589 ‡
590 5 │ select a from ft;
591 ╰╴ ─ 1. source
592 ");
593 }
594
595 #[test]
596 fn goto_foreign_table_definition() {
597 assert_snapshot!(goto("
598create foreign table ft(a int)
599 server s;
600
601select a from ft$0;
602"), @r"
603 ╭▸
604 2 │ create foreign table ft(a int)
605 │ ── 2. destination
606 ‡
607 5 │ select a from ft;
608 ╰╴ ─ 1. source
609 ");
610 }
611
612 #[test]
613 fn goto_foreign_table_server_name() {
614 assert_snapshot!(goto("
615create server myserver foreign data wrapper fdw;
616create foreign table ft(a int)
617 server my$0server;
618"), @r"
619 ╭▸
620 2 │ create server myserver foreign data wrapper fdw;
621 │ ──────── 2. destination
622 3 │ create foreign table ft(a int)
623 4 │ server myserver;
624 ╰╴ ─ 1. source
625 ");
626 }
627
628 #[test]
629 fn goto_foreign_table_server_name_defined_after() {
630 assert_snapshot!(goto("
631create foreign table ft(a int)
632 server my$0server;
633create server myserver foreign data wrapper fdw;
634"), @r"
635 ╭▸
636 3 │ server myserver;
637 │ ─ 1. source
638 4 │ create server myserver foreign data wrapper fdw;
639 ╰╴ ──────── 2. destination
640 ");
641 }
642
643 #[test]
644 fn goto_user_mapping_server_name() {
645 assert_snapshot!(goto("
646create server myserver foreign data wrapper fdw;
647create user mapping for current_user server my$0server;
648"), @r"
649 ╭▸
650 2 │ create server myserver foreign data wrapper fdw;
651 │ ──────── 2. destination
652 3 │ create user mapping for current_user server myserver;
653 ╰╴ ─ 1. source
654 ");
655 }
656
657 #[test]
658 fn goto_foreign_key_references_table() {
659 assert_snapshot!(goto("
660create table foo(id int);
661create table bar(
662 id int,
663 foo_id int,
664 foreign key (foo_id) references foo$0(id)
665);
666"), @r"
667 ╭▸
668 2 │ create table foo(id int);
669 │ ─── 2. destination
670 ‡
671 6 │ foreign key (foo_id) references foo(id)
672 ╰╴ ─ 1. source
673 ");
674 }
675
676 #[test]
677 fn goto_foreign_key_on_delete_set_null_column() {
678 assert_snapshot!(goto("
679create table users (
680 user_id integer not null,
681 primary key (user_id)
682);
683
684create table posts (
685 post_id integer not null,
686 author_id integer,
687 primary key (post_id),
688 foreign key (author_id) references users on delete set null (author_id$0)
689);
690"), @r"
691 ╭▸
692 9 │ author_id integer,
693 │ ───────── 2. destination
694 10 │ primary key (post_id),
695 11 │ foreign key (author_id) references users on delete set null (author_id)
696 ╰╴ ─ 1. source
697 ");
698 }
699
700 #[test]
701 fn goto_references_constraint_table() {
702 assert_snapshot!(goto("
703create table t (
704 id serial primary key
705);
706
707create table u (
708 id serial primary key,
709 t_id int references t$0
710);
711"), @r"
712 ╭▸
713 2 │ create table t (
714 │ ─ 2. destination
715 ‡
716 8 │ t_id int references t
717 ╰╴ ─ 1. source
718 ");
719 }
720
721 #[test]
722 fn goto_references_constraint_column() {
723 assert_snapshot!(goto("
724create table t (
725 id serial primary key
726);
727
728create table u (
729 id serial primary key,
730 t_id int references t(id$0)
731);
732"), @r"
733 ╭▸
734 3 │ id serial primary key
735 │ ── 2. destination
736 ‡
737 8 │ t_id int references t(id)
738 ╰╴ ─ 1. source
739 ");
740 }
741
742 #[test]
743 fn goto_foreign_key_references_column() {
744 assert_snapshot!(goto("
745create table foo(id int);
746create table bar(
747 id int,
748 foo_id int,
749 foreign key (foo_id) references foo(id$0)
750);
751"), @r"
752 ╭▸
753 2 │ create table foo(id int);
754 │ ── 2. destination
755 ‡
756 6 │ foreign key (foo_id) references foo(id)
757 ╰╴ ─ 1. source
758 ");
759 }
760
761 #[test]
762 fn goto_foreign_key_local_column() {
763 assert_snapshot!(goto("
764create table bar(
765 id int,
766 foo_id int,
767 foreign key (foo_id$0) references foo(id)
768);
769"), @r"
770 ╭▸
771 4 │ foo_id int,
772 │ ────── 2. destination
773 5 │ foreign key (foo_id) references foo(id)
774 ╰╴ ─ 1. source
775 ");
776 }
777
778 #[test]
779 fn goto_check_constraint_column() {
780 assert_snapshot!(goto("
781create table t (
782 b int check (b > 10),
783 c int check (c$0 > 10) no inherit
784);
785"), @r"
786 ╭▸
787 4 │ c int check (c > 10) no inherit
788 │ ┬ ─ 1. source
789 │ │
790 ╰╴ 2. destination
791 ");
792 }
793
794 #[test]
795 fn goto_generated_column() {
796 assert_snapshot!(goto("
797create table t (
798 a int,
799 b int generated always as (
800 a$0 * 2
801 ) stored
802);
803"), @r"
804 ╭▸
805 3 │ a int,
806 │ ─ 2. destination
807 4 │ b int generated always as (
808 5 │ a * 2
809 ╰╴ ─ 1. source
810 ");
811 }
812
813 #[test]
814 fn goto_table_check_constraint_column() {
815 assert_snapshot!(goto("
816create table t (
817 a int,
818 b text,
819 check (a$0 > b)
820);
821"), @r"
822 ╭▸
823 3 │ a int,
824 │ ─ 2. destination
825 4 │ b text,
826 5 │ check (a > b)
827 ╰╴ ─ 1. source
828 ");
829 }
830
831 #[test]
832 fn goto_table_unique_constraint_column() {
833 assert_snapshot!(goto("
834create table t (
835 a int,
836 b text,
837 unique (a$0)
838);
839"), @r"
840 ╭▸
841 3 │ a int,
842 │ ─ 2. destination
843 4 │ b text,
844 5 │ unique (a)
845 ╰╴ ─ 1. source
846 ");
847 }
848
849 #[test]
850 fn goto_table_primary_key_constraint_column() {
851 assert_snapshot!(goto("
852create table t (
853 id bigint generated always as identity,
854 inserted_at timestamptz not null default now(),
855 primary key (id, inserted_at$0)
856);
857"), @r"
858 ╭▸
859 4 │ inserted_at timestamptz not null default now(),
860 │ ─────────── 2. destination
861 5 │ primary key (id, inserted_at)
862 ╰╴ ─ 1. source
863 ");
864 }
865
866 #[test]
867 fn goto_table_not_null_constraint_column() {
868 assert_snapshot!(goto("
869create table t (
870 id integer,
871 name text,
872 not null name$0
873);
874"), @r"
875 ╭▸
876 4 │ name text,
877 │ ──── 2. destination
878 5 │ not null name
879 ╰╴ ─ 1. source
880 ");
881 }
882
883 #[test]
884 fn goto_table_exclude_constraint_column() {
885 assert_snapshot!(goto("
886create table circles (
887 c circle,
888 exclude using gist (c$0 with &&)
889);
890"), @r"
891 ╭▸
892 3 │ c circle,
893 │ ─ 2. destination
894 4 │ exclude using gist (c with &&)
895 ╰╴ ─ 1. source
896 ");
897 }
898
899 #[test]
900 fn goto_table_exclude_constraint_include_column() {
901 assert_snapshot!(goto("
902create table t (
903 a int,
904 b text,
905 exclude using btree ( a with > )
906 include (a$0, b)
907);
908"), @r"
909 ╭▸
910 3 │ a int,
911 │ ─ 2. destination
912 ‡
913 6 │ include (a, b)
914 ╰╴ ─ 1. source
915 ");
916 }
917
918 #[test]
919 fn goto_table_exclude_constraint_where_column() {
920 assert_snapshot!(goto("
921create table t (
922 a int,
923 b text,
924 exclude using btree ( a with > )
925 where ( a$0 > 10 and b like '%foo' )
926);
927"), @r"
928 ╭▸
929 3 │ a int,
930 │ ─ 2. destination
931 ‡
932 6 │ where ( a > 10 and b like '%foo' )
933 ╰╴ ─ 1. source
934 ");
935 }
936
937 #[test]
938 fn goto_table_partition_by_column() {
939 assert_snapshot!(goto("
940create table t (
941 id bigint generated always as identity,
942 inserted_at timestamptz not null default now()
943) partition by range (inserted_at$0);
944"), @r"
945 ╭▸
946 4 │ inserted_at timestamptz not null default now()
947 │ ─────────── 2. destination
948 5 │ ) partition by range (inserted_at);
949 ╰╴ ─ 1. source
950 ");
951 }
952
953 #[test]
954 fn goto_table_partition_of_table() {
955 assert_snapshot!(goto("
956create table t ();
957create table t_2026_01_02 partition of t$0
958 for values from ('2026-01-02') to ('2026-01-03');
959"), @r"
960 ╭▸
961 2 │ create table t ();
962 │ ─ 2. destination
963 3 │ create table t_2026_01_02 partition of t
964 ╰╴ ─ 1. source
965 ");
966 }
967
968 #[test]
969 fn goto_create_table_like_clause() {
970 assert_snapshot!(goto("
971create table large_data_table(a text);
972create table t (
973 a text,
974 like large_data_table$0,
975 b integer
976);
977"), @r"
978 ╭▸
979 2 │ create table large_data_table(a text);
980 │ ──────────────── 2. destination
981 ‡
982 5 │ like large_data_table,
983 ╰╴ ─ 1. source
984 ");
985 }
986
987 #[test]
988 fn goto_create_table_inherits() {
989 assert_snapshot!(goto("
990create table bar(a int);
991create table t (a int)
992inherits (foo.bar, bar$0, buzz);
993"), @r"
994 ╭▸
995 2 │ create table bar(a int);
996 │ ─── 2. destination
997 3 │ create table t (a int)
998 4 │ inherits (foo.bar, bar, buzz);
999 ╰╴ ─ 1. source
1000 ");
1001 }
1002
1003 #[test]
1004 fn goto_drop_temp_table_shadows_public() {
1005 assert_snapshot!(goto("
1007create table t();
1008create temp table t();
1009drop table t$0;
1010"), @r"
1011 ╭▸
1012 2 │ create table t();
1013 │ ─ 2. destination
1014 3 │ create temp table t();
1015 4 │ drop table t;
1016 ╰╴ ─ 1. source
1017 ");
1018 }
1019
1020 #[test]
1021 fn goto_drop_public_table_when_temp_exists() {
1022 assert_snapshot!(goto("
1024create table t();
1025create temp table t();
1026drop table public.t$0;
1027"), @r"
1028 ╭▸
1029 2 │ create table t();
1030 │ ─ 2. destination
1031 3 │ create temp table t();
1032 4 │ drop table public.t;
1033 ╰╴ ─ 1. source
1034 ");
1035 }
1036
1037 #[test]
1038 fn goto_drop_table_defined_after() {
1039 assert_snapshot!(goto("
1040drop table t$0;
1041create table t();
1042"), @r"
1043 ╭▸
1044 2 │ drop table t;
1045 │ ─ 1. source
1046 3 │ create table t();
1047 ╰╴ ─ 2. destination
1048 ");
1049 }
1050
1051 #[test]
1052 fn goto_drop_type() {
1053 assert_snapshot!(goto("
1054create type t as enum ('a', 'b');
1055drop type t$0;
1056"), @r"
1057 ╭▸
1058 2 │ create type t as enum ('a', 'b');
1059 │ ─ 2. destination
1060 3 │ drop type t;
1061 ╰╴ ─ 1. source
1062 ");
1063 }
1064
1065 #[test]
1066 fn goto_drop_type_with_schema() {
1067 assert_snapshot!(goto("
1068create type public.t as enum ('a', 'b');
1069drop type t$0;
1070"), @r"
1071 ╭▸
1072 2 │ create type public.t as enum ('a', 'b');
1073 │ ─ 2. destination
1074 3 │ drop type t;
1075 ╰╴ ─ 1. source
1076 ");
1077
1078 assert_snapshot!(goto("
1079create type foo.t as enum ('a', 'b');
1080drop type foo.t$0;
1081"), @r"
1082 ╭▸
1083 2 │ create type foo.t as enum ('a', 'b');
1084 │ ─ 2. destination
1085 3 │ drop type foo.t;
1086 ╰╴ ─ 1. source
1087 ");
1088
1089 goto_not_found(
1090 "
1091create type t as enum ('a', 'b');
1092drop type foo.t$0;
1093",
1094 );
1095 }
1096
1097 #[test]
1098 fn goto_drop_type_defined_after() {
1099 assert_snapshot!(goto("
1100drop type t$0;
1101create type t as enum ('a', 'b');
1102"), @r"
1103 ╭▸
1104 2 │ drop type t;
1105 │ ─ 1. source
1106 3 │ create type t as enum ('a', 'b');
1107 ╰╴ ─ 2. destination
1108 ");
1109 }
1110
1111 #[test]
1112 fn goto_drop_type_composite() {
1113 assert_snapshot!(goto("
1114create type person as (name text, age int);
1115drop type person$0;
1116"), @r"
1117 ╭▸
1118 2 │ create type person as (name text, age int);
1119 │ ────── 2. destination
1120 3 │ drop type person;
1121 ╰╴ ─ 1. source
1122 ");
1123 }
1124
1125 #[test]
1126 fn goto_create_table_type_reference() {
1127 assert_snapshot!(goto("
1128create type person_info as (name text, email text);
1129create table user(id int, member person_info$0);
1130"), @r"
1131 ╭▸
1132 2 │ create type person_info as (name text, email text);
1133 │ ─────────── 2. destination
1134 3 │ create table user(id int, member person_info);
1135 ╰╴ ─ 1. source
1136 ");
1137 }
1138
1139 #[test]
1140 fn goto_create_table_type_reference_enum() {
1141 assert_snapshot!(goto("
1142create type mood as enum ('sad', 'ok', 'happy');
1143create table users(id int, mood mood$0);
1144"), @r"
1145 ╭▸
1146 2 │ create type mood as enum ('sad', 'ok', 'happy');
1147 │ ──── 2. destination
1148 3 │ create table users(id int, mood mood);
1149 ╰╴ ─ 1. source
1150 ");
1151 }
1152
1153 #[test]
1154 fn goto_create_table_type_reference_range() {
1155 assert_snapshot!(goto("
1156create type int4_range as range (subtype = int4);
1157create table metrics(id int, span int4_range$0);
1158"), @r"
1159 ╭▸
1160 2 │ create type int4_range as range (subtype = int4);
1161 │ ────────── 2. destination
1162 3 │ create table metrics(id int, span int4_range);
1163 ╰╴ ─ 1. source
1164 ");
1165 }
1166
1167 #[test]
1168 fn goto_create_table_type_reference_input_output() {
1169 assert_snapshot!(goto("
1170create type myint (input = myintin, output = myintout, like = int4);
1171create table data(id int, value myint$0);
1172"), @r"
1173 ╭▸
1174 2 │ create type myint (input = myintin, output = myintout, like = int4);
1175 │ ───── 2. destination
1176 3 │ create table data(id int, value myint);
1177 ╰╴ ─ 1. source
1178 ");
1179 }
1180
1181 #[test]
1182 fn goto_composite_type_field() {
1183 assert_snapshot!(goto("
1184create type person_info as (name text, email text);
1185create table user(id int, member person_info);
1186select (member).name$0 from user;
1187"), @r"
1188 ╭▸
1189 2 │ create type person_info as (name text, email text);
1190 │ ──── 2. destination
1191 3 │ create table user(id int, member person_info);
1192 4 │ select (member).name from user;
1193 ╰╴ ─ 1. source
1194 ");
1195 }
1196
1197 #[test]
1198 fn goto_drop_type_range() {
1199 assert_snapshot!(goto("
1200create type int4_range as range (subtype = int4);
1201drop type int4_range$0;
1202"), @r"
1203 ╭▸
1204 2 │ create type int4_range as range (subtype = int4);
1205 │ ────────── 2. destination
1206 3 │ drop type int4_range;
1207 ╰╴ ─ 1. source
1208 ");
1209 }
1210
1211 #[test]
1212 fn goto_drop_view() {
1213 assert_snapshot!(goto("
1214create view v as select 1;
1215drop view v$0;
1216"), @r"
1217 ╭▸
1218 2 │ create view v as select 1;
1219 │ ─ 2. destination
1220 3 │ drop view v;
1221 ╰╴ ─ 1. source
1222 ");
1223 }
1224
1225 #[test]
1226 fn goto_drop_materialized_view() {
1227 assert_snapshot!(goto("
1228create materialized view v as select 1;
1229drop materialized view v$0;
1230"), @r"
1231 ╭▸
1232 2 │ create materialized view v as select 1;
1233 │ ─ 2. destination
1234 3 │ drop materialized view v;
1235 ╰╴ ─ 1. source
1236 ");
1237 }
1238
1239 #[test]
1240 fn goto_drop_view_with_schema() {
1241 assert_snapshot!(goto("
1242create view public.v as select 1;
1243drop view v$0;
1244"), @r"
1245 ╭▸
1246 2 │ create view public.v as select 1;
1247 │ ─ 2. destination
1248 3 │ drop view v;
1249 ╰╴ ─ 1. source
1250 ");
1251
1252 assert_snapshot!(goto("
1253create view foo.v as select 1;
1254drop view foo.v$0;
1255"), @r"
1256 ╭▸
1257 2 │ create view foo.v as select 1;
1258 │ ─ 2. destination
1259 3 │ drop view foo.v;
1260 ╰╴ ─ 1. source
1261 ");
1262
1263 goto_not_found(
1264 "
1265create view v as select 1;
1266drop view foo.v$0;
1267",
1268 );
1269 }
1270
1271 #[test]
1272 fn goto_drop_temp_view() {
1273 assert_snapshot!(goto("
1274create temp view v as select 1;
1275drop view v$0;
1276"), @r"
1277 ╭▸
1278 2 │ create temp view v as select 1;
1279 │ ─ 2. destination
1280 3 │ drop view v;
1281 ╰╴ ─ 1. source
1282 ");
1283 }
1284
1285 #[test]
1286 fn goto_select_from_view() {
1287 assert_snapshot!(goto("
1288create view v as select 1;
1289select * from v$0;
1290"), @r"
1291 ╭▸
1292 2 │ create view v as select 1;
1293 │ ─ 2. destination
1294 3 │ select * from v;
1295 ╰╴ ─ 1. source
1296 ");
1297 }
1298
1299 #[test]
1300 fn goto_select_from_materialized_view() {
1301 assert_snapshot!(goto("
1302create materialized view v as select 1;
1303select * from v$0;
1304"), @r"
1305 ╭▸
1306 2 │ create materialized view v as select 1;
1307 │ ─ 2. destination
1308 3 │ select * from v;
1309 ╰╴ ─ 1. source
1310 ");
1311 }
1312
1313 #[test]
1314 fn goto_select_from_view_with_schema() {
1315 assert_snapshot!(goto("
1316create view public.v as select 1;
1317select * from public.v$0;
1318"), @r"
1319 ╭▸
1320 2 │ create view public.v as select 1;
1321 │ ─ 2. destination
1322 3 │ select * from public.v;
1323 ╰╴ ─ 1. source
1324 ");
1325 }
1326
1327 #[test]
1328 fn goto_view_column() {
1329 assert_snapshot!(goto("
1330create view v as select 1 as a;
1331select a$0 from v;
1332"), @r"
1333 ╭▸
1334 2 │ create view v as select 1 as a;
1335 │ ─ 2. destination
1336 3 │ select a from v;
1337 ╰╴ ─ 1. source
1338 ");
1339 }
1340
1341 #[test]
1342 fn goto_view_column_qualified() {
1343 assert_snapshot!(goto("
1344create view v as select 1 as a;
1345select v.a$0 from v;
1346"), @r"
1347 ╭▸
1348 2 │ create view v as select 1 as a;
1349 │ ─ 2. destination
1350 3 │ select v.a from v;
1351 ╰╴ ─ 1. source
1352 ");
1353 }
1354
1355 #[test]
1356 fn goto_view_with_explicit_column_list() {
1357 assert_snapshot!(goto("
1358create view v(col1) as select 1;
1359select * from v$0;
1360"), @r"
1361 ╭▸
1362 2 │ create view v(col1) as select 1;
1363 │ ─ 2. destination
1364 3 │ select * from v;
1365 ╰╴ ─ 1. source
1366 ");
1367 }
1368
1369 #[test]
1370 fn goto_view_column_with_explicit_column_list() {
1371 assert_snapshot!(goto("
1372 create view v(col1) as select 1;
1373 select col1$0 from v;
1374 "), @r"
1375 ╭▸
1376 2 │ create view v(col1) as select 1;
1377 │ ──── 2. destination
1378 3 │ select col1 from v;
1379 ╰╴ ─ 1. source
1380 ");
1381 }
1382
1383 #[test]
1384 fn goto_view_column_with_schema() {
1385 assert_snapshot!(goto("
1386create view public.v as select 1 as a;
1387select a$0 from public.v;
1388"), @r"
1389 ╭▸
1390 2 │ create view public.v as select 1 as a;
1391 │ ─ 2. destination
1392 3 │ select a from public.v;
1393 ╰╴ ─ 1. source
1394 ");
1395 }
1396
1397 #[test]
1398 fn goto_view_multiple_columns() {
1399 assert_snapshot!(goto("
1400create view v as select 1 as a, 2 as b;
1401select b$0 from v;
1402"), @r"
1403 ╭▸
1404 2 │ create view v as select 1 as a, 2 as b;
1405 │ ─ 2. destination
1406 3 │ select b from v;
1407 ╰╴ ─ 1. source
1408 ");
1409 }
1410
1411 #[test]
1412 fn goto_view_column_from_table() {
1413 assert_snapshot!(goto("
1414create table t(x int, y int);
1415create view v as select x, y from t;
1416select x$0 from v;
1417"), @r"
1418 ╭▸
1419 3 │ create view v as select x, y from t;
1420 │ ─ 2. destination
1421 4 │ select x from v;
1422 ╰╴ ─ 1. source
1423 ");
1424 }
1425
1426 #[test]
1427 fn goto_view_column_with_table_preference() {
1428 assert_snapshot!(goto("
1429create table v(a int);
1430create view vw as select 1 as a;
1431select a$0 from v;
1432"), @r"
1433 ╭▸
1434 2 │ create table v(a int);
1435 │ ─ 2. destination
1436 3 │ create view vw as select 1 as a;
1437 4 │ select a from v;
1438 ╰╴ ─ 1. source
1439 ");
1440 }
1441
1442 #[test]
1443 fn goto_cast_operator() {
1444 assert_snapshot!(goto("
1445create type foo as enum ('a', 'b');
1446select x::foo$0;
1447"), @r"
1448 ╭▸
1449 2 │ create type foo as enum ('a', 'b');
1450 │ ─── 2. destination
1451 3 │ select x::foo;
1452 ╰╴ ─ 1. source
1453 ");
1454 }
1455
1456 #[test]
1457 fn goto_cast_function() {
1458 assert_snapshot!(goto("
1459create type bar as enum ('x', 'y');
1460select cast(x as bar$0);
1461"), @r"
1462 ╭▸
1463 2 │ create type bar as enum ('x', 'y');
1464 │ ─── 2. destination
1465 3 │ select cast(x as bar);
1466 ╰╴ ─ 1. source
1467 ");
1468 }
1469
1470 #[test]
1471 fn goto_cast_with_schema() {
1472 assert_snapshot!(goto("
1473create type public.baz as enum ('m', 'n');
1474select x::public.baz$0;
1475"), @r"
1476 ╭▸
1477 2 │ create type public.baz as enum ('m', 'n');
1478 │ ─── 2. destination
1479 3 │ select x::public.baz;
1480 ╰╴ ─ 1. source
1481 ");
1482 }
1483
1484 #[test]
1485 fn goto_cast_composite_type() {
1486 assert_snapshot!(goto("
1487create type person_info as (name varchar(50), age int);
1488select ('Alice', 30)::person_info$0;
1489"), @r"
1490 ╭▸
1491 2 │ create type person_info as (name varchar(50), age int);
1492 │ ─────────── 2. destination
1493 3 │ select ('Alice', 30)::person_info;
1494 ╰╴ ─ 1. source
1495 ");
1496 }
1497
1498 #[test]
1499 fn goto_cast_composite_type_in_cte() {
1500 assert_snapshot!(goto("
1501create type person_info as (name varchar(50), age int);
1502with team as (
1503 select 1 as id, ('Alice', 30)::person_info$0 as member
1504)
1505select * from team;
1506"), @r"
1507 ╭▸
1508 2 │ create type person_info as (name varchar(50), age int);
1509 │ ─────────── 2. destination
1510 3 │ with team as (
1511 4 │ select 1 as id, ('Alice', 30)::person_info as member
1512 ╰╴ ─ 1. source
1513 ");
1514 }
1515
1516 #[test]
1517 fn goto_composite_type_field_name() {
1518 assert_snapshot!(goto("
1519create type person_info as (name varchar(50), age int);
1520with team as (
1521 select 1 as id, ('Alice', 30)::person_info as member
1522)
1523select (member).name$0, (member).age from team;
1524"), @r"
1525 ╭▸
1526 2 │ create type person_info as (name varchar(50), age int);
1527 │ ──── 2. destination
1528 ‡
1529 6 │ select (member).name, (member).age from team;
1530 ╰╴ ─ 1. source
1531 ");
1532 }
1533
1534 #[test]
1535 fn goto_composite_type_field_in_where() {
1536 assert_snapshot!(goto("
1537create type person_info as (name varchar(50), age int);
1538with team as (
1539 select 1 as id, ('Alice', 30)::person_info as member
1540 union all
1541 select 2, ('Bob', 25)::person_info
1542)
1543select (member).name, (member).age
1544from team
1545where (member).age$0 >= 18;
1546"), @r"
1547 ╭▸
1548 2 │ create type person_info as (name varchar(50), age int);
1549 │ ─── 2. destination
1550 ‡
1551 10 │ where (member).age >= 18;
1552 ╰╴ ─ 1. source
1553 ");
1554 }
1555
1556 #[test]
1557 fn goto_composite_type_field_base() {
1558 assert_snapshot!(goto("
1559create type person_info as (name varchar(50), age int);
1560with team as (
1561 select 1 as id, ('Alice', 30)::person_info as member
1562)
1563select (member$0).age from team;
1564"), @r"
1565 ╭▸
1566 4 │ select 1 as id, ('Alice', 30)::person_info as member
1567 │ ────── 2. destination
1568 5 │ )
1569 6 │ select (member).age from team;
1570 ╰╴ ─ 1. source
1571 ");
1572 }
1573
1574 #[test]
1575 fn goto_composite_type_field_nested_parens() {
1576 assert_snapshot!(goto("
1577create type person_info as (name varchar(50), age int);
1578with team as (
1579 select 1 as id, ('Alice', 30)::person_info as member
1580)
1581select ((((member))).name$0) from team;
1582"), @r"
1583 ╭▸
1584 2 │ create type person_info as (name varchar(50), age int);
1585 │ ──── 2. destination
1586 ‡
1587 6 │ select ((((member))).name) from team;
1588 ╰╴ ─ 1. source
1589 ");
1590 }
1591
1592 #[test]
1593 fn begin_to_rollback() {
1594 assert_snapshot!(goto(
1595 "
1596begin$0;
1597select 1;
1598rollback;
1599commit;
1600",
1601 ), @r"
1602 ╭▸
1603 2 │ begin;
1604 │ ─ 1. source
1605 3 │ select 1;
1606 4 │ rollback;
1607 ╰╴──────── 2. destination
1608 ");
1609 }
1610
1611 #[test]
1612 fn commit_to_begin() {
1613 assert_snapshot!(goto(
1614 "
1615begin;
1616select 1;
1617commit$0;
1618",
1619 ), @r"
1620 ╭▸
1621 2 │ begin;
1622 │ ───── 2. destination
1623 3 │ select 1;
1624 4 │ commit;
1625 ╰╴ ─ 1. source
1626 ");
1627 }
1628
1629 #[test]
1630 fn begin_to_commit() {
1631 assert_snapshot!(goto(
1632 "
1633begin$0;
1634select 1;
1635commit;
1636",
1637 ), @r"
1638 ╭▸
1639 2 │ begin;
1640 │ ─ 1. source
1641 3 │ select 1;
1642 4 │ commit;
1643 ╰╴────── 2. destination
1644 ");
1645 }
1646
1647 #[test]
1648 fn commit_to_start_transaction() {
1649 assert_snapshot!(goto(
1650 "
1651start transaction;
1652select 1;
1653commit$0;
1654",
1655 ), @r"
1656 ╭▸
1657 2 │ start transaction;
1658 │ ───────────────── 2. destination
1659 3 │ select 1;
1660 4 │ commit;
1661 ╰╴ ─ 1. source
1662 ");
1663 }
1664
1665 #[test]
1666 fn start_transaction_to_commit() {
1667 assert_snapshot!(goto(
1668 "
1669start$0 transaction;
1670select 1;
1671commit;
1672",
1673 ), @r"
1674 ╭▸
1675 2 │ start transaction;
1676 │ ─ 1. source
1677 3 │ select 1;
1678 4 │ commit;
1679 ╰╴────── 2. destination
1680 ");
1681 }
1682
1683 #[test]
1684 fn goto_with_search_path() {
1685 assert_snapshot!(goto(r#"
1686set search_path to "foo", public;
1687create table foo.t();
1688drop table t$0;
1689"#), @r"
1690 ╭▸
1691 3 │ create table foo.t();
1692 │ ─ 2. destination
1693 4 │ drop table t;
1694 ╰╴ ─ 1. source
1695 ");
1696 }
1697
1698 #[test]
1699 fn goto_with_search_path_and_unspecified_table() {
1700 assert_snapshot!(goto(r#"
1701set search_path to foo,bar;
1702create table t();
1703drop table foo.t$0;
1704"#), @r"
1705 ╭▸
1706 3 │ create table t();
1707 │ ─ 2. destination
1708 4 │ drop table foo.t;
1709 ╰╴ ─ 1. source
1710 ");
1711 }
1712
1713 #[test]
1714 fn goto_with_search_path_empty() {
1715 goto_not_found(
1716 r#"
1717set search_path = '';
1718create table t();
1719drop table t$0;
1720"#,
1721 );
1722 }
1723
1724 #[test]
1725 fn goto_with_search_path_like_variable() {
1726 goto_not_found(
1728 "
1729set bar.search_path to foo, public;
1730create table foo.t();
1731drop table t$0;
1732",
1733 )
1734 }
1735
1736 #[test]
1737 fn goto_with_search_path_second_schema() {
1738 assert_snapshot!(goto("
1739set search_path to foo, bar, public;
1740create table bar.t();
1741drop table t$0;
1742"), @r"
1743 ╭▸
1744 3 │ create table bar.t();
1745 │ ─ 2. destination
1746 4 │ drop table t;
1747 ╰╴ ─ 1. source
1748 ");
1749 }
1750
1751 #[test]
1752 fn goto_with_search_path_skips_first() {
1753 assert_snapshot!(goto("
1754set search_path to foo, bar, public;
1755create table foo.t();
1756create table bar.t();
1757drop table t$0;
1758"), @r"
1759 ╭▸
1760 3 │ create table foo.t();
1761 │ ─ 2. destination
1762 4 │ create table bar.t();
1763 5 │ drop table t;
1764 ╰╴ ─ 1. source
1765 ");
1766 }
1767
1768 #[test]
1769 fn goto_without_search_path_uses_default() {
1770 assert_snapshot!(goto("
1771create table foo.t();
1772create table public.t();
1773drop table t$0;
1774"), @r"
1775 ╭▸
1776 3 │ create table public.t();
1777 │ ─ 2. destination
1778 4 │ drop table t;
1779 ╰╴ ─ 1. source
1780 ");
1781 }
1782
1783 #[test]
1784 fn goto_with_set_schema() {
1785 assert_snapshot!(goto("
1786set schema 'myschema';
1787create table myschema.t();
1788drop table t$0;
1789"), @r"
1790 ╭▸
1791 3 │ create table myschema.t();
1792 │ ─ 2. destination
1793 4 │ drop table t;
1794 ╰╴ ─ 1. source
1795 ");
1796 }
1797
1798 #[test]
1799 fn goto_with_set_schema_ignores_other_schemas() {
1800 assert_snapshot!(goto("
1801set schema 'myschema';
1802create table public.t();
1803create table myschema.t();
1804drop table t$0;
1805"), @r"
1806 ╭▸
1807 4 │ create table myschema.t();
1808 │ ─ 2. destination
1809 5 │ drop table t;
1810 ╰╴ ─ 1. source
1811 ");
1812 }
1813
1814 #[test]
1815 fn goto_with_search_path_changed_twice() {
1816 assert_snapshot!(goto("
1817set search_path to foo;
1818create table foo.t();
1819set search_path to bar;
1820create table bar.t();
1821drop table t$0;
1822"), @r"
1823 ╭▸
1824 5 │ create table bar.t();
1825 │ ─ 2. destination
1826 6 │ drop table t;
1827 ╰╴ ─ 1. source
1828 ");
1829
1830 assert_snapshot!(goto("
1831set search_path to foo;
1832create table foo.t();
1833drop table t$0;
1834set search_path to bar;
1835create table bar.t();
1836drop table t;
1837"), @r"
1838 ╭▸
1839 3 │ create table foo.t();
1840 │ ─ 2. destination
1841 4 │ drop table t;
1842 ╰╴ ─ 1. source
1843 ");
1844 }
1845
1846 #[test]
1847 fn goto_with_empty_search_path() {
1848 goto_not_found(
1849 "
1850set search_path to '';
1851create table public.t();
1852drop table t$0;
1853",
1854 )
1855 }
1856
1857 #[test]
1858 fn goto_with_search_path_uppercase() {
1859 assert_snapshot!(goto("
1860SET SEARCH_PATH TO foo;
1861create table foo.t();
1862drop table t$0;
1863"), @r"
1864 ╭▸
1865 3 │ create table foo.t();
1866 │ ─ 2. destination
1867 4 │ drop table t;
1868 ╰╴ ─ 1. source
1869 ");
1870 }
1871
1872 #[test]
1873 fn goto_table_stmt() {
1874 assert_snapshot!(goto("
1875create table t();
1876table t$0;
1877"), @r"
1878 ╭▸
1879 2 │ create table t();
1880 │ ─ 2. destination
1881 3 │ table t;
1882 ╰╴ ─ 1. source
1883 ");
1884 }
1885
1886 #[test]
1887 fn goto_table_stmt_with_schema() {
1888 assert_snapshot!(goto("
1889create table public.t();
1890table public.t$0;
1891"), @r"
1892 ╭▸
1893 2 │ create table public.t();
1894 │ ─ 2. destination
1895 3 │ table public.t;
1896 ╰╴ ─ 1. source
1897 ");
1898 }
1899
1900 #[test]
1901 fn goto_table_stmt_with_search_path() {
1902 assert_snapshot!(goto("
1903set search_path to foo;
1904create table foo.t();
1905table t$0;
1906"), @r"
1907 ╭▸
1908 3 │ create table foo.t();
1909 │ ─ 2. destination
1910 4 │ table t;
1911 ╰╴ ─ 1. source
1912 ");
1913 }
1914
1915 #[test]
1916 fn goto_drop_index() {
1917 assert_snapshot!(goto("
1918create index idx_name on t(x);
1919drop index idx_name$0;
1920"), @r"
1921 ╭▸
1922 2 │ create index idx_name on t(x);
1923 │ ──────── 2. destination
1924 3 │ drop index idx_name;
1925 ╰╴ ─ 1. source
1926 ");
1927 }
1928
1929 #[test]
1930 fn goto_drop_index_with_schema() {
1931 assert_snapshot!(goto(r#"
1932set search_path to public;
1933create index idx_name on t(x);
1934drop index public.idx_name$0;
1935"#), @r"
1936 ╭▸
1937 3 │ create index idx_name on t(x);
1938 │ ──────── 2. destination
1939 4 │ drop index public.idx_name;
1940 ╰╴ ─ 1. source
1941 ");
1942 }
1943
1944 #[test]
1945 fn goto_drop_index_defined_after() {
1946 assert_snapshot!(goto("
1947drop index idx_name$0;
1948create index idx_name on t(x);
1949"), @r"
1950 ╭▸
1951 2 │ drop index idx_name;
1952 │ ─ 1. source
1953 3 │ create index idx_name on t(x);
1954 ╰╴ ──────── 2. destination
1955 ");
1956 }
1957
1958 #[test]
1959 fn goto_index_definition_returns_self() {
1960 assert_snapshot!(goto("
1961create index idx_name$0 on t(x);
1962"), @r"
1963 ╭▸
1964 2 │ create index idx_name on t(x);
1965 │ ┬──────┬
1966 │ │ │
1967 │ │ 1. source
1968 ╰╴ 2. destination
1969 ");
1970 }
1971
1972 #[test]
1973 fn goto_drop_index_with_search_path() {
1974 assert_snapshot!(goto(r#"
1975create index idx_name on t(x);
1976set search_path to bar;
1977create index idx_name on f(x);
1978set search_path to default;
1979drop index idx_name$0;
1980"#), @r"
1981 ╭▸
1982 2 │ create index idx_name on t(x);
1983 │ ──────── 2. destination
1984 ‡
1985 6 │ drop index idx_name;
1986 ╰╴ ─ 1. source
1987 ");
1988 }
1989
1990 #[test]
1991 fn goto_drop_index_multiple() {
1992 assert_snapshot!(goto("
1993create index idx1 on t(x);
1994create index idx2 on t(y);
1995drop index idx1, idx2$0;
1996"), @r"
1997 ╭▸
1998 3 │ create index idx2 on t(y);
1999 │ ──── 2. destination
2000 4 │ drop index idx1, idx2;
2001 ╰╴ ─ 1. source
2002 ");
2003 }
2004
2005 #[test]
2006 fn goto_create_index_table() {
2007 assert_snapshot!(goto("
2008create table users(id int);
2009create index idx_users on users$0(id);
2010"), @r"
2011 ╭▸
2012 2 │ create table users(id int);
2013 │ ───── 2. destination
2014 3 │ create index idx_users on users(id);
2015 ╰╴ ─ 1. source
2016 ");
2017 }
2018
2019 #[test]
2020 fn goto_create_index_table_with_schema() {
2021 assert_snapshot!(goto("
2022create table public.users(id int);
2023create index idx_users on public.users$0(id);
2024"), @r"
2025 ╭▸
2026 2 │ create table public.users(id int);
2027 │ ───── 2. destination
2028 3 │ create index idx_users on public.users(id);
2029 ╰╴ ─ 1. source
2030 ");
2031 }
2032
2033 #[test]
2034 fn goto_create_index_table_with_search_path() {
2035 assert_snapshot!(goto(r#"
2036set search_path to foo;
2037create table foo.users(id int);
2038create index idx_users on users$0(id);
2039"#), @r"
2040 ╭▸
2041 3 │ create table foo.users(id int);
2042 │ ───── 2. destination
2043 4 │ create index idx_users on users(id);
2044 ╰╴ ─ 1. source
2045 ");
2046 }
2047
2048 #[test]
2049 fn goto_create_index_temp_table() {
2050 assert_snapshot!(goto("
2051create temp table users(id int);
2052create index idx_users on users$0(id);
2053"), @r"
2054 ╭▸
2055 2 │ create temp table users(id int);
2056 │ ───── 2. destination
2057 3 │ create index idx_users on users(id);
2058 ╰╴ ─ 1. source
2059 ");
2060 }
2061
2062 #[test]
2063 fn goto_create_index_column() {
2064 assert_snapshot!(goto("
2065create table users(id int, email text);
2066create index idx_email on users(email$0);
2067"), @r"
2068 ╭▸
2069 2 │ create table users(id int, email text);
2070 │ ───── 2. destination
2071 3 │ create index idx_email on users(email);
2072 ╰╴ ─ 1. source
2073 ");
2074 }
2075
2076 #[test]
2077 fn goto_create_index_first_column() {
2078 assert_snapshot!(goto("
2079create table users(id int, email text);
2080create index idx_id on users(id$0);
2081"), @r"
2082 ╭▸
2083 2 │ create table users(id int, email text);
2084 │ ── 2. destination
2085 3 │ create index idx_id on users(id);
2086 ╰╴ ─ 1. source
2087 ");
2088 }
2089
2090 #[test]
2091 fn goto_create_index_multiple_columns() {
2092 assert_snapshot!(goto("
2093create table users(id int, email text, name text);
2094create index idx_users on users(id, email$0, name);
2095"), @r"
2096 ╭▸
2097 2 │ create table users(id int, email text, name text);
2098 │ ───── 2. destination
2099 3 │ create index idx_users on users(id, email, name);
2100 ╰╴ ─ 1. source
2101 ");
2102 }
2103
2104 #[test]
2105 fn goto_create_index_column_with_schema() {
2106 assert_snapshot!(goto("
2107create table public.users(id int, email text);
2108create index idx_email on public.users(email$0);
2109"), @r"
2110 ╭▸
2111 2 │ create table public.users(id int, email text);
2112 │ ───── 2. destination
2113 3 │ create index idx_email on public.users(email);
2114 ╰╴ ─ 1. source
2115 ");
2116 }
2117
2118 #[test]
2119 fn goto_create_index_column_temp_table() {
2120 assert_snapshot!(goto("
2121create temp table users(id int, email text);
2122create index idx_email on users(email$0);
2123"), @r"
2124 ╭▸
2125 2 │ create temp table users(id int, email text);
2126 │ ───── 2. destination
2127 3 │ create index idx_email on users(email);
2128 ╰╴ ─ 1. source
2129 ");
2130 }
2131
2132 #[test]
2133 fn goto_drop_function() {
2134 assert_snapshot!(goto("
2135create function foo() returns int as $$ select 1 $$ language sql;
2136drop function foo$0();
2137"), @r"
2138 ╭▸
2139 2 │ create function foo() returns int as $$ select 1 $$ language sql;
2140 │ ─── 2. destination
2141 3 │ drop function foo();
2142 ╰╴ ─ 1. source
2143 ");
2144 }
2145
2146 #[test]
2147 fn goto_drop_function_with_schema() {
2148 assert_snapshot!(goto("
2149set search_path to public;
2150create function foo() returns int as $$ select 1 $$ language sql;
2151drop function public.foo$0();
2152"), @r"
2153 ╭▸
2154 3 │ create function foo() returns int as $$ select 1 $$ language sql;
2155 │ ─── 2. destination
2156 4 │ drop function public.foo();
2157 ╰╴ ─ 1. source
2158 ");
2159 }
2160
2161 #[test]
2162 fn goto_drop_function_defined_after() {
2163 assert_snapshot!(goto("
2164drop function foo$0();
2165create function foo() returns int as $$ select 1 $$ language sql;
2166"), @r"
2167 ╭▸
2168 2 │ drop function foo();
2169 │ ─ 1. source
2170 3 │ create function foo() returns int as $$ select 1 $$ language sql;
2171 ╰╴ ─── 2. destination
2172 ");
2173 }
2174
2175 #[test]
2176 fn goto_function_definition_returns_self() {
2177 assert_snapshot!(goto("
2178create function foo$0() returns int as $$ select 1 $$ language sql;
2179"), @r"
2180 ╭▸
2181 2 │ create function foo() returns int as $$ select 1 $$ language sql;
2182 │ ┬─┬
2183 │ │ │
2184 │ │ 1. source
2185 ╰╴ 2. destination
2186 ");
2187 }
2188
2189 #[test]
2190 fn goto_drop_function_with_search_path() {
2191 assert_snapshot!(goto("
2192create function foo() returns int as $$ select 1 $$ language sql;
2193set search_path to bar;
2194create function foo() returns int as $$ select 1 $$ language sql;
2195set search_path to default;
2196drop function foo$0();
2197"), @r"
2198 ╭▸
2199 2 │ create function foo() returns int as $$ select 1 $$ language sql;
2200 │ ─── 2. destination
2201 ‡
2202 6 │ drop function foo();
2203 ╰╴ ─ 1. source
2204 ");
2205 }
2206
2207 #[test]
2208 fn goto_drop_function_multiple() {
2209 assert_snapshot!(goto("
2210create function foo() returns int as $$ select 1 $$ language sql;
2211create function bar() returns int as $$ select 1 $$ language sql;
2212drop function foo(), bar$0();
2213"), @r"
2214 ╭▸
2215 3 │ create function bar() returns int as $$ select 1 $$ language sql;
2216 │ ─── 2. destination
2217 4 │ drop function foo(), bar();
2218 ╰╴ ─ 1. source
2219 ");
2220 }
2221
2222 #[test]
2223 fn goto_drop_function_overloaded() {
2224 assert_snapshot!(goto("
2225create function add(complex) returns complex as $$ select null $$ language sql;
2226create function add(bigint) returns bigint as $$ select 1 $$ language sql;
2227drop function add$0(complex);
2228"), @r"
2229 ╭▸
2230 2 │ create function add(complex) returns complex as $$ select null $$ language sql;
2231 │ ─── 2. destination
2232 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
2233 4 │ drop function add(complex);
2234 ╰╴ ─ 1. source
2235 ");
2236 }
2237
2238 #[test]
2239 fn goto_drop_function_second_overload() {
2240 assert_snapshot!(goto("
2241create function add(complex) returns complex as $$ select null $$ language sql;
2242create function add(bigint) returns bigint as $$ select 1 $$ language sql;
2243drop function add$0(bigint);
2244"), @r"
2245 ╭▸
2246 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
2247 │ ─── 2. destination
2248 4 │ drop function add(bigint);
2249 ╰╴ ─ 1. source
2250 ");
2251 }
2252
2253 #[test]
2254 fn goto_select_function_call() {
2255 assert_snapshot!(goto("
2256create function foo() returns int as $$ select 1 $$ language sql;
2257select foo$0();
2258"), @r"
2259 ╭▸
2260 2 │ create function foo() returns int as $$ select 1 $$ language sql;
2261 │ ─── 2. destination
2262 3 │ select foo();
2263 ╰╴ ─ 1. source
2264 ");
2265 }
2266
2267 #[test]
2268 fn goto_select_aggregate_call() {
2269 assert_snapshot!(goto("
2270create aggregate foo(int) (
2271 sfunc = int4pl,
2272 stype = int,
2273 initcond = '0'
2274);
2275
2276select foo$0(1);
2277"), @r"
2278 ╭▸
2279 2 │ create aggregate foo(int) (
2280 │ ─── 2. destination
2281 ‡
2282 8 │ select foo(1);
2283 ╰╴ ─ 1. source
2284 ");
2285 }
2286
2287 #[test]
2288 fn goto_default_constraint_function_call() {
2289 assert_snapshot!(goto("
2290create function f() returns int as 'select 1' language sql;
2291create table t(
2292 a int default f$0()
2293);
2294"), @r"
2295 ╭▸
2296 2 │ create function f() returns int as 'select 1' language sql;
2297 │ ─ 2. destination
2298 3 │ create table t(
2299 4 │ a int default f()
2300 ╰╴ ─ 1. source
2301 ");
2302 }
2303
2304 #[test]
2305 fn goto_select_function_call_with_schema() {
2306 assert_snapshot!(goto("
2307create function public.foo() returns int as $$ select 1 $$ language sql;
2308select public.foo$0();
2309"), @r"
2310 ╭▸
2311 2 │ create function public.foo() returns int as $$ select 1 $$ language sql;
2312 │ ─── 2. destination
2313 3 │ select public.foo();
2314 ╰╴ ─ 1. source
2315 ");
2316 }
2317
2318 #[test]
2319 fn goto_select_function_call_with_search_path() {
2320 assert_snapshot!(goto("
2321set search_path to myschema;
2322create function foo() returns int as $$ select 1 $$ language sql;
2323select myschema.foo$0();
2324"), @r"
2325 ╭▸
2326 3 │ create function foo() returns int as $$ select 1 $$ language sql;
2327 │ ─── 2. destination
2328 4 │ select myschema.foo();
2329 ╰╴ ─ 1. source
2330 ");
2331 }
2332
2333 #[test]
2334 fn goto_function_call_style_column_access() {
2335 assert_snapshot!(goto("
2336create table t(a int, b int);
2337select a$0(t) from t;
2338"), @r"
2339 ╭▸
2340 2 │ create table t(a int, b int);
2341 │ ─ 2. destination
2342 3 │ select a(t) from t;
2343 ╰╴ ─ 1. source
2344 ");
2345 }
2346
2347 #[test]
2348 fn goto_function_call_style_column_access_with_function_precedence() {
2349 assert_snapshot!(goto("
2350create table t(a int, b int);
2351create function b(t) returns int as 'select 1' LANGUAGE sql;
2352select b$0(t) from t;
2353"), @r"
2354 ╭▸
2355 3 │ create function b(t) returns int as 'select 1' LANGUAGE sql;
2356 │ ─ 2. destination
2357 4 │ select b(t) from t;
2358 ╰╴ ─ 1. source
2359 ");
2360 }
2361
2362 #[test]
2363 fn goto_function_call_style_column_access_table_arg() {
2364 assert_snapshot!(goto("
2365create table t(a int, b int);
2366select a(t$0) from t;
2367"), @r"
2368 ╭▸
2369 2 │ create table t(a int, b int);
2370 │ ─ 2. destination
2371 3 │ select a(t) from t;
2372 ╰╴ ─ 1. source
2373 ");
2374 }
2375
2376 #[test]
2377 fn goto_function_call_style_column_access_table_arg_with_function() {
2378 assert_snapshot!(goto("
2379create table t(a int, b int);
2380create function b(t) returns int as 'select 1' LANGUAGE sql;
2381select b(t$0) from t;
2382"), @r"
2383 ╭▸
2384 2 │ create table t(a int, b int);
2385 │ ─ 2. destination
2386 3 │ create function b(t) returns int as 'select 1' LANGUAGE sql;
2387 4 │ select b(t) from t;
2388 ╰╴ ─ 1. source
2389 ");
2390 }
2391
2392 #[test]
2393 fn goto_function_call_multiple_args_not_column_access() {
2394 goto_not_found(
2395 "
2396create table t(a int, b int);
2397select a$0(t, 1) from t;
2398",
2399 );
2400 }
2401
2402 #[test]
2403 fn goto_field_style_function_call() {
2404 assert_snapshot!(goto("
2405create table t(a int);
2406create function b(t) returns int as 'select 1' language sql;
2407select t.b$0 from t;
2408"), @r"
2409 ╭▸
2410 3 │ create function b(t) returns int as 'select 1' language sql;
2411 │ ─ 2. destination
2412 4 │ select t.b from t;
2413 ╰╴ ─ 1. source
2414 ");
2415 }
2416
2417 #[test]
2418 fn goto_field_style_function_call_column_precedence() {
2419 assert_snapshot!(goto("
2420create table t(a int, b int);
2421create function b(t) returns int as 'select 1' language sql;
2422select t.b$0 from t;
2423"), @r"
2424 ╭▸
2425 2 │ create table t(a int, b int);
2426 │ ─ 2. destination
2427 3 │ create function b(t) returns int as 'select 1' language sql;
2428 4 │ select t.b from t;
2429 ╰╴ ─ 1. source
2430 ");
2431 }
2432
2433 #[test]
2434 fn goto_field_style_function_call_table_ref() {
2435 assert_snapshot!(goto("
2436create table t(a int);
2437create function b(t) returns int as 'select 1' language sql;
2438select t$0.b from t;
2439"), @r"
2440 ╭▸
2441 2 │ create table t(a int);
2442 │ ─ 2. destination
2443 3 │ create function b(t) returns int as 'select 1' language sql;
2444 4 │ select t.b from t;
2445 ╰╴ ─ 1. source
2446 ");
2447 }
2448
2449 #[test]
2450 fn goto_function_call_style_in_where() {
2451 assert_snapshot!(goto("
2452create table t(a int, b int);
2453select * from t where a$0(t) > 0;
2454"), @r"
2455 ╭▸
2456 2 │ create table t(a int, b int);
2457 │ ─ 2. destination
2458 3 │ select * from t where a(t) > 0;
2459 ╰╴ ─ 1. source
2460 ");
2461 }
2462
2463 #[test]
2464 fn goto_function_call_style_in_where_function_precedence() {
2465 assert_snapshot!(goto("
2466create table t(a int, b int);
2467create function b(t) returns int as 'select 1' language sql;
2468select * from t where b$0(t) > 0;
2469"), @r"
2470 ╭▸
2471 3 │ create function b(t) returns int as 'select 1' language sql;
2472 │ ─ 2. destination
2473 4 │ select * from t where b(t) > 0;
2474 ╰╴ ─ 1. source
2475 ");
2476 }
2477
2478 #[test]
2479 fn goto_field_style_function_call_in_where() {
2480 assert_snapshot!(goto("
2481create table t(a int);
2482create function b(t) returns int as 'select 1' language sql;
2483select * from t where t.b$0 > 0;
2484"), @r"
2485 ╭▸
2486 3 │ create function b(t) returns int as 'select 1' language sql;
2487 │ ─ 2. destination
2488 4 │ select * from t where t.b > 0;
2489 ╰╴ ─ 1. source
2490 ");
2491 }
2492
2493 #[test]
2494 fn goto_field_style_in_where_column_precedence() {
2495 assert_snapshot!(goto("
2496create table t(a int, b int);
2497create function b(t) returns int as 'select 1' language sql;
2498select * from t where t.b$0 > 0;
2499"), @r"
2500 ╭▸
2501 2 │ create table t(a int, b int);
2502 │ ─ 2. destination
2503 3 │ create function b(t) returns int as 'select 1' language sql;
2504 4 │ select * from t where t.b > 0;
2505 ╰╴ ─ 1. source
2506 ");
2507 }
2508
2509 #[test]
2510 fn goto_function_call_style_table_arg_in_where() {
2511 assert_snapshot!(goto("
2512create table t(a int);
2513select * from t where a(t$0) > 2;
2514"), @r"
2515 ╭▸
2516 2 │ create table t(a int);
2517 │ ─ 2. destination
2518 3 │ select * from t where a(t) > 2;
2519 ╰╴ ─ 1. source
2520 ");
2521 }
2522
2523 #[test]
2524 fn goto_qualified_table_ref_in_where() {
2525 assert_snapshot!(goto("
2526create table t(a int);
2527create function b(t) returns int as 'select 1' language sql;
2528select * from t where t$0.b > 2;
2529"), @r"
2530 ╭▸
2531 2 │ create table t(a int);
2532 │ ─ 2. destination
2533 3 │ create function b(t) returns int as 'select 1' language sql;
2534 4 │ select * from t where t.b > 2;
2535 ╰╴ ─ 1. source
2536 ");
2537 }
2538
2539 #[test]
2540 fn goto_function_call_style_in_order_by() {
2541 assert_snapshot!(goto("
2542create table t(a int, b int);
2543create function b(t) returns int as 'select 1' language sql;
2544select * from t order by b$0(t);
2545"), @r"
2546 ╭▸
2547 3 │ create function b(t) returns int as 'select 1' language sql;
2548 │ ─ 2. destination
2549 4 │ select * from t order by b(t);
2550 ╰╴ ─ 1. source
2551 ");
2552 }
2553
2554 #[test]
2555 fn goto_field_style_in_order_by() {
2556 assert_snapshot!(goto("
2557create table t(a int);
2558create function b(t) returns int as 'select 1' language sql;
2559select * from t order by t.b$0;
2560"), @r"
2561 ╭▸
2562 3 │ create function b(t) returns int as 'select 1' language sql;
2563 │ ─ 2. destination
2564 4 │ select * from t order by t.b;
2565 ╰╴ ─ 1. source
2566 ");
2567 }
2568
2569 #[test]
2570 fn goto_function_call_style_in_group_by() {
2571 assert_snapshot!(goto("
2572create table t(a int, b int);
2573select * from t group by a$0(t);
2574"), @r"
2575 ╭▸
2576 2 │ create table t(a int, b int);
2577 │ ─ 2. destination
2578 3 │ select * from t group by a(t);
2579 ╰╴ ─ 1. source
2580 ");
2581 }
2582
2583 #[test]
2584 fn goto_field_style_in_group_by() {
2585 assert_snapshot!(goto("
2586create table t(a int);
2587create function b(t) returns int as 'select 1' language sql;
2588select * from t group by t.b$0;
2589"), @r"
2590 ╭▸
2591 3 │ create function b(t) returns int as 'select 1' language sql;
2592 │ ─ 2. destination
2593 4 │ select * from t group by t.b;
2594 ╰╴ ─ 1. source
2595 ");
2596 }
2597
2598 #[test]
2599 fn goto_cte_table() {
2600 assert_snapshot!(goto("
2601with x as (select 1 as a)
2602select a from x$0;
2603"), @r"
2604 ╭▸
2605 2 │ with x as (select 1 as a)
2606 │ ─ 2. destination
2607 3 │ select a from x;
2608 ╰╴ ─ 1. source
2609 ");
2610 }
2611
2612 #[test]
2613 fn goto_cte_column() {
2614 assert_snapshot!(goto("
2615with x as (select 1 as a)
2616select a$0 from x;
2617"), @r"
2618 ╭▸
2619 2 │ with x as (select 1 as a)
2620 │ ─ 2. destination
2621 3 │ select a from x;
2622 ╰╴ ─ 1. source
2623 ");
2624 }
2625
2626 #[test]
2627 fn goto_cte_qualified_column_prefers_cte_over_table() {
2628 assert_snapshot!(goto("
2629create table u(id int, b int);
2630with u as (select 1 id, 2 b)
2631select u.id$0 from u;
2632"), @r"
2633 ╭▸
2634 3 │ with u as (select 1 id, 2 b)
2635 │ ── 2. destination
2636 4 │ select u.id from u;
2637 ╰╴ ─ 1. source
2638 ");
2639 }
2640
2641 #[test]
2642 fn goto_subquery_qualified_column() {
2643 assert_snapshot!(goto("
2644select t.a$0 from (select 1 a) t;
2645"), @r"
2646 ╭▸
2647 2 │ select t.a from (select 1 a) t;
2648 ╰╴ ─ 1. source ─ 2. destination
2649 ");
2650 }
2651
2652 #[test]
2653 fn goto_cte_multiple_columns() {
2654 assert_snapshot!(goto("
2655with x as (select 1 as a, 2 as b)
2656select b$0 from x;
2657"), @r"
2658 ╭▸
2659 2 │ with x as (select 1 as a, 2 as b)
2660 │ ─ 2. destination
2661 3 │ select b from x;
2662 ╰╴ ─ 1. source
2663 ");
2664 }
2665
2666 #[test]
2667 fn goto_cte_nested() {
2668 assert_snapshot!(goto("
2669with x as (select 1 as a),
2670 y as (select a from x)
2671select a$0 from y;
2672"), @r"
2673 ╭▸
2674 3 │ y as (select a from x)
2675 │ ─ 2. destination
2676 4 │ select a from y;
2677 ╰╴ ─ 1. source
2678 ");
2679 }
2680
2681 #[test]
2682 fn goto_cte_unnamed_column() {
2683 assert_snapshot!(goto(r#"
2684with x as (select 1)
2685select "?column?"$0 from x;
2686"#), @r#"
2687 ╭▸
2688 2 │ with x as (select 1)
2689 │ ─ 2. destination
2690 3 │ select "?column?" from x;
2691 ╰╴ ─ 1. source
2692 "#);
2693 }
2694
2695 #[test]
2696 fn goto_cte_star_expansion() {
2697 assert_snapshot!(goto("
2698with t as (select 1 a),
2699 y as (select * from t)
2700select a$0 from y;
2701"), @r"
2702 ╭▸
2703 2 │ with t as (select 1 a),
2704 │ ─ 2. destination
2705 3 │ y as (select * from t)
2706 4 │ select a from y;
2707 ╰╴ ─ 1. source
2708 ");
2709 }
2710
2711 #[test]
2712 fn goto_cte_qualified_star_join_column() {
2713 assert_snapshot!(goto("
2714create table u(id int, b int);
2715create table t(id int, a int);
2716
2717with k as (
2718 select u.* from t join u on a = b
2719)
2720select b$0 from k;
2721"), @r"
2722 ╭▸
2723 2 │ create table u(id int, b int);
2724 │ ─ 2. destination
2725 ‡
2726 8 │ select b from k;
2727 ╰╴ ─ 1. source
2728 ");
2729 }
2730
2731 #[test]
2732 fn goto_cte_qualified_star_join_column_with_partial_column_list() {
2733 assert_snapshot!(goto("
2734with
2735 u as (
2736 select 1 id, 2 b
2737 ),
2738 t as (
2739 select 1 id, 2 a
2740 ),
2741 k(x) as (
2742 select u.* from t join u on a = b
2743 )
2744select b$0 from k;
2745"), @r"
2746 ╭▸
2747 4 │ select 1 id, 2 b
2748 │ ─ 2. destination
2749 ‡
2750 12 │ select b from k;
2751 ╰╴ ─ 1. source
2752 ");
2753 }
2754
2755 #[test]
2756 fn goto_cte_reference_inside_cte() {
2757 assert_snapshot!(goto("
2758with t as (select 1 a),
2759 y as (select a$0 from t)
2760select a from y;
2761"), @r"
2762 ╭▸
2763 2 │ with t as (select 1 a),
2764 │ ─ 2. destination
2765 3 │ y as (select a from t)
2766 ╰╴ ─ 1. source
2767 ");
2768 }
2769
2770 #[test]
2771 fn goto_cte_with_column_list() {
2772 assert_snapshot!(goto("
2773with t(a) as (select 1)
2774select a$0 from t;
2775"), @r"
2776 ╭▸
2777 2 │ with t(a) as (select 1)
2778 │ ─ 2. destination
2779 3 │ select a from t;
2780 ╰╴ ─ 1. source
2781 ");
2782 }
2783
2784 #[test]
2785 fn goto_cte_with_partial_column_list() {
2786 assert_snapshot!(goto("
2787with t(x) as (select 1 as a, 2 as b)
2788select b$0 from t;
2789"), @r"
2790 ╭▸
2791 2 │ with t(x) as (select 1 as a, 2 as b)
2792 │ ─ 2. destination
2793 3 │ select b from t;
2794 ╰╴ ─ 1. source
2795 ");
2796 }
2797
2798 #[test]
2799 fn goto_cte_with_partial_column_list_renamed() {
2800 assert_snapshot!(goto("
2801with t(x) as (select 1 as a, 2 as b)
2802select x$0 from t;
2803"), @r"
2804 ╭▸
2805 2 │ with t(x) as (select 1 as a, 2 as b)
2806 │ ─ 2. destination
2807 3 │ select x from t;
2808 ╰╴ ─ 1. source
2809 ");
2810 }
2811
2812 #[test]
2813 fn goto_cte_insert_returning_star_column() {
2814 assert_snapshot!(goto("
2815create table t(a int, b int);
2816with inserted as (
2817 insert into t values (1, 2), (3, 4)
2818 returning *
2819)
2820select a$0 from inserted;
2821"), @r"
2822 ╭▸
2823 2 │ create table t(a int, b int);
2824 │ ─ 2. destination
2825 ‡
2826 7 │ select a from inserted;
2827 ╰╴ ─ 1. source
2828 ");
2829 }
2830
2831 #[test]
2832 fn goto_cte_delete_returning_star_column() {
2833 assert_snapshot!(goto("
2834create table t(a int, b int);
2835with deleted as (
2836 delete from t
2837 returning *
2838)
2839select a$0 from deleted;
2840"), @r"
2841 ╭▸
2842 2 │ create table t(a int, b int);
2843 │ ─ 2. destination
2844 ‡
2845 7 │ select a from deleted;
2846 ╰╴ ─ 1. source
2847 ");
2848 }
2849
2850 #[test]
2851 fn goto_cte_update_returning_star_column() {
2852 assert_snapshot!(goto("
2853create table t(a int, b int);
2854with updated as (
2855 update t set a = 42
2856 returning *
2857)
2858select a$0 from updated;
2859"), @r"
2860 ╭▸
2861 2 │ create table t(a int, b int);
2862 │ ─ 2. destination
2863 ‡
2864 7 │ select a from updated;
2865 ╰╴ ─ 1. source
2866 ");
2867 }
2868
2869 #[test]
2870 fn goto_cte_update_returning_column_list_overwrites_column() {
2871 goto_not_found(
2872 "
2873create table t(a int, b int);
2874with updated(c) as (
2875 update t set a = 10
2876 returning a
2877)
2878select a$0 from updated;
2879",
2880 );
2881 }
2882
2883 #[test]
2884 fn goto_cte_column_list_overwrites_column() {
2885 goto_not_found(
2886 "
2887with t(x) as (select 1 as a)
2888select a$0 from t;
2889",
2890 );
2891 }
2892
2893 #[test]
2894 fn goto_cte_shadows_table() {
2895 assert_snapshot!(goto("
2896create table t(a int);
2897with t as (select a$0 from t)
2898select a from t;
2899"), @r"
2900 ╭▸
2901 2 │ create table t(a int);
2902 │ ─ 2. destination
2903 3 │ with t as (select a from t)
2904 ╰╴ ─ 1. source
2905 ");
2906 }
2907
2908 #[test]
2909 fn goto_subquery_column() {
2910 assert_snapshot!(goto("
2911select a$0 from (select 1 a);
2912"), @r"
2913 ╭▸
2914 2 │ select a from (select 1 a);
2915 ╰╴ ─ 1. source ─ 2. destination
2916 ");
2917 }
2918
2919 #[test]
2920 fn goto_subquery_column_with_as() {
2921 assert_snapshot!(goto("
2922select a$0 from (select 1 as a);
2923"), @r"
2924 ╭▸
2925 2 │ select a from (select 1 as a);
2926 ╰╴ ─ 1. source ─ 2. destination
2927 ");
2928 }
2929
2930 #[test]
2931 fn goto_subquery_column_multiple_columns() {
2932 assert_snapshot!(goto("
2933select b$0 from (select 1 a, 2 b);
2934"), @r"
2935 ╭▸
2936 2 │ select b from (select 1 a, 2 b);
2937 ╰╴ ─ 1. source ─ 2. destination
2938 ");
2939 }
2940
2941 #[test]
2942 fn goto_subquery_column_nested_parens() {
2943 assert_snapshot!(goto("
2944select a$0 from ((select 1 a));
2945"), @r"
2946 ╭▸
2947 2 │ select a from ((select 1 a));
2948 ╰╴ ─ 1. source ─ 2. destination
2949 ");
2950 }
2951
2952 #[test]
2953 fn goto_subquery_column_star_table() {
2954 assert_snapshot!(goto("
2955create table foo.t(a int);
2956select a$0 from (select * from foo.t);
2957"), @r"
2958 ╭▸
2959 2 │ create table foo.t(a int);
2960 │ ─ 2. destination
2961 3 │ select a from (select * from foo.t);
2962 ╰╴ ─ 1. source
2963 ");
2964 }
2965
2966 #[test]
2967 fn goto_subquery_column_qualified_star_join() {
2968 assert_snapshot!(goto("
2969create table t(a int);
2970create table u(b int);
2971select b$0 from (select u.* from t join u on a = b);
2972"), @r"
2973 ╭▸
2974 3 │ create table u(b int);
2975 │ ─ 2. destination
2976 4 │ select b from (select u.* from t join u on a = b);
2977 ╰╴ ─ 1. source
2978 ");
2979 }
2980
2981 #[test]
2982 fn goto_subquery_column_qualified_star_join_not_found() {
2983 goto_not_found(
2984 "
2985create table t(a int);
2986create table u(b int);
2987select a$0 from (select u.* from t join u on a = b);
2988",
2989 );
2990 }
2991
2992 #[test]
2993 fn goto_insert_table() {
2994 assert_snapshot!(goto("
2995create table users(id int, email text);
2996insert into users$0(id, email) values (1, 'test@example.com');
2997"), @r"
2998 ╭▸
2999 2 │ create table users(id int, email text);
3000 │ ───── 2. destination
3001 3 │ insert into users(id, email) values (1, 'test@example.com');
3002 ╰╴ ─ 1. source
3003 ");
3004 }
3005
3006 #[test]
3007 fn goto_insert_table_with_schema() {
3008 assert_snapshot!(goto("
3009create table public.users(id int, email text);
3010insert into public.users$0(id, email) values (1, 'test@example.com');
3011"), @r"
3012 ╭▸
3013 2 │ create table public.users(id int, email text);
3014 │ ───── 2. destination
3015 3 │ insert into public.users(id, email) values (1, 'test@example.com');
3016 ╰╴ ─ 1. source
3017 ");
3018 }
3019
3020 #[test]
3021 fn goto_insert_column() {
3022 assert_snapshot!(goto("
3023create table users(id int, email text);
3024insert into users(id$0, email) values (1, 'test@example.com');
3025"), @r"
3026 ╭▸
3027 2 │ create table users(id int, email text);
3028 │ ── 2. destination
3029 3 │ insert into users(id, email) values (1, 'test@example.com');
3030 ╰╴ ─ 1. source
3031 ");
3032 }
3033
3034 #[test]
3035 fn goto_insert_column_second() {
3036 assert_snapshot!(goto("
3037create table users(id int, email text);
3038insert into users(id, email$0) values (1, 'test@example.com');
3039"), @r"
3040 ╭▸
3041 2 │ create table users(id int, email text);
3042 │ ───── 2. destination
3043 3 │ insert into users(id, email) values (1, 'test@example.com');
3044 ╰╴ ─ 1. source
3045 ");
3046 }
3047
3048 #[test]
3049 fn goto_insert_column_with_schema() {
3050 assert_snapshot!(goto("
3051create table public.users(id int, email text);
3052insert into public.users(email$0) values ('test@example.com');
3053"), @r"
3054 ╭▸
3055 2 │ create table public.users(id int, email text);
3056 │ ───── 2. destination
3057 3 │ insert into public.users(email) values ('test@example.com');
3058 ╰╴ ─ 1. source
3059 ");
3060 }
3061
3062 #[test]
3063 fn goto_insert_table_with_search_path() {
3064 assert_snapshot!(goto("
3065set search_path to foo;
3066create table foo.users(id int, email text);
3067insert into users$0(id, email) values (1, 'test@example.com');
3068"), @r"
3069 ╭▸
3070 3 │ create table foo.users(id int, email text);
3071 │ ───── 2. destination
3072 4 │ insert into users(id, email) values (1, 'test@example.com');
3073 ╰╴ ─ 1. source
3074 ");
3075 }
3076
3077 #[test]
3078 fn goto_insert_column_with_search_path() {
3079 assert_snapshot!(goto("
3080set search_path to myschema;
3081create table myschema.users(id int, email text, name text);
3082insert into users(email$0, name) values ('test@example.com', 'Test');
3083"), @r"
3084 ╭▸
3085 3 │ create table myschema.users(id int, email text, name text);
3086 │ ───── 2. destination
3087 4 │ insert into users(email, name) values ('test@example.com', 'Test');
3088 ╰╴ ─ 1. source
3089 ");
3090 }
3091
3092 #[test]
3093 fn goto_delete_table() {
3094 assert_snapshot!(goto("
3095create table users(id int, email text);
3096delete from users$0 where id = 1;
3097"), @r"
3098 ╭▸
3099 2 │ create table users(id int, email text);
3100 │ ───── 2. destination
3101 3 │ delete from users where id = 1;
3102 ╰╴ ─ 1. source
3103 ");
3104 }
3105
3106 #[test]
3107 fn goto_delete_table_with_schema() {
3108 assert_snapshot!(goto("
3109create table public.users(id int, email text);
3110delete from public.users$0 where id = 1;
3111"), @r"
3112 ╭▸
3113 2 │ create table public.users(id int, email text);
3114 │ ───── 2. destination
3115 3 │ delete from public.users where id = 1;
3116 ╰╴ ─ 1. source
3117 ");
3118 }
3119
3120 #[test]
3121 fn goto_delete_table_with_search_path() {
3122 assert_snapshot!(goto("
3123set search_path to foo;
3124create table foo.users(id int, email text);
3125delete from users$0 where id = 1;
3126"), @r"
3127 ╭▸
3128 3 │ create table foo.users(id int, email text);
3129 │ ───── 2. destination
3130 4 │ delete from users where id = 1;
3131 ╰╴ ─ 1. source
3132 ");
3133 }
3134
3135 #[test]
3136 fn goto_delete_temp_table() {
3137 assert_snapshot!(goto("
3138create temp table users(id int, email text);
3139delete from users$0 where id = 1;
3140"), @r"
3141 ╭▸
3142 2 │ create temp table users(id int, email text);
3143 │ ───── 2. destination
3144 3 │ delete from users where id = 1;
3145 ╰╴ ─ 1. source
3146 ");
3147 }
3148
3149 #[test]
3150 fn goto_delete_where_column() {
3151 assert_snapshot!(goto("
3152create table users(id int, email text);
3153delete from users where id$0 = 1;
3154"), @r"
3155 ╭▸
3156 2 │ create table users(id int, email text);
3157 │ ── 2. destination
3158 3 │ delete from users where id = 1;
3159 ╰╴ ─ 1. source
3160 ");
3161 }
3162
3163 #[test]
3164 fn goto_delete_where_column_second() {
3165 assert_snapshot!(goto("
3166create table users(id int, email text);
3167delete from users where email$0 = 'test@example.com';
3168"), @r"
3169 ╭▸
3170 2 │ create table users(id int, email text);
3171 │ ───── 2. destination
3172 3 │ delete from users where email = 'test@example.com';
3173 ╰╴ ─ 1. source
3174 ");
3175 }
3176
3177 #[test]
3178 fn goto_delete_where_column_with_schema() {
3179 assert_snapshot!(goto("
3180create table public.users(id int, email text, name text);
3181delete from public.users where name$0 = 'Test';
3182"), @r"
3183 ╭▸
3184 2 │ create table public.users(id int, email text, name text);
3185 │ ──── 2. destination
3186 3 │ delete from public.users where name = 'Test';
3187 ╰╴ ─ 1. source
3188 ");
3189 }
3190
3191 #[test]
3192 fn goto_delete_where_column_with_search_path() {
3193 assert_snapshot!(goto("
3194set search_path to myschema;
3195create table myschema.users(id int, email text, active boolean);
3196delete from users where active$0 = true;
3197"), @r"
3198 ╭▸
3199 3 │ create table myschema.users(id int, email text, active boolean);
3200 │ ────── 2. destination
3201 4 │ delete from users where active = true;
3202 ╰╴ ─ 1. source
3203 ");
3204 }
3205
3206 #[test]
3207 fn goto_delete_where_multiple_columns() {
3208 assert_snapshot!(goto("
3209create table users(id int, email text, active boolean);
3210delete from users where id$0 = 1 and active = true;
3211"), @r"
3212 ╭▸
3213 2 │ create table users(id int, email text, active boolean);
3214 │ ── 2. destination
3215 3 │ delete from users where id = 1 and active = true;
3216 ╰╴ ─ 1. source
3217 ");
3218 }
3219
3220 #[test]
3221 fn goto_delete_using_table() {
3222 assert_snapshot!(goto("
3223create table t(id int, f_id int);
3224create table f(id int, name text);
3225delete from t using f$0 where f_id = f.id and f.name = 'foo';
3226"), @r"
3227 ╭▸
3228 3 │ create table f(id int, name text);
3229 │ ─ 2. destination
3230 4 │ delete from t using f where f_id = f.id and f.name = 'foo';
3231 ╰╴ ─ 1. source
3232 ");
3233 }
3234
3235 #[test]
3236 fn goto_delete_using_table_with_schema() {
3237 assert_snapshot!(goto("
3238create table t(id int, f_id int);
3239create table public.f(id int, name text);
3240delete from t using public.f$0 where f_id = f.id;
3241"), @r"
3242 ╭▸
3243 3 │ create table public.f(id int, name text);
3244 │ ─ 2. destination
3245 4 │ delete from t using public.f where f_id = f.id;
3246 ╰╴ ─ 1. source
3247 ");
3248 }
3249
3250 #[test]
3251 fn goto_delete_using_column_in_where() {
3252 assert_snapshot!(goto("
3253create table t(id int, f_id int);
3254create table f(id int, name text);
3255delete from t using f where f_id = f.id$0 and f.name = 'foo';
3256"), @r"
3257 ╭▸
3258 2 │ create table t(id int, f_id int);
3259 │ ── 2. destination
3260 3 │ create table f(id int, name text);
3261 4 │ delete from t using f where f_id = f.id and f.name = 'foo';
3262 ╰╴ ─ 1. source
3263 ");
3264 }
3265
3266 #[test]
3267 fn goto_select_from_table() {
3268 assert_snapshot!(goto("
3269create table users(id int, email text);
3270select * from users$0;
3271"), @r"
3272 ╭▸
3273 2 │ create table users(id int, email text);
3274 │ ───── 2. destination
3275 3 │ select * from users;
3276 ╰╴ ─ 1. source
3277 ");
3278 }
3279
3280 #[test]
3281 fn goto_select_from_table_with_schema() {
3282 assert_snapshot!(goto("
3283create table public.users(id int, email text);
3284select * from public.users$0;
3285"), @r"
3286 ╭▸
3287 2 │ create table public.users(id int, email text);
3288 │ ───── 2. destination
3289 3 │ select * from public.users;
3290 ╰╴ ─ 1. source
3291 ");
3292 }
3293
3294 #[test]
3295 fn goto_select_from_table_with_search_path() {
3296 assert_snapshot!(goto("
3297set search_path to foo;
3298create table foo.users(id int, email text);
3299select * from users$0;
3300"), @r"
3301 ╭▸
3302 3 │ create table foo.users(id int, email text);
3303 │ ───── 2. destination
3304 4 │ select * from users;
3305 ╰╴ ─ 1. source
3306 ");
3307 }
3308
3309 #[test]
3310 fn goto_select_from_temp_table() {
3311 assert_snapshot!(goto("
3312create temp table users(id int, email text);
3313select * from users$0;
3314"), @r"
3315 ╭▸
3316 2 │ create temp table users(id int, email text);
3317 │ ───── 2. destination
3318 3 │ select * from users;
3319 ╰╴ ─ 1. source
3320 ");
3321 }
3322
3323 #[test]
3324 fn goto_select_from_table_defined_after() {
3325 assert_snapshot!(goto("
3326select * from users$0;
3327create table users(id int, email text);
3328"), @r"
3329 ╭▸
3330 2 │ select * from users;
3331 │ ─ 1. source
3332 3 │ create table users(id int, email text);
3333 ╰╴ ───── 2. destination
3334 ");
3335 }
3336
3337 #[test]
3338 fn goto_select_column() {
3339 assert_snapshot!(goto("
3340create table users(id int, email text);
3341select id$0 from users;
3342"), @r"
3343 ╭▸
3344 2 │ create table users(id int, email text);
3345 │ ── 2. destination
3346 3 │ select id from users;
3347 ╰╴ ─ 1. source
3348 ");
3349 }
3350
3351 #[test]
3352 fn goto_select_column_second() {
3353 assert_snapshot!(goto("
3354create table users(id int, email text);
3355select id, email$0 from users;
3356"), @r"
3357 ╭▸
3358 2 │ create table users(id int, email text);
3359 │ ───── 2. destination
3360 3 │ select id, email from users;
3361 ╰╴ ─ 1. source
3362 ");
3363 }
3364
3365 #[test]
3366 fn goto_select_column_with_schema() {
3367 assert_snapshot!(goto("
3368create table public.users(id int, email text);
3369select email$0 from public.users;
3370"), @r"
3371 ╭▸
3372 2 │ create table public.users(id int, email text);
3373 │ ───── 2. destination
3374 3 │ select email from public.users;
3375 ╰╴ ─ 1. source
3376 ");
3377 }
3378
3379 #[test]
3380 fn goto_select_column_with_search_path() {
3381 assert_snapshot!(goto("
3382set search_path to foo;
3383create table foo.users(id int, email text);
3384select id$0 from users;
3385"), @r"
3386 ╭▸
3387 3 │ create table foo.users(id int, email text);
3388 │ ── 2. destination
3389 4 │ select id from users;
3390 ╰╴ ─ 1. source
3391 ");
3392 }
3393
3394 #[test]
3395 fn goto_select_table_as_column() {
3396 assert_snapshot!(goto("
3397create table t(x bigint, y bigint);
3398select t$0 from t;
3399"), @r"
3400 ╭▸
3401 2 │ create table t(x bigint, y bigint);
3402 │ ─ 2. destination
3403 3 │ select t from t;
3404 ╰╴ ─ 1. source
3405 ");
3406 }
3407
3408 #[test]
3409 fn goto_select_table_star_expansion() {
3410 assert_snapshot!(goto("
3411create table t(id int, a int);
3412select t$0.* from t;
3413"), @r"
3414 ╭▸
3415 2 │ create table t(id int, a int);
3416 │ ─ 2. destination
3417 3 │ select t.* from t;
3418 ╰╴ ─ 1. source
3419 ");
3420 }
3421
3422 #[test]
3423 fn goto_select_table_as_column_with_schema() {
3424 assert_snapshot!(goto("
3425create table public.t(x bigint, y bigint);
3426select t$0 from public.t;
3427"), @r"
3428 ╭▸
3429 2 │ create table public.t(x bigint, y bigint);
3430 │ ─ 2. destination
3431 3 │ select t from public.t;
3432 ╰╴ ─ 1. source
3433 ");
3434 }
3435
3436 #[test]
3437 fn goto_select_table_as_column_with_search_path() {
3438 assert_snapshot!(goto("
3439set search_path to foo;
3440create table foo.users(id int, email text);
3441select users$0 from users;
3442"), @r"
3443 ╭▸
3444 3 │ create table foo.users(id int, email text);
3445 │ ───── 2. destination
3446 4 │ select users from users;
3447 ╰╴ ─ 1. source
3448 ");
3449 }
3450
3451 #[test]
3452 fn goto_select_column_with_same_name_as_table() {
3453 assert_snapshot!(goto("
3454create table t(t int);
3455select t$0 from t;
3456"), @r"
3457 ╭▸
3458 2 │ create table t(t int);
3459 │ ─ 2. destination
3460 3 │ select t from t;
3461 ╰╴ ─ 1. source
3462 ");
3463 }
3464
3465 #[test]
3466 fn goto_drop_schema() {
3467 assert_snapshot!(goto("
3468create schema foo;
3469drop schema foo$0;
3470"), @r"
3471 ╭▸
3472 2 │ create schema foo;
3473 │ ─── 2. destination
3474 3 │ drop schema foo;
3475 ╰╴ ─ 1. source
3476 ");
3477 }
3478
3479 #[test]
3480 fn goto_create_schema_authorization() {
3481 assert_snapshot!(goto("
3482create schema authorization foo$0;
3483"), @r"
3484 ╭▸
3485 2 │ create schema authorization foo;
3486 │ ┬─┬
3487 │ │ │
3488 │ │ 1. source
3489 ╰╴ 2. destination
3490 ");
3491 }
3492
3493 #[test]
3494 fn goto_drop_schema_authorization() {
3495 assert_snapshot!(goto("
3496create schema authorization foo;
3497drop schema foo$0;
3498"), @r"
3499 ╭▸
3500 2 │ create schema authorization foo;
3501 │ ─── 2. destination
3502 3 │ drop schema foo;
3503 ╰╴ ─ 1. source
3504 ");
3505 }
3506
3507 #[test]
3508 fn goto_drop_schema_defined_after() {
3509 assert_snapshot!(goto("
3510drop schema foo$0;
3511create schema foo;
3512"), @r"
3513 ╭▸
3514 2 │ drop schema foo;
3515 │ ─ 1. source
3516 3 │ create schema foo;
3517 ╰╴ ─── 2. destination
3518 ");
3519 }
3520
3521 #[test]
3522 fn goto_schema_qualifier_in_table() {
3523 assert_snapshot!(goto("
3524create schema foo;
3525create table foo$0.t(a int);
3526"), @r"
3527 ╭▸
3528 2 │ create schema foo;
3529 │ ─── 2. destination
3530 3 │ create table foo.t(a int);
3531 ╰╴ ─ 1. source
3532 ");
3533 }
3534
3535 #[test]
3536 fn goto_schema_qualifier_in_drop_table() {
3537 assert_snapshot!(goto("
3538create schema foo;
3539create table foo.t(a int);
3540drop table foo$0.t;
3541"), @r"
3542 ╭▸
3543 2 │ create schema foo;
3544 │ ─── 2. destination
3545 3 │ create table foo.t(a int);
3546 4 │ drop table foo.t;
3547 ╰╴ ─ 1. source
3548 ");
3549 }
3550
3551 #[test]
3552 fn goto_schema_qualifier_multiple_schemas() {
3553 assert_snapshot!(goto("
3554create schema foo;
3555create schema bar;
3556create table bar$0.t(a int);
3557"), @r"
3558 ╭▸
3559 3 │ create schema bar;
3560 │ ─── 2. destination
3561 4 │ create table bar.t(a int);
3562 ╰╴ ─ 1. source
3563 ");
3564 }
3565
3566 #[test]
3567 fn goto_schema_qualifier_in_function_call() {
3568 assert_snapshot!(goto(r#"
3569create schema foo;
3570create function foo.bar() returns int as $$ begin return 1; end; $$ language plpgsql;
3571select foo$0.bar();
3572"#), @r"
3573 ╭▸
3574 2 │ create schema foo;
3575 │ ─── 2. destination
3576 3 │ create function foo.bar() returns int as $$ begin return 1; end; $$ language plpgsql;
3577 4 │ select foo.bar();
3578 ╰╴ ─ 1. source
3579 ");
3580 }
3581
3582 #[test]
3583 fn goto_schema_qualifier_in_function_call_from_clause() {
3584 assert_snapshot!(goto(r#"
3585create schema myschema;
3586create function myschema.get_data() returns table(id int) as $$ begin return query select 1; end; $$ language plpgsql;
3587select * from myschema$0.get_data();
3588"#), @r"
3589 ╭▸
3590 2 │ create schema myschema;
3591 │ ──────── 2. destination
3592 3 │ create function myschema.get_data() returns table(id int) as $$ begin return query select 1; end; $$ language plpgsql;
3593 4 │ select * from myschema.get_data();
3594 ╰╴ ─ 1. source
3595 ");
3596 }
3597
3598 #[test]
3599 fn goto_schema_qualifier_in_select_from() {
3600 assert_snapshot!(goto("
3601create schema foo;
3602create table foo.t(x int);
3603select x from foo$0.t;
3604"), @r"
3605 ╭▸
3606 2 │ create schema foo;
3607 │ ─── 2. destination
3608 3 │ create table foo.t(x int);
3609 4 │ select x from foo.t;
3610 ╰╴ ─ 1. source
3611 ");
3612 }
3613
3614 #[test]
3615 fn goto_qualified_column_table() {
3616 assert_snapshot!(goto("
3617create table t(a int);
3618select t$0.a from t;
3619"), @r"
3620 ╭▸
3621 2 │ create table t(a int);
3622 │ ─ 2. destination
3623 3 │ select t.a from t;
3624 ╰╴ ─ 1. source
3625 ");
3626 }
3627
3628 #[test]
3629 fn goto_qualified_column_column() {
3630 assert_snapshot!(goto("
3631create table t(a int);
3632select t.a$0 from t;
3633"), @r"
3634 ╭▸
3635 2 │ create table t(a int);
3636 │ ─ 2. destination
3637 3 │ select t.a from t;
3638 ╰╴ ─ 1. source
3639 ");
3640 }
3641
3642 #[test]
3643 fn goto_three_part_qualified_column_schema() {
3644 assert_snapshot!(goto("
3645create schema foo;
3646create table foo.t(a int);
3647select foo$0.t.a from t;
3648"), @r"
3649 ╭▸
3650 2 │ create schema foo;
3651 │ ─── 2. destination
3652 3 │ create table foo.t(a int);
3653 4 │ select foo.t.a from t;
3654 ╰╴ ─ 1. source
3655 ");
3656 }
3657
3658 #[test]
3659 fn goto_three_part_qualified_column_table() {
3660 assert_snapshot!(goto("
3661create schema foo;
3662create table foo.t(a int);
3663select foo.t$0.a from t;
3664"), @r"
3665 ╭▸
3666 3 │ create table foo.t(a int);
3667 │ ─ 2. destination
3668 4 │ select foo.t.a from t;
3669 ╰╴ ─ 1. source
3670 ");
3671 }
3672
3673 #[test]
3674 fn goto_three_part_qualified_column_column() {
3675 assert_snapshot!(goto("
3676create schema foo;
3677create table foo.t(a int);
3678select foo.t.a$0 from t;
3679"), @r"
3680 ╭▸
3681 3 │ create table foo.t(a int);
3682 │ ─ 2. destination
3683 4 │ select foo.t.a from t;
3684 ╰╴ ─ 1. source
3685 ");
3686 }
3687
3688 #[test]
3689 fn goto_cte_values_column1() {
3690 assert_snapshot!(goto("
3691with t as (
3692 values (1, 2), (3, 4)
3693)
3694select column1$0, column2 from t;
3695"), @r"
3696 ╭▸
3697 3 │ values (1, 2), (3, 4)
3698 │ ─ 2. destination
3699 4 │ )
3700 5 │ select column1, column2 from t;
3701 ╰╴ ─ 1. source
3702 ");
3703 }
3704
3705 #[test]
3706 fn goto_cte_values_column2() {
3707 assert_snapshot!(goto("
3708with t as (
3709 values (1, 2), (3, 4)
3710)
3711select column1, column2$0 from t;
3712"), @r"
3713 ╭▸
3714 3 │ values (1, 2), (3, 4)
3715 │ ─ 2. destination
3716 4 │ )
3717 5 │ select column1, column2 from t;
3718 ╰╴ ─ 1. source
3719 ");
3720 }
3721
3722 #[test]
3723 fn goto_cte_values_single_column() {
3724 assert_snapshot!(goto("
3725with t as (
3726 values (1), (2), (3)
3727)
3728select column1$0 from t;
3729"), @r"
3730 ╭▸
3731 3 │ values (1), (2), (3)
3732 │ ─ 2. destination
3733 4 │ )
3734 5 │ select column1 from t;
3735 ╰╴ ─ 1. source
3736 ");
3737 }
3738
3739 #[test]
3740 fn goto_cte_values_multiple_rows() {
3741 assert_snapshot!(goto("
3742with t as (
3743 values
3744 (1, 2, 3),
3745 (4, 5, 6),
3746 (7, 8, 9)
3747)
3748select column3$0 from t;
3749"), @r"
3750 ╭▸
3751 4 │ (1, 2, 3),
3752 │ ─ 2. destination
3753 ‡
3754 8 │ select column3 from t;
3755 ╰╴ ─ 1. source
3756 ");
3757 }
3758
3759 #[test]
3760 fn goto_cte_values_uppercase_column_names() {
3761 assert_snapshot!(goto("
3762with t as (
3763 values (1, 2), (3, 4)
3764)
3765select COLUMN1$0, COLUMN2 from t;
3766"), @r"
3767 ╭▸
3768 3 │ values (1, 2), (3, 4)
3769 │ ─ 2. destination
3770 4 │ )
3771 5 │ select COLUMN1, COLUMN2 from t;
3772 ╰╴ ─ 1. source
3773 ");
3774 }
3775
3776 #[test]
3777 fn goto_qualified_column_with_schema_in_from_table() {
3778 assert_snapshot!(goto("
3779create table foo.t(a int, b int);
3780select t$0.a from foo.t;
3781"), @r"
3782 ╭▸
3783 2 │ create table foo.t(a int, b int);
3784 │ ─ 2. destination
3785 3 │ select t.a from foo.t;
3786 ╰╴ ─ 1. source
3787 ");
3788 }
3789
3790 #[test]
3791 fn goto_qualified_column_with_schema_in_from_column() {
3792 assert_snapshot!(goto("
3793create table foo.t(a int, b int);
3794select t.a$0 from foo.t;
3795"), @r"
3796 ╭▸
3797 2 │ create table foo.t(a int, b int);
3798 │ ─ 2. destination
3799 3 │ select t.a from foo.t;
3800 ╰╴ ─ 1. source
3801 ");
3802 }
3803
3804 #[test]
3805 fn goto_cte_union_all_column() {
3806 assert_snapshot!(goto("
3807with t as (
3808 select 1 as a, 2 as b
3809 union all
3810 select 3, 4
3811)
3812select a$0, b from t;
3813"), @r"
3814 ╭▸
3815 3 │ select 1 as a, 2 as b
3816 │ ─ 2. destination
3817 ‡
3818 7 │ select a, b from t;
3819 ╰╴ ─ 1. source
3820 ");
3821 }
3822
3823 #[test]
3824 fn goto_cte_union_all_column_second() {
3825 assert_snapshot!(goto("
3826with t as (
3827 select 1 as a, 2 as b
3828 union all
3829 select 3, 4
3830)
3831select a, b$0 from t;
3832"), @r"
3833 ╭▸
3834 3 │ select 1 as a, 2 as b
3835 │ ─ 2. destination
3836 ‡
3837 7 │ select a, b from t;
3838 ╰╴ ─ 1. source
3839 ");
3840 }
3841
3842 #[test]
3843 fn goto_cte_union_column() {
3844 assert_snapshot!(goto("
3845with t as (
3846 select 1 as a, 2 as b
3847 union
3848 select 3, 4
3849)
3850select a$0 from t;
3851"), @r"
3852 ╭▸
3853 3 │ select 1 as a, 2 as b
3854 │ ─ 2. destination
3855 ‡
3856 7 │ select a from t;
3857 ╰╴ ─ 1. source
3858 ");
3859 }
3860
3861 #[test]
3862 fn goto_cte_insert_returning_column() {
3863 assert_snapshot!(goto("
3864create table t(a int, b int);
3865with inserted as (
3866 insert into t values (1, 2), (3, 4)
3867 returning a, b
3868)
3869select a$0 from inserted;
3870"), @r"
3871 ╭▸
3872 5 │ returning a, b
3873 │ ─ 2. destination
3874 6 │ )
3875 7 │ select a from inserted;
3876 ╰╴ ─ 1. source
3877 ");
3878 }
3879
3880 #[test]
3881 fn goto_cte_insert_returning_aliased_column() {
3882 assert_snapshot!(goto("
3883create table t(a int, b int);
3884with inserted as (
3885 insert into t values (1, 2), (3, 4)
3886 returning a as x
3887)
3888select x$0 from inserted;
3889"), @r"
3890 ╭▸
3891 5 │ returning a as x
3892 │ ─ 2. destination
3893 6 │ )
3894 7 │ select x from inserted;
3895 ╰╴ ─ 1. source
3896 ");
3897 }
3898
3899 #[test]
3900 fn goto_drop_aggregate() {
3901 assert_snapshot!(goto("
3902create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3903drop aggregate myavg$0(int);
3904"), @r"
3905 ╭▸
3906 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3907 │ ───── 2. destination
3908 3 │ drop aggregate myavg(int);
3909 ╰╴ ─ 1. source
3910 ");
3911 }
3912
3913 #[test]
3914 fn goto_drop_aggregate_with_schema() {
3915 assert_snapshot!(goto("
3916set search_path to public;
3917create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3918drop aggregate public.myavg$0(int);
3919"), @r"
3920 ╭▸
3921 3 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3922 │ ───── 2. destination
3923 4 │ drop aggregate public.myavg(int);
3924 ╰╴ ─ 1. source
3925 ");
3926 }
3927
3928 #[test]
3929 fn goto_drop_aggregate_defined_after() {
3930 assert_snapshot!(goto("
3931drop aggregate myavg$0(int);
3932create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3933"), @r"
3934 ╭▸
3935 2 │ drop aggregate myavg(int);
3936 │ ─ 1. source
3937 3 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3938 ╰╴ ───── 2. destination
3939 ");
3940 }
3941
3942 #[test]
3943 fn goto_aggregate_definition_returns_self() {
3944 assert_snapshot!(goto("
3945create aggregate myavg$0(int) (sfunc = int4_avg_accum, stype = _int8);
3946"), @r"
3947 ╭▸
3948 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3949 │ ┬───┬
3950 │ │ │
3951 │ │ 1. source
3952 ╰╴ 2. destination
3953 ");
3954 }
3955
3956 #[test]
3957 fn goto_drop_aggregate_with_search_path() {
3958 assert_snapshot!(goto("
3959create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3960set search_path to bar;
3961create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3962set search_path to default;
3963drop aggregate myavg$0(int);
3964"), @r"
3965 ╭▸
3966 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
3967 │ ───── 2. destination
3968 ‡
3969 6 │ drop aggregate myavg(int);
3970 ╰╴ ─ 1. source
3971 ");
3972 }
3973
3974 #[test]
3975 fn goto_drop_aggregate_multiple() {
3976 assert_snapshot!(goto("
3977create aggregate avg1(int) (sfunc = int4_avg_accum, stype = _int8);
3978create aggregate avg2(int) (sfunc = int4_avg_accum, stype = _int8);
3979drop aggregate avg1(int), avg2$0(int);
3980"), @r"
3981 ╭▸
3982 3 │ create aggregate avg2(int) (sfunc = int4_avg_accum, stype = _int8);
3983 │ ──── 2. destination
3984 4 │ drop aggregate avg1(int), avg2(int);
3985 ╰╴ ─ 1. source
3986 ");
3987 }
3988
3989 #[test]
3990 fn goto_drop_aggregate_overloaded() {
3991 assert_snapshot!(goto("
3992create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
3993create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
3994drop aggregate sum$0(complex);
3995"), @r"
3996 ╭▸
3997 2 │ create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
3998 │ ─── 2. destination
3999 3 │ create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
4000 4 │ drop aggregate sum(complex);
4001 ╰╴ ─ 1. source
4002 ");
4003 }
4004
4005 #[test]
4006 fn goto_drop_aggregate_second_overload() {
4007 assert_snapshot!(goto("
4008create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
4009create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
4010drop aggregate sum$0(bigint);
4011"), @r"
4012 ╭▸
4013 3 │ create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
4014 │ ─── 2. destination
4015 4 │ drop aggregate sum(bigint);
4016 ╰╴ ─ 1. source
4017 ");
4018 }
4019
4020 #[test]
4021 fn goto_drop_routine_function() {
4022 assert_snapshot!(goto("
4023create function foo() returns int as $$ select 1 $$ language sql;
4024drop routine foo$0();
4025"), @r"
4026 ╭▸
4027 2 │ create function foo() returns int as $$ select 1 $$ language sql;
4028 │ ─── 2. destination
4029 3 │ drop routine foo();
4030 ╰╴ ─ 1. source
4031 ");
4032 }
4033
4034 #[test]
4035 fn goto_drop_routine_aggregate() {
4036 assert_snapshot!(goto("
4037create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
4038drop routine myavg$0(int);
4039"), @r"
4040 ╭▸
4041 2 │ create aggregate myavg(int) (sfunc = int4_avg_accum, stype = _int8);
4042 │ ───── 2. destination
4043 3 │ drop routine myavg(int);
4044 ╰╴ ─ 1. source
4045 ");
4046 }
4047
4048 #[test]
4049 fn goto_drop_routine_with_schema() {
4050 assert_snapshot!(goto("
4051set search_path to public;
4052create function foo() returns int as $$ select 1 $$ language sql;
4053drop routine public.foo$0();
4054"), @r"
4055 ╭▸
4056 3 │ create function foo() returns int as $$ select 1 $$ language sql;
4057 │ ─── 2. destination
4058 4 │ drop routine public.foo();
4059 ╰╴ ─ 1. source
4060 ");
4061 }
4062
4063 #[test]
4064 fn goto_drop_routine_defined_after() {
4065 assert_snapshot!(goto("
4066drop routine foo$0();
4067create function foo() returns int as $$ select 1 $$ language sql;
4068"), @r"
4069 ╭▸
4070 2 │ drop routine foo();
4071 │ ─ 1. source
4072 3 │ create function foo() returns int as $$ select 1 $$ language sql;
4073 ╰╴ ─── 2. destination
4074 ");
4075 }
4076
4077 #[test]
4078 fn goto_drop_routine_with_search_path() {
4079 assert_snapshot!(goto("
4080create function foo() returns int as $$ select 1 $$ language sql;
4081set search_path to bar;
4082create function foo() returns int as $$ select 1 $$ language sql;
4083set search_path to default;
4084drop routine foo$0();
4085"), @r"
4086 ╭▸
4087 2 │ create function foo() returns int as $$ select 1 $$ language sql;
4088 │ ─── 2. destination
4089 ‡
4090 6 │ drop routine foo();
4091 ╰╴ ─ 1. source
4092 ");
4093 }
4094
4095 #[test]
4096 fn goto_drop_routine_overloaded() {
4097 assert_snapshot!(goto("
4098create function add(complex) returns complex as $$ select null $$ language sql;
4099create function add(bigint) returns bigint as $$ select 1 $$ language sql;
4100drop routine add$0(complex);
4101"), @r"
4102 ╭▸
4103 2 │ create function add(complex) returns complex as $$ select null $$ language sql;
4104 │ ─── 2. destination
4105 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
4106 4 │ drop routine add(complex);
4107 ╰╴ ─ 1. source
4108 ");
4109 }
4110
4111 #[test]
4112 fn goto_drop_routine_second_overload() {
4113 assert_snapshot!(goto("
4114create function add(complex) returns complex as $$ select null $$ language sql;
4115create function add(bigint) returns bigint as $$ select 1 $$ language sql;
4116drop routine add$0(bigint);
4117"), @r"
4118 ╭▸
4119 3 │ create function add(bigint) returns bigint as $$ select 1 $$ language sql;
4120 │ ─── 2. destination
4121 4 │ drop routine add(bigint);
4122 ╰╴ ─ 1. source
4123 ");
4124 }
4125
4126 #[test]
4127 fn goto_drop_routine_aggregate_overloaded() {
4128 assert_snapshot!(goto("
4129create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
4130create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
4131drop routine sum$0(complex);
4132"), @r"
4133 ╭▸
4134 2 │ create aggregate sum(complex) (sfunc = complex_add, stype = complex, initcond = '(0,0)');
4135 │ ─── 2. destination
4136 3 │ create aggregate sum(bigint) (sfunc = bigint_add, stype = bigint, initcond = '0');
4137 4 │ drop routine sum(complex);
4138 ╰╴ ─ 1. source
4139 ");
4140 }
4141
4142 #[test]
4143 fn goto_drop_routine_multiple() {
4144 assert_snapshot!(goto("
4145create function foo() returns int as $$ select 1 $$ language sql;
4146create function bar() returns int as $$ select 1 $$ language sql;
4147drop routine foo(), bar$0();
4148"), @r"
4149 ╭▸
4150 3 │ create function bar() returns int as $$ select 1 $$ language sql;
4151 │ ─── 2. destination
4152 4 │ drop routine foo(), bar();
4153 ╰╴ ─ 1. source
4154 ");
4155 }
4156
4157 #[test]
4158 fn goto_drop_procedure() {
4159 assert_snapshot!(goto("
4160create procedure foo() language sql as $$ select 1 $$;
4161drop procedure foo$0();
4162"), @r"
4163 ╭▸
4164 2 │ create procedure foo() language sql as $$ select 1 $$;
4165 │ ─── 2. destination
4166 3 │ drop procedure foo();
4167 ╰╴ ─ 1. source
4168 ");
4169 }
4170
4171 #[test]
4172 fn goto_drop_procedure_with_schema() {
4173 assert_snapshot!(goto("
4174set search_path to public;
4175create procedure foo() language sql as $$ select 1 $$;
4176drop procedure public.foo$0();
4177"), @r"
4178 ╭▸
4179 3 │ create procedure foo() language sql as $$ select 1 $$;
4180 │ ─── 2. destination
4181 4 │ drop procedure public.foo();
4182 ╰╴ ─ 1. source
4183 ");
4184 }
4185
4186 #[test]
4187 fn goto_drop_procedure_defined_after() {
4188 assert_snapshot!(goto("
4189drop procedure foo$0();
4190create procedure foo() language sql as $$ select 1 $$;
4191"), @r"
4192 ╭▸
4193 2 │ drop procedure foo();
4194 │ ─ 1. source
4195 3 │ create procedure foo() language sql as $$ select 1 $$;
4196 ╰╴ ─── 2. destination
4197 ");
4198 }
4199
4200 #[test]
4201 fn goto_drop_procedure_with_search_path() {
4202 assert_snapshot!(goto("
4203create procedure foo() language sql as $$ select 1 $$;
4204set search_path to bar;
4205create procedure foo() language sql as $$ select 1 $$;
4206set search_path to default;
4207drop procedure foo$0();
4208"), @r"
4209 ╭▸
4210 2 │ create procedure foo() language sql as $$ select 1 $$;
4211 │ ─── 2. destination
4212 ‡
4213 6 │ drop procedure foo();
4214 ╰╴ ─ 1. source
4215 ");
4216 }
4217
4218 #[test]
4219 fn goto_drop_procedure_overloaded() {
4220 assert_snapshot!(goto("
4221create procedure add(complex) language sql as $$ select null $$;
4222create procedure add(bigint) language sql as $$ select 1 $$;
4223drop procedure add$0(complex);
4224"), @r"
4225 ╭▸
4226 2 │ create procedure add(complex) language sql as $$ select null $$;
4227 │ ─── 2. destination
4228 3 │ create procedure add(bigint) language sql as $$ select 1 $$;
4229 4 │ drop procedure add(complex);
4230 ╰╴ ─ 1. source
4231 ");
4232 }
4233
4234 #[test]
4235 fn goto_drop_procedure_second_overload() {
4236 assert_snapshot!(goto("
4237create procedure add(complex) language sql as $$ select null $$;
4238create procedure add(bigint) language sql as $$ select 1 $$;
4239drop procedure add$0(bigint);
4240"), @r"
4241 ╭▸
4242 3 │ create procedure add(bigint) language sql as $$ select 1 $$;
4243 │ ─── 2. destination
4244 4 │ drop procedure add(bigint);
4245 ╰╴ ─ 1. source
4246 ");
4247 }
4248
4249 #[test]
4250 fn goto_drop_procedure_multiple() {
4251 assert_snapshot!(goto("
4252create procedure foo() language sql as $$ select 1 $$;
4253create procedure bar() language sql as $$ select 1 $$;
4254drop procedure foo(), bar$0();
4255"), @r"
4256 ╭▸
4257 3 │ create procedure bar() language sql as $$ select 1 $$;
4258 │ ─── 2. destination
4259 4 │ drop procedure foo(), bar();
4260 ╰╴ ─ 1. source
4261 ");
4262 }
4263
4264 #[test]
4265 fn goto_procedure_definition_returns_self() {
4266 assert_snapshot!(goto("
4267create procedure foo$0() language sql as $$ select 1 $$;
4268"), @r"
4269 ╭▸
4270 2 │ create procedure foo() language sql as $$ select 1 $$;
4271 │ ┬─┬
4272 │ │ │
4273 │ │ 1. source
4274 ╰╴ 2. destination
4275 ");
4276 }
4277
4278 #[test]
4279 fn goto_call_procedure() {
4280 assert_snapshot!(goto("
4281create procedure foo() language sql as $$ select 1 $$;
4282call foo$0();
4283"), @r"
4284 ╭▸
4285 2 │ create procedure foo() language sql as $$ select 1 $$;
4286 │ ─── 2. destination
4287 3 │ call foo();
4288 ╰╴ ─ 1. source
4289 ");
4290 }
4291
4292 #[test]
4293 fn goto_call_procedure_with_schema() {
4294 assert_snapshot!(goto("
4295create procedure public.foo() language sql as $$ select 1 $$;
4296call public.foo$0();
4297"), @r"
4298 ╭▸
4299 2 │ create procedure public.foo() language sql as $$ select 1 $$;
4300 │ ─── 2. destination
4301 3 │ call public.foo();
4302 ╰╴ ─ 1. source
4303 ");
4304 }
4305
4306 #[test]
4307 fn goto_call_procedure_with_search_path() {
4308 assert_snapshot!(goto("
4309set search_path to myschema;
4310create procedure foo() language sql as $$ select 1 $$;
4311call myschema.foo$0();
4312"), @r"
4313 ╭▸
4314 3 │ create procedure foo() language sql as $$ select 1 $$;
4315 │ ─── 2. destination
4316 4 │ call myschema.foo();
4317 ╰╴ ─ 1. source
4318 ");
4319 }
4320
4321 #[test]
4322 fn goto_drop_routine_procedure() {
4323 assert_snapshot!(goto("
4324create procedure foo() language sql as $$ select 1 $$;
4325drop routine foo$0();
4326"), @r"
4327 ╭▸
4328 2 │ create procedure foo() language sql as $$ select 1 $$;
4329 │ ─── 2. destination
4330 3 │ drop routine foo();
4331 ╰╴ ─ 1. source
4332 ");
4333 }
4334
4335 #[test]
4336 fn goto_drop_routine_prefers_function_over_procedure() {
4337 assert_snapshot!(goto("
4338create function foo() returns int as $$ select 1 $$ language sql;
4339create procedure foo() language sql as $$ select 1 $$;
4340drop routine foo$0();
4341"), @r"
4342 ╭▸
4343 2 │ create function foo() returns int as $$ select 1 $$ language sql;
4344 │ ─── 2. destination
4345 3 │ create procedure foo() language sql as $$ select 1 $$;
4346 4 │ drop routine foo();
4347 ╰╴ ─ 1. source
4348 ");
4349 }
4350
4351 #[test]
4352 fn goto_drop_routine_prefers_aggregate_over_procedure() {
4353 assert_snapshot!(goto("
4354create aggregate foo(int) (sfunc = int4_avg_accum, stype = _int8);
4355create procedure foo(int) language sql as $$ select 1 $$;
4356drop routine foo$0(int);
4357"), @r"
4358 ╭▸
4359 2 │ create aggregate foo(int) (sfunc = int4_avg_accum, stype = _int8);
4360 │ ─── 2. destination
4361 3 │ create procedure foo(int) language sql as $$ select 1 $$;
4362 4 │ drop routine foo(int);
4363 ╰╴ ─ 1. source
4364 ");
4365 }
4366
4367 #[test]
4368 fn goto_table_alias_in_qualified_column() {
4369 assert_snapshot!(goto("
4370create table t(a int8, b text);
4371select f$0.a from t as f;
4372"), @r"
4373 ╭▸
4374 3 │ select f.a from t as f;
4375 ╰╴ ─ 1. source ─ 2. destination
4376 ");
4377 }
4378
4379 #[test]
4380 fn goto_column_through_table_alias() {
4381 assert_snapshot!(goto("
4382create table t(a int8, b text);
4383select f.a$0 from t as f;
4384"), @r"
4385 ╭▸
4386 2 │ create table t(a int8, b text);
4387 │ ─ 2. destination
4388 3 │ select f.a from t as f;
4389 ╰╴ ─ 1. source
4390 ");
4391 }
4392
4393 #[test]
4394 fn goto_cte_alias_renamed_column() {
4395 assert_snapshot!(goto("
4396with t as (select 1 a, 2 b)
4397select f.x$0 from t as f(x);
4398"), @r"
4399 ╭▸
4400 3 │ select f.x from t as f(x);
4401 ╰╴ ─ 1. source ─ 2. destination
4402 ");
4403 }
4404
4405 #[test]
4406 fn goto_cte_alias_unrenamed_column() {
4407 assert_snapshot!(goto("
4408with t as (select 1 a, 2 b)
4409select f.b$0 from t as f(x);
4410"), @r"
4411 ╭▸
4412 2 │ with t as (select 1 a, 2 b)
4413 │ ─ 2. destination
4414 3 │ select f.b from t as f(x);
4415 ╰╴ ─ 1. source
4416 ");
4417 }
4418
4419 #[test]
4420 fn goto_join_table() {
4421 assert_snapshot!(goto("
4422create table users(id int, email text);
4423create table messages(id int, user_id int, message text);
4424select * from users join messages$0 on users.id = messages.user_id;
4425"), @r"
4426 ╭▸
4427 3 │ create table messages(id int, user_id int, message text);
4428 │ ──────── 2. destination
4429 4 │ select * from users join messages on users.id = messages.user_id;
4430 ╰╴ ─ 1. source
4431 ");
4432 }
4433
4434 #[test]
4435 fn goto_join_qualified_column_from_joined_table() {
4436 assert_snapshot!(goto("
4437create table users(id int, email text);
4438create table messages(id int, user_id int, message text);
4439select messages.user_id$0 from users join messages on users.id = messages.user_id;
4440"), @r"
4441 ╭▸
4442 3 │ create table messages(id int, user_id int, message text);
4443 │ ─────── 2. destination
4444 4 │ select messages.user_id from users join messages on users.id = messages.user_id;
4445 ╰╴ ─ 1. source
4446 ");
4447 }
4448
4449 #[test]
4450 fn goto_join_qualified_column_from_base_table() {
4451 assert_snapshot!(goto("
4452create table users(id int, email text);
4453create table messages(id int, user_id int, message text);
4454select users.id$0 from users join messages on users.id = messages.user_id;
4455"), @r"
4456 ╭▸
4457 2 │ create table users(id int, email text);
4458 │ ── 2. destination
4459 3 │ create table messages(id int, user_id int, message text);
4460 4 │ select users.id from users join messages on users.id = messages.user_id;
4461 ╰╴ ─ 1. source
4462 ");
4463 }
4464
4465 #[test]
4466 fn goto_join_multiple_joins() {
4467 assert_snapshot!(goto("
4468create table users(id int, name text);
4469create table messages(id int, user_id int, message text);
4470create table comments(id int, message_id int, text text);
4471select comments.text$0 from users
4472 join messages on users.id = messages.user_id
4473 join comments on messages.id = comments.message_id;
4474"), @r"
4475 ╭▸
4476 4 │ create table comments(id int, message_id int, text text);
4477 │ ──── 2. destination
4478 5 │ select comments.text from users
4479 ╰╴ ─ 1. source
4480 ");
4481 }
4482
4483 #[test]
4484 fn goto_join_with_aliases() {
4485 assert_snapshot!(goto("
4486create table users(id int, name text);
4487create table messages(id int, user_id int, message text);
4488select m.message$0 from users as u join messages as m on u.id = m.user_id;
4489"), @r"
4490 ╭▸
4491 3 │ create table messages(id int, user_id int, message text);
4492 │ ─────── 2. destination
4493 4 │ select m.message from users as u join messages as m on u.id = m.user_id;
4494 ╰╴ ─ 1. source
4495 ");
4496 }
4497
4498 #[test]
4499 fn goto_join_unqualified_column() {
4500 assert_snapshot!(goto("
4501create table users(id int, email text);
4502create table messages(id int, user_id int, message text);
4503select message$0 from users join messages on users.id = messages.user_id;
4504"), @r"
4505 ╭▸
4506 3 │ create table messages(id int, user_id int, message text);
4507 │ ─────── 2. destination
4508 4 │ select message from users join messages on users.id = messages.user_id;
4509 ╰╴ ─ 1. source
4510 ");
4511 }
4512
4513 #[test]
4514 fn goto_join_with_many_tables() {
4515 assert_snapshot!(goto("
4516create table users(id int, email text);
4517create table messages(id int, user_id int, message text);
4518create table logins(id int, user_id int, at timestamptz);
4519create table posts(id int, user_id int, post text);
4520
4521select post$0
4522 from users
4523 join messages
4524 on users.id = messages.user_id
4525 join logins
4526 on users.id = logins.user_id
4527 join posts
4528 on users.id = posts.user_id
4529"), @r"
4530 ╭▸
4531 5 │ create table posts(id int, user_id int, post text);
4532 │ ──── 2. destination
4533 6 │
4534 7 │ select post
4535 ╰╴ ─ 1. source
4536 ");
4537 }
4538
4539 #[test]
4540 fn goto_join_with_schema() {
4541 assert_snapshot!(goto("
4542create schema foo;
4543create table foo.users(id int, email text);
4544create table foo.messages(id int, user_id int, message text);
4545select foo.messages.message$0 from foo.users join foo.messages on foo.users.id = foo.messages.user_id;
4546"), @r"
4547 ╭▸
4548 4 │ create table foo.messages(id int, user_id int, message text);
4549 │ ─────── 2. destination
4550 5 │ select foo.messages.message from foo.users join foo.messages on foo.users.id = foo.messages.user_id;
4551 ╰╴ ─ 1. source
4552 ");
4553 }
4554
4555 #[test]
4556 fn goto_join_left_join() {
4557 assert_snapshot!(goto("
4558create table users(id int, email text);
4559create table messages(id int, user_id int, message text);
4560select messages.message$0 from users left join messages on users.id = messages.user_id;
4561"), @r"
4562 ╭▸
4563 3 │ create table messages(id int, user_id int, message text);
4564 │ ─────── 2. destination
4565 4 │ select messages.message from users left join messages on users.id = messages.user_id;
4566 ╰╴ ─ 1. source
4567 ");
4568 }
4569
4570 #[test]
4571 fn goto_join_on_table_qualifier() {
4572 assert_snapshot!(goto("
4573create table t(a int);
4574create table u(a int);
4575select * from t join u on u$0.a = t.a;
4576"), @r"
4577 ╭▸
4578 3 │ create table u(a int);
4579 │ ─ 2. destination
4580 4 │ select * from t join u on u.a = t.a;
4581 ╰╴ ─ 1. source
4582 ");
4583 }
4584
4585 #[test]
4586 fn goto_join_on_column() {
4587 assert_snapshot!(goto("
4588create table t(a int);
4589create table u(a int);
4590select * from t join u on u.a$0 = t.a;
4591"), @r"
4592 ╭▸
4593 3 │ create table u(a int);
4594 │ ─ 2. destination
4595 4 │ select * from t join u on u.a = t.a;
4596 ╰╴ ─ 1. source
4597 ");
4598 }
4599
4600 #[test]
4601 fn goto_join_using_column() {
4602 assert_snapshot!(goto("
4603create table t(a int);
4604create table u(a int);
4605select * from t join u using (a$0);
4606"), @r"
4607 ╭▸
4608 2 │ create table t(a int);
4609 │ ─ 2. destination
4610 3 │ create table u(a int);
4611 │ ─ 3. destination
4612 4 │ select * from t join u using (a);
4613 ╰╴ ─ 1. source
4614 ");
4615 }
4616
4617 #[test]
4618 fn goto_insert_select_cte_column() {
4619 assert_snapshot!(goto("
4620create table users(id int, email text);
4621with new_data as (
4622 select 1 as id, 'test@example.com' as email
4623)
4624insert into users (id, email)
4625select id$0, email from new_data;
4626"), @r"
4627 ╭▸
4628 4 │ select 1 as id, 'test@example.com' as email
4629 │ ── 2. destination
4630 ‡
4631 7 │ select id, email from new_data;
4632 ╰╴ ─ 1. source
4633 ");
4634 }
4635
4636 #[test]
4637 fn goto_insert_select_cte_column_second() {
4638 assert_snapshot!(goto("
4639create table users(id int, email text);
4640with new_data as (
4641 select 1 as id, 'test@example.com' as email
4642)
4643insert into users (id, email)
4644select id, email$0 from new_data;
4645"), @r"
4646 ╭▸
4647 4 │ select 1 as id, 'test@example.com' as email
4648 │ ───── 2. destination
4649 ‡
4650 7 │ select id, email from new_data;
4651 ╰╴ ─ 1. source
4652 ");
4653 }
4654
4655 #[test]
4656 fn goto_insert_select_cte_table() {
4657 assert_snapshot!(goto("
4658create table users(id int, email text);
4659with new_data as (
4660 select 1 as id, 'test@example.com' as email
4661)
4662insert into users (id, email)
4663select id, email from new_data$0;
4664"), @r"
4665 ╭▸
4666 3 │ with new_data as (
4667 │ ──────── 2. destination
4668 ‡
4669 7 │ select id, email from new_data;
4670 ╰╴ ─ 1. source
4671 ");
4672 }
4673
4674 #[test]
4675 fn goto_delete_cte_column() {
4676 assert_snapshot!(goto("
4677create table users(id int, email text);
4678with old_data as (
4679 select 1 as id
4680)
4681delete from users where id in (select id$0 from old_data);
4682"), @r"
4683 ╭▸
4684 4 │ select 1 as id
4685 │ ── 2. destination
4686 5 │ )
4687 6 │ delete from users where id in (select id from old_data);
4688 ╰╴ ─ 1. source
4689 ");
4690 }
4691
4692 #[test]
4693 fn goto_update_table() {
4694 assert_snapshot!(goto("
4695create table users(id int, email text);
4696update users$0 set email = 'new@example.com';
4697"), @r"
4698 ╭▸
4699 2 │ create table users(id int, email text);
4700 │ ───── 2. destination
4701 3 │ update users set email = 'new@example.com';
4702 ╰╴ ─ 1. source
4703 ");
4704 }
4705
4706 #[test]
4707 fn goto_update_table_with_schema() {
4708 assert_snapshot!(goto("
4709create table public.users(id int, email text);
4710update public.users$0 set email = 'new@example.com';
4711"), @r"
4712 ╭▸
4713 2 │ create table public.users(id int, email text);
4714 │ ───── 2. destination
4715 3 │ update public.users set email = 'new@example.com';
4716 ╰╴ ─ 1. source
4717 ");
4718 }
4719
4720 #[test]
4721 fn goto_update_table_with_search_path() {
4722 assert_snapshot!(goto("
4723set search_path to foo;
4724create table foo.users(id int, email text);
4725update users$0 set email = 'new@example.com';
4726"), @r"
4727 ╭▸
4728 3 │ create table foo.users(id int, email text);
4729 │ ───── 2. destination
4730 4 │ update users set email = 'new@example.com';
4731 ╰╴ ─ 1. source
4732 ");
4733 }
4734
4735 #[test]
4736 fn goto_update_where_column() {
4737 assert_snapshot!(goto("
4738create table users(id int, email text);
4739update users set email = 'new@example.com' where id$0 = 1;
4740"), @r"
4741 ╭▸
4742 2 │ create table users(id int, email text);
4743 │ ── 2. destination
4744 3 │ update users set email = 'new@example.com' where id = 1;
4745 ╰╴ ─ 1. source
4746 ");
4747 }
4748
4749 #[test]
4750 fn goto_update_where_column_with_schema() {
4751 assert_snapshot!(goto("
4752create table public.users(id int, email text);
4753update public.users set email = 'new@example.com' where id$0 = 1;
4754"), @r"
4755 ╭▸
4756 2 │ create table public.users(id int, email text);
4757 │ ── 2. destination
4758 3 │ update public.users set email = 'new@example.com' where id = 1;
4759 ╰╴ ─ 1. source
4760 ");
4761 }
4762
4763 #[test]
4764 fn goto_update_where_column_with_search_path() {
4765 assert_snapshot!(goto("
4766set search_path to foo;
4767create table foo.users(id int, email text);
4768update users set email = 'new@example.com' where id$0 = 1;
4769"), @r"
4770 ╭▸
4771 3 │ create table foo.users(id int, email text);
4772 │ ── 2. destination
4773 4 │ update users set email = 'new@example.com' where id = 1;
4774 ╰╴ ─ 1. source
4775 ");
4776 }
4777
4778 #[test]
4779 fn goto_update_set_column() {
4780 assert_snapshot!(goto("
4781create table users(id int, email text);
4782update users set email$0 = 'new@example.com' where id = 1;
4783"), @r"
4784 ╭▸
4785 2 │ create table users(id int, email text);
4786 │ ───── 2. destination
4787 3 │ update users set email = 'new@example.com' where id = 1;
4788 ╰╴ ─ 1. source
4789 ");
4790 }
4791
4792 #[test]
4793 fn goto_update_set_column_with_schema() {
4794 assert_snapshot!(goto("
4795create table public.users(id int, email text);
4796update public.users set email$0 = 'new@example.com' where id = 1;
4797"), @r"
4798 ╭▸
4799 2 │ create table public.users(id int, email text);
4800 │ ───── 2. destination
4801 3 │ update public.users set email = 'new@example.com' where id = 1;
4802 ╰╴ ─ 1. source
4803 ");
4804 }
4805
4806 #[test]
4807 fn goto_update_set_column_with_search_path() {
4808 assert_snapshot!(goto("
4809set search_path to foo;
4810create table foo.users(id int, email text);
4811update users set email$0 = 'new@example.com' where id = 1;
4812"), @r"
4813 ╭▸
4814 3 │ create table foo.users(id int, email text);
4815 │ ───── 2. destination
4816 4 │ update users set email = 'new@example.com' where id = 1;
4817 ╰╴ ─ 1. source
4818 ");
4819 }
4820
4821 #[test]
4822 fn goto_update_from_table() {
4823 assert_snapshot!(goto("
4824create table users(id int, email text);
4825create table messages(id int, user_id int, email text);
4826update users set email = messages.email from messages$0 where users.id = messages.user_id;
4827"), @r"
4828 ╭▸
4829 3 │ create table messages(id int, user_id int, email text);
4830 │ ──────── 2. destination
4831 4 │ update users set email = messages.email from messages where users.id = messages.user_id;
4832 ╰╴ ─ 1. source
4833 ");
4834 }
4835
4836 #[test]
4837 fn goto_update_from_table_with_schema() {
4838 assert_snapshot!(goto("
4839create table users(id int, email text);
4840create table public.messages(id int, user_id int, email text);
4841update users set email = messages.email from public.messages$0 where users.id = messages.user_id;
4842"), @r"
4843 ╭▸
4844 3 │ create table public.messages(id int, user_id int, email text);
4845 │ ──────── 2. destination
4846 4 │ update users set email = messages.email from public.messages where users.id = messages.user_id;
4847 ╰╴ ─ 1. source
4848 ");
4849 }
4850
4851 #[test]
4852 fn goto_update_from_table_with_search_path() {
4853 assert_snapshot!(goto("
4854set search_path to foo;
4855create table users(id int, email text);
4856create table foo.messages(id int, user_id int, email text);
4857update users set email = messages.email from messages$0 where users.id = messages.user_id;
4858"), @r"
4859 ╭▸
4860 4 │ create table foo.messages(id int, user_id int, email text);
4861 │ ──────── 2. destination
4862 5 │ update users set email = messages.email from messages where users.id = messages.user_id;
4863 ╰╴ ─ 1. source
4864 ");
4865 }
4866
4867 #[test]
4868 fn goto_update_with_cte_table() {
4869 assert_snapshot!(goto("
4870create table users(id int, email text);
4871with new_data as (
4872 select 1 as id, 'new@example.com' as email
4873)
4874update users set email = new_data.email from new_data$0 where users.id = new_data.id;
4875"), @r"
4876 ╭▸
4877 3 │ with new_data as (
4878 │ ──────── 2. destination
4879 ‡
4880 6 │ update users set email = new_data.email from new_data where users.id = new_data.id;
4881 ╰╴ ─ 1. source
4882 ");
4883 }
4884
4885 #[test]
4886 fn goto_update_with_cte_column_in_set() {
4887 assert_snapshot!(goto("
4888create table users(id int, email text);
4889with new_data as (
4890 select 1 as id, 'new@example.com' as email
4891)
4892update users set email = new_data.email$0 from new_data where users.id = new_data.id;
4893"), @r"
4894 ╭▸
4895 4 │ select 1 as id, 'new@example.com' as email
4896 │ ───── 2. destination
4897 5 │ )
4898 6 │ update users set email = new_data.email from new_data where users.id = new_data.id;
4899 ╰╴ ─ 1. source
4900 ");
4901 }
4902
4903 #[test]
4904 fn goto_update_with_cte_column_in_where() {
4905 assert_snapshot!(goto("
4906create table users(id int, email text);
4907with new_data as (
4908 select 1 as id, 'new@example.com' as email
4909)
4910update users set email = new_data.email from new_data where new_data.id$0 = users.id;
4911"), @r"
4912 ╭▸
4913 4 │ select 1 as id, 'new@example.com' as email
4914 │ ── 2. destination
4915 5 │ )
4916 6 │ update users set email = new_data.email from new_data where new_data.id = users.id;
4917 ╰╴ ─ 1. source
4918 ");
4919 }
4920
4921 #[test]
4922 fn goto_update_with_cte_values() {
4923 assert_snapshot!(goto("
4924create table users(id int, email text);
4925with new_data as (
4926 values (1, 'new@example.com')
4927)
4928update users set email = new_data.column2$0 from new_data where users.id = new_data.column1;
4929"), @r"
4930 ╭▸
4931 4 │ values (1, 'new@example.com')
4932 │ ───────────────── 2. destination
4933 5 │ )
4934 6 │ update users set email = new_data.column2 from new_data where users.id = new_data.column1;
4935 ╰╴ ─ 1. source
4936 ");
4937 }
4938
4939 #[test]
4940 fn goto_truncate_table() {
4941 assert_snapshot!(goto("
4942create table t();
4943truncate table t$0;
4944"), @r"
4945 ╭▸
4946 2 │ create table t();
4947 │ ─ 2. destination
4948 3 │ truncate table t;
4949 ╰╴ ─ 1. source
4950 ");
4951 }
4952
4953 #[test]
4954 fn goto_truncate_table_without_table_keyword() {
4955 assert_snapshot!(goto("
4956create table t();
4957truncate t$0;
4958"), @r"
4959 ╭▸
4960 2 │ create table t();
4961 │ ─ 2. destination
4962 3 │ truncate t;
4963 ╰╴ ─ 1. source
4964 ");
4965 }
4966
4967 #[test]
4968 fn goto_truncate_multiple_tables() {
4969 assert_snapshot!(goto("
4970create table t1();
4971create table t2();
4972truncate t1, t2$0;
4973"), @r"
4974 ╭▸
4975 3 │ create table t2();
4976 │ ── 2. destination
4977 4 │ truncate t1, t2;
4978 ╰╴ ─ 1. source
4979 ");
4980 }
4981
4982 #[test]
4983 fn goto_lock_table() {
4984 assert_snapshot!(goto("
4985create table t();
4986lock table t$0;
4987"), @r"
4988 ╭▸
4989 2 │ create table t();
4990 │ ─ 2. destination
4991 3 │ lock table t;
4992 ╰╴ ─ 1. source
4993 ");
4994 }
4995
4996 #[test]
4997 fn goto_lock_table_without_table_keyword() {
4998 assert_snapshot!(goto("
4999create table t();
5000lock t$0;
5001"), @r"
5002 ╭▸
5003 2 │ create table t();
5004 │ ─ 2. destination
5005 3 │ lock t;
5006 ╰╴ ─ 1. source
5007 ");
5008 }
5009
5010 #[test]
5011 fn goto_lock_multiple_tables() {
5012 assert_snapshot!(goto("
5013create table t1();
5014create table t2();
5015lock t1, t2$0;
5016"), @r"
5017 ╭▸
5018 3 │ create table t2();
5019 │ ── 2. destination
5020 4 │ lock t1, t2;
5021 ╰╴ ─ 1. source
5022 ");
5023 }
5024
5025 #[test]
5026 fn goto_vacuum_table() {
5027 assert_snapshot!(goto("
5028create table users(id int, email text);
5029vacuum users$0;
5030"), @r"
5031 ╭▸
5032 2 │ create table users(id int, email text);
5033 │ ───── 2. destination
5034 3 │ vacuum users;
5035 ╰╴ ─ 1. source
5036 ");
5037 }
5038
5039 #[test]
5040 fn goto_vacuum_multiple_tables() {
5041 assert_snapshot!(goto("
5042create table t1();
5043create table t2();
5044vacuum t1, t2$0;
5045"), @r"
5046 ╭▸
5047 3 │ create table t2();
5048 │ ── 2. destination
5049 4 │ vacuum t1, t2;
5050 ╰╴ ─ 1. source
5051 ");
5052 }
5053
5054 #[test]
5055 fn goto_alter_table() {
5056 assert_snapshot!(goto("
5057create table users(id int, email text);
5058alter table users$0 alter email set not null;
5059"), @r"
5060 ╭▸
5061 2 │ create table users(id int, email text);
5062 │ ───── 2. destination
5063 3 │ alter table users alter email set not null;
5064 ╰╴ ─ 1. source
5065 ");
5066 }
5067
5068 #[test]
5069 fn goto_alter_table_column() {
5070 assert_snapshot!(goto("
5071create table users(id int, email text);
5072alter table users alter email$0 set not null;
5073"), @r"
5074 ╭▸
5075 2 │ create table users(id int, email text);
5076 │ ───── 2. destination
5077 3 │ alter table users alter email set not null;
5078 ╰╴ ─ 1. source
5079 ");
5080 }
5081
5082 #[test]
5083 fn goto_alter_table_column_with_column_keyword() {
5084 assert_snapshot!(goto("
5085create table users(id int, email text);
5086alter table users alter column email$0 set not null;
5087"), @r"
5088 ╭▸
5089 2 │ create table users(id int, email text);
5090 │ ───── 2. destination
5091 3 │ alter table users alter column email set not null;
5092 ╰╴ ─ 1. source
5093 ");
5094 }
5095
5096 #[test]
5097 fn goto_alter_table_add_column() {
5098 assert_snapshot!(goto("
5099create table users(id int);
5100alter table users$0 add column email text;
5101"), @r"
5102 ╭▸
5103 2 │ create table users(id int);
5104 │ ───── 2. destination
5105 3 │ alter table users add column email text;
5106 ╰╴ ─ 1. source
5107 ");
5108 }
5109
5110 #[test]
5111 fn goto_alter_table_drop_column() {
5112 assert_snapshot!(goto("
5113create table users(id int, email text);
5114alter table users drop column email$0;
5115"), @r"
5116 ╭▸
5117 2 │ create table users(id int, email text);
5118 │ ───── 2. destination
5119 3 │ alter table users drop column email;
5120 ╰╴ ─ 1. source
5121 ");
5122 }
5123
5124 #[test]
5125 fn goto_alter_table_drop_column_table_name() {
5126 assert_snapshot!(goto("
5127create table users(id int, email text);
5128alter table users$0 drop column email;
5129"), @r"
5130 ╭▸
5131 2 │ create table users(id int, email text);
5132 │ ───── 2. destination
5133 3 │ alter table users drop column email;
5134 ╰╴ ─ 1. source
5135 ");
5136 }
5137
5138 #[test]
5139 fn goto_refresh_materialized_view() {
5140 assert_snapshot!(goto("
5141create materialized view mv as select 1;
5142refresh materialized view mv$0;
5143"), @r"
5144 ╭▸
5145 2 │ create materialized view mv as select 1;
5146 │ ── 2. destination
5147 3 │ refresh materialized view mv;
5148 ╰╴ ─ 1. source
5149 ");
5150 }
5151
5152 #[test]
5153 fn goto_refresh_materialized_view_concurrently() {
5154 assert_snapshot!(goto("
5155create materialized view mv as select 1;
5156refresh materialized view concurrently mv$0;
5157"), @r"
5158 ╭▸
5159 2 │ create materialized view mv as select 1;
5160 │ ── 2. destination
5161 3 │ refresh materialized view concurrently mv;
5162 ╰╴ ─ 1. source
5163 ");
5164 }
5165
5166 #[test]
5167 fn goto_reindex_table() {
5168 assert_snapshot!(goto("
5169create table users(id int);
5170reindex table users$0;
5171"), @r"
5172 ╭▸
5173 2 │ create table users(id int);
5174 │ ───── 2. destination
5175 3 │ reindex table users;
5176 ╰╴ ─ 1. source
5177 ");
5178 }
5179
5180 #[test]
5181 fn goto_reindex_index() {
5182 assert_snapshot!(goto("
5183create table t(c int);
5184create index idx on t(c);
5185reindex index idx$0;
5186"), @r"
5187 ╭▸
5188 3 │ create index idx on t(c);
5189 │ ─── 2. destination
5190 4 │ reindex index idx;
5191 ╰╴ ─ 1. source
5192 ");
5193 }
5194
5195 #[test]
5196 fn goto_select_exists_column() {
5197 assert_snapshot!(goto("
5198select exists$0 from (
5199 select exists(select 1)
5200);
5201"), @r"
5202 ╭▸
5203 2 │ select exists from (
5204 │ ─ 1. source
5205 3 │ select exists(select 1)
5206 ╰╴ ──────────────── 2. destination
5207 ");
5208 }
5209
5210 #[test]
5211 fn goto_reindex_schema() {
5212 assert_snapshot!(goto("
5213create schema app;
5214reindex schema app$0;
5215"), @r"
5216 ╭▸
5217 2 │ create schema app;
5218 │ ─── 2. destination
5219 3 │ reindex schema app;
5220 ╰╴ ─ 1. source
5221 ");
5222 }
5223
5224 #[test]
5225 fn goto_reindex_database() {
5226 assert_snapshot!(goto("
5227create database appdb;
5228reindex database appdb$0;
5229"), @r"
5230 ╭▸
5231 2 │ create database appdb;
5232 │ ───── 2. destination
5233 3 │ reindex database appdb;
5234 ╰╴ ─ 1. source
5235 ");
5236 }
5237
5238 #[test]
5239 fn goto_reindex_system() {
5240 assert_snapshot!(goto("
5241create database systemdb;
5242reindex system systemdb$0;
5243"), @r"
5244 ╭▸
5245 2 │ create database systemdb;
5246 │ ──────── 2. destination
5247 3 │ reindex system systemdb;
5248 ╰╴ ─ 1. source
5249 ");
5250 }
5251
5252 #[test]
5253 fn goto_merge_returning_aliased_column() {
5254 assert_snapshot!(goto(
5255 "
5256create table t(a int, b int);
5257with u(x, y) as (
5258 select 1, 2
5259),
5260merged as (
5261 merge into t
5262 using u
5263 on t.a = u.x
5264 when matched then
5265 do nothing
5266 when not matched then
5267 do nothing
5268 returning a as x, b as y
5269)
5270select x$0 from merged;
5271",
5272 ), @r"
5273 ╭▸
5274 14 │ returning a as x, b as y
5275 │ ─ 2. destination
5276 15 │ )
5277 16 │ select x from merged;
5278 ╰╴ ─ 1. source
5279 ");
5280 }
5281
5282 #[test]
5283 fn goto_cte_update_returning_column() {
5284 assert_snapshot!(goto("
5285create table t(a int, b int);
5286with updated(c) as (
5287 update t set a = 10
5288 returning a, b
5289)
5290select c, b$0 from updated;"
5291 ), @r"
5292 ╭▸
5293 5 │ returning a, b
5294 │ ─ 2. destination
5295 6 │ )
5296 7 │ select c, b from updated;
5297 ╰╴ ─ 1. source
5298 ");
5299 }
5300
5301 #[test]
5302 fn goto_update_returning_column_to_table_def() {
5303 assert_snapshot!(goto("
5304create table t(a int, b int);
5305with updated(c) as (
5306 update t set a = 10
5307 returning a, b$0
5308)
5309select c, b from updated;"
5310 ), @r"
5311 ╭▸
5312 2 │ create table t(a int, b int);
5313 │ ─ 2. destination
5314 ‡
5315 5 │ returning a, b
5316 ╰╴ ─ 1. source
5317 ");
5318 }
5319
5320 #[test]
5321 fn goto_insert_returning_column_to_table_def() {
5322 assert_snapshot!(goto("
5323create table t(a int, b int);
5324with inserted as (
5325 insert into t values (1, 2)
5326 returning a$0, b
5327)
5328select a, b from inserted;"
5329 ), @r"
5330 ╭▸
5331 2 │ create table t(a int, b int);
5332 │ ─ 2. destination
5333 ‡
5334 5 │ returning a, b
5335 ╰╴ ─ 1. source
5336 ");
5337 }
5338
5339 #[test]
5340 fn goto_delete_returning_column_to_table_def() {
5341 assert_snapshot!(goto("
5342create table t(a int, b int);
5343with deleted as (
5344 delete from t
5345 returning a, b$0
5346)
5347select a, b from deleted;"
5348 ), @r"
5349 ╭▸
5350 2 │ create table t(a int, b int);
5351 │ ─ 2. destination
5352 ‡
5353 5 │ returning a, b
5354 ╰╴ ─ 1. source
5355 ");
5356 }
5357
5358 #[test]
5359 fn goto_update_returning_qualified_star_table() {
5360 assert_snapshot!(goto("
5361create table t(a int, b int);
5362update t set a = 10
5363returning t$0.*;"
5364 ), @r"
5365 ╭▸
5366 2 │ create table t(a int, b int);
5367 │ ─ 2. destination
5368 3 │ update t set a = 10
5369 4 │ returning t.*;
5370 ╰╴ ─ 1. source
5371 ");
5372 }
5373
5374 #[test]
5375 fn goto_insert_returning_qualified_star_table() {
5376 assert_snapshot!(goto("
5377create table t(a int, b int);
5378insert into t values (1, 2)
5379returning t$0.*;"
5380 ), @r"
5381 ╭▸
5382 2 │ create table t(a int, b int);
5383 │ ─ 2. destination
5384 3 │ insert into t values (1, 2)
5385 4 │ returning t.*;
5386 ╰╴ ─ 1. source
5387 ");
5388 }
5389
5390 #[test]
5391 fn goto_delete_returning_qualified_star_table() {
5392 assert_snapshot!(goto("
5393create table t(a int, b int);
5394delete from t
5395returning t$0.*;"
5396 ), @r"
5397 ╭▸
5398 2 │ create table t(a int, b int);
5399 │ ─ 2. destination
5400 3 │ delete from t
5401 4 │ returning t.*;
5402 ╰╴ ─ 1. source
5403 ");
5404 }
5405
5406 #[test]
5407 fn goto_update_alias_in_set_clause() {
5408 assert_snapshot!(goto("
5409create table t(a int, b int);
5410update t as f set f$0.a = 10;"
5411 ), @r"
5412 ╭▸
5413 3 │ update t as f set f.a = 10;
5414 │ ┬ ─ 1. source
5415 │ │
5416 ╰╴ 2. destination
5417 ");
5418 }
5419
5420 #[test]
5421 fn goto_update_alias_in_where_clause() {
5422 assert_snapshot!(goto("
5423create table t(a int, b int);
5424update t as f set a = 10 where f$0.b = 5;"
5425 ), @r"
5426 ╭▸
5427 3 │ update t as f set a = 10 where f.b = 5;
5428 ╰╴ ─ 2. destination ─ 1. source
5429 ");
5430 }
5431
5432 #[test]
5433 fn goto_update_alias_in_from_clause() {
5434 assert_snapshot!(goto("
5435create table t(a int, b int);
5436create table u(c int);
5437update t as f set a = 10 from u where f$0.b = u.c;"
5438 ), @r"
5439 ╭▸
5440 4 │ update t as f set a = 10 from u where f.b = u.c;
5441 ╰╴ ─ 2. destination ─ 1. source
5442 ");
5443 }
5444
5445 #[test]
5446 fn goto_insert_alias_in_on_conflict() {
5447 assert_snapshot!(goto("
5448create table t(a int primary key, b int);
5449insert into t as f values (1, 2) on conflict (f$0.a) do nothing;"
5450 ), @r"
5451 ╭▸
5452 3 │ insert into t as f values (1, 2) on conflict (f.a) do nothing;
5453 ╰╴ ─ 2. destination ─ 1. source
5454 ");
5455 }
5456
5457 #[test]
5458 fn goto_insert_alias_in_returning() {
5459 assert_snapshot!(goto("
5460create table t(a int, b int);
5461insert into t as f values (1, 2) returning f$0.a;"
5462 ), @r"
5463 ╭▸
5464 3 │ insert into t as f values (1, 2) returning f.a;
5465 ╰╴ ─ 2. destination ─ 1. source
5466 ");
5467 }
5468
5469 #[test]
5470 fn goto_insert_alias_returning_column() {
5471 assert_snapshot!(goto("
5472create table t(a int, b int);
5473insert into t as f values (1, 2) returning f.a$0;"
5474 ), @r"
5475 ╭▸
5476 2 │ create table t(a int, b int);
5477 │ ─ 2. destination
5478 3 │ insert into t as f values (1, 2) returning f.a;
5479 ╰╴ ─ 1. source
5480 ");
5481 }
5482
5483 #[test]
5484 fn goto_delete_from_alias() {
5485 assert_snapshot!(goto("
5486create table t(a int, b int);
5487delete from t as f where f$0.a = 10;"
5488 ), @r"
5489 ╭▸
5490 3 │ delete from t as f where f.a = 10;
5491 │ ┬ ─ 1. source
5492 │ │
5493 ╰╴ 2. destination
5494 ");
5495 }
5496
5497 #[test]
5498 fn goto_delete_from_alias_column() {
5499 assert_snapshot!(goto("
5500create table t(a int, b int);
5501delete from t as f where f.a$0 = 10;"
5502 ), @r"
5503 ╭▸
5504 2 │ create table t(a int, b int);
5505 │ ─ 2. destination
5506 3 │ delete from t as f where f.a = 10;
5507 ╰╴ ─ 1. source
5508 ");
5509 }
5510
5511 #[test]
5512 fn goto_delete_from_alias_returning() {
5513 assert_snapshot!(goto("
5514create table t(a int, b int);
5515delete from t as f returning f$0.a"
5516 ), @r"
5517 ╭▸
5518 3 │ delete from t as f returning f.a
5519 │ ┬ ─ 1. source
5520 │ │
5521 ╰╴ 2. destination
5522 ");
5523
5524 assert_snapshot!(goto("
5525create table t(a int, b int);
5526delete from t as f returning f.a$0"
5527 ), @r"
5528 ╭▸
5529 2 │ create table t(a int, b int);
5530 │ ─ 2. destination
5531 3 │ delete from t as f returning f.a
5532 ╰╴ ─ 1. source
5533 ");
5534 }
5535
5536 #[test]
5537 fn goto_merge_alias_on_table() {
5538 assert_snapshot!(goto("
5539create table t(a int, b int);
5540create table u(a int, b int);
5541merge into t as f
5542 using u on u.a = f$0.a
5543 when matched then do nothing;
5544"
5545
5546 ), @r"
5547 ╭▸
5548 4 │ merge into t as f
5549 │ ─ 2. destination
5550 5 │ using u on u.a = f.a
5551 ╰╴ ─ 1. source
5552 ");
5553 }
5554
5555 #[test]
5556 fn goto_merge_alias_on_column() {
5557 assert_snapshot!(goto("
5558create table t(a int, b int);
5559create table u(a int, b int);
5560merge into t as f
5561 using u on u.a = f.a$0
5562 when matched then do nothing;
5563"
5564
5565 ), @r"
5566 ╭▸
5567 2 │ create table t(a int, b int);
5568 │ ─ 2. destination
5569 ‡
5570 5 │ using u on u.a = f.a
5571 ╰╴ ─ 1. source
5572 ");
5573 }
5574
5575 #[test]
5576 fn goto_merge_alias_returning() {
5577 assert_snapshot!(goto("
5578create table t(a int, b int);
5579create table u(a int, b int);
5580merge into t as f
5581 using u on u.a = f.a
5582 when matched then do nothing
5583 returning f$0.a;
5584"
5585
5586 ), @r"
5587 ╭▸
5588 4 │ merge into t as f
5589 │ ─ 2. destination
5590 ‡
5591 7 │ returning f.a;
5592 ╰╴ ─ 1. source
5593 ");
5594
5595 assert_snapshot!(goto("
5596create table t(a int, b int);
5597create table u(a int, b int);
5598merge into t as f
5599 using u on u.a = f.a
5600 when matched then do nothing
5601 returning f.a$0;
5602"
5603 ), @r"
5604 ╭▸
5605 2 │ create table t(a int, b int);
5606 │ ─ 2. destination
5607 ‡
5608 7 │ returning f.a;
5609 ╰╴ ─ 1. source
5610 ");
5611 }
5612
5613 #[test]
5614 fn goto_merge_using_table_in_when_clause() {
5615 assert_snapshot!(goto("
5616create table t(a int, b int);
5617create table u(a int, b int);
5618merge into t
5619 using u on true
5620 when matched and u$0.a = t.a
5621 then do nothing;
5622"
5623 ), @r"
5624 ╭▸
5625 3 │ create table u(a int, b int);
5626 │ ─ 2. destination
5627 ‡
5628 6 │ when matched and u.a = t.a
5629 ╰╴ ─ 1. source
5630 ");
5631 }
5632
5633 #[test]
5634 fn goto_merge_using_table_column_in_when_clause() {
5635 assert_snapshot!(goto("
5636create table t(a int, b int);
5637create table u(a int, b int);
5638merge into t
5639 using u on true
5640 when matched and u.a$0 = t.a
5641 then do nothing;
5642"
5643 ), @r"
5644 ╭▸
5645 3 │ create table u(a int, b int);
5646 │ ─ 2. destination
5647 ‡
5648 6 │ when matched and u.a = t.a
5649 ╰╴ ─ 1. source
5650 ");
5651 }
5652
5653 #[test]
5654 fn goto_merge_unqualified_column_target_table() {
5655 assert_snapshot!(goto("
5656create table x(a int, b int);
5657create table y(c int, d int);
5658merge into x
5659 using y
5660 on true
5661 when matched and a$0 = c
5662 then do nothing;
5663"
5664 ), @r"
5665 ╭▸
5666 2 │ create table x(a int, b int);
5667 │ ─ 2. destination
5668 ‡
5669 7 │ when matched and a = c
5670 ╰╴ ─ 1. source
5671 ");
5672 }
5673
5674 #[test]
5675 fn goto_merge_unqualified_column_source_table() {
5676 assert_snapshot!(goto("
5677create table x(a int, b int);
5678create table y(c int, d int);
5679merge into x
5680 using y
5681 on true
5682 when matched and a = c$0
5683 then do nothing;
5684"
5685 ), @r"
5686 ╭▸
5687 3 │ create table y(c int, d int);
5688 │ ─ 2. destination
5689 ‡
5690 7 │ when matched and a = c
5691 ╰╴ ─ 1. source
5692 ");
5693 }
5694
5695 #[test]
5696 fn goto_merge_into_table() {
5697 assert_snapshot!(goto("
5698create table x(a int, b int);
5699create table y(c int, d int);
5700merge into x$0
5701 using y
5702 on true
5703 when matched and a = c
5704 then do nothing;
5705"
5706 ), @r"
5707 ╭▸
5708 2 │ create table x(a int, b int);
5709 │ ─ 2. destination
5710 3 │ create table y(c int, d int);
5711 4 │ merge into x
5712 ╰╴ ─ 1. source
5713 ");
5714 }
5715
5716 #[test]
5717 fn goto_merge_using_clause_table() {
5718 assert_snapshot!(goto("
5719create table x(a int, b int);
5720create table y(c int, d int);
5721merge into x
5722 using y$0
5723 on true
5724 when matched and a = c
5725 then do nothing;
5726"
5727 ), @r"
5728 ╭▸
5729 3 │ create table y(c int, d int);
5730 │ ─ 2. destination
5731 4 │ merge into x
5732 5 │ using y
5733 ╰╴ ─ 1. source
5734 ");
5735 }
5736
5737 #[test]
5738 fn goto_merge_using_clause_alias() {
5739 assert_snapshot!(goto("
5740create table x(a int, b int);
5741create table y(c int, d int);
5742merge into x as g
5743 using y as k
5744 on true
5745 when matched and a = k.c$0
5746 then do nothing;
5747"
5748 ), @r"
5749 ╭▸
5750 3 │ create table y(c int, d int);
5751 │ ─ 2. destination
5752 ‡
5753 7 │ when matched and a = k.c
5754 ╰╴ ─ 1. source
5755 ");
5756 }
5757
5758 #[test]
5759 fn goto_merge_on_clause_unqualified_source_column() {
5760 assert_snapshot!(goto("
5761create table x(a int, b int);
5762create table y(c int, d int);
5763merge into x as g
5764 using y as k
5765 on g.a = c$0 and a = c
5766 when matched and g.a = k.c
5767 then do nothing;
5768"
5769 ), @r"
5770 ╭▸
5771 3 │ create table y(c int, d int);
5772 │ ─ 2. destination
5773 ‡
5774 6 │ on g.a = c and a = c
5775 ╰╴ ─ 1. source
5776 ");
5777 }
5778
5779 #[test]
5780 fn goto_merge_returning_old_table() {
5781 assert_snapshot!(goto("
5782create table x(a int, b int);
5783create table y(c int, d int);
5784merge into x as g
5785 using y as k
5786 on g.a = c and a = k.c
5787 when matched and g.a = k.c
5788 then do nothing
5789 returning old$0.a, new.a;
5790"
5791 ), @r"
5792 ╭▸
5793 2 │ create table x(a int, b int);
5794 │ ─ 2. destination
5795 ‡
5796 9 │ returning old.a, new.a;
5797 ╰╴ ─ 1. source
5798 ");
5799 }
5800
5801 #[test]
5802 fn goto_merge_returning_old_column() {
5803 assert_snapshot!(goto("
5804create table x(a int, b int);
5805create table y(c int, d int);
5806merge into x as g
5807 using y as k
5808 on g.a = c and a = k.c
5809 when matched and g.a = k.c
5810 then do nothing
5811 returning old.a$0, new.a;
5812"
5813 ), @r"
5814 ╭▸
5815 2 │ create table x(a int, b int);
5816 │ ─ 2. destination
5817 ‡
5818 9 │ returning old.a, new.a;
5819 ╰╴ ─ 1. source
5820 ");
5821 }
5822
5823 #[test]
5824 fn goto_merge_returning_new_table() {
5825 assert_snapshot!(goto("
5826create table x(a int, b int);
5827create table y(c int, d int);
5828merge into x as g
5829 using y as k
5830 on g.a = c and a = k.c
5831 when matched and g.a = k.c
5832 then do nothing
5833 returning old.a, new$0.a;
5834"
5835 ), @r"
5836 ╭▸
5837 2 │ create table x(a int, b int);
5838 │ ─ 2. destination
5839 ‡
5840 9 │ returning old.a, new.a;
5841 ╰╴ ─ 1. source
5842 ");
5843 }
5844
5845 #[test]
5846 fn goto_merge_returning_new_column() {
5847 assert_snapshot!(goto("
5848create table x(a int, b int);
5849create table y(c int, d int);
5850merge into x as g
5851 using y as k
5852 on g.a = c and a = k.c
5853 when matched and g.a = k.c
5854 then do nothing
5855 returning old.a, new.a$0;
5856"
5857 ), @r"
5858 ╭▸
5859 2 │ create table x(a int, b int);
5860 │ ─ 2. destination
5861 ‡
5862 9 │ returning old.a, new.a;
5863 ╰╴ ─ 1. source
5864 ");
5865 }
5866
5867 #[test]
5868 fn goto_merge_with_tables_named_old_new_old_table() {
5869 assert_snapshot!(goto("
5870create table old(a int, b int);
5871create table new(c int, d int);
5872merge into old
5873 using new
5874 on true
5875 when matched
5876 then do nothing
5877 returning old$0.a, new.d;
5878"
5879 ), @r"
5880 ╭▸
5881 2 │ create table old(a int, b int);
5882 │ ─── 2. destination
5883 ‡
5884 9 │ returning old.a, new.d;
5885 ╰╴ ─ 1. source
5886 ");
5887 }
5888
5889 #[test]
5890 fn goto_merge_with_tables_named_old_new_old_column() {
5891 assert_snapshot!(goto("
5892create table old(a int, b int);
5893create table new(c int, d int);
5894merge into old
5895 using new
5896 on true
5897 when matched
5898 then do nothing
5899 returning old.a$0, new.d;
5900"
5901 ), @r"
5902 ╭▸
5903 2 │ create table old(a int, b int);
5904 │ ─ 2. destination
5905 ‡
5906 9 │ returning old.a, new.d;
5907 ╰╴ ─ 1. source
5908 ");
5909 }
5910
5911 #[test]
5912 fn goto_merge_with_tables_named_old_new_new_table() {
5913 assert_snapshot!(goto("
5914create table old(a int, b int);
5915create table new(c int, d int);
5916merge into old
5917 using new
5918 on true
5919 when matched
5920 then do nothing
5921 returning old.a, new$0.d;
5922"
5923 ), @r"
5924 ╭▸
5925 3 │ create table new(c int, d int);
5926 │ ─── 2. destination
5927 ‡
5928 9 │ returning old.a, new.d;
5929 ╰╴ ─ 1. source
5930 ");
5931 }
5932
5933 #[test]
5934 fn goto_merge_with_tables_named_old_new_new_column() {
5935 assert_snapshot!(goto("
5936create table old(a int, b int);
5937create table new(c int, d int);
5938merge into old
5939 using new
5940 on true
5941 when matched
5942 then do nothing
5943 returning old.a, new.d$0;
5944"
5945 ), @r"
5946 ╭▸
5947 3 │ create table new(c int, d int);
5948 │ ─ 2. destination
5949 ‡
5950 9 │ returning old.a, new.d;
5951 ╰╴ ─ 1. source
5952 ");
5953 }
5954
5955 #[test]
5956 fn goto_merge_returning_with_aliases_before_table() {
5957 assert_snapshot!(goto("
5958create table x(a int, b int);
5959create table y(c int, d int);
5960merge into x
5961 using y on true
5962 when matched then do nothing
5963 returning
5964 with (old as before, new as after)
5965 before$0.a, after.a;
5966"
5967 ), @r"
5968 ╭▸
5969 8 │ with (old as before, new as after)
5970 │ ────── 2. destination
5971 9 │ before.a, after.a;
5972 ╰╴ ─ 1. source
5973 ");
5974 }
5975
5976 #[test]
5977 fn goto_merge_returning_with_aliases_before_column() {
5978 assert_snapshot!(goto("
5979create table x(a int, b int);
5980create table y(c int, d int);
5981merge into x
5982 using y on true
5983 when matched then do nothing
5984 returning
5985 with (old as before, new as after)
5986 before.a$0, after.a;
5987"
5988 ), @r"
5989 ╭▸
5990 2 │ create table x(a int, b int);
5991 │ ─ 2. destination
5992 ‡
5993 9 │ before.a, after.a;
5994 ╰╴ ─ 1. source
5995 ");
5996 }
5997
5998 #[test]
5999 fn goto_merge_returning_with_aliases_after_table() {
6000 assert_snapshot!(goto("
6001create table x(a int, b int);
6002create table y(c int, d int);
6003merge into x
6004 using y on true
6005 when matched then do nothing
6006 returning
6007 with (old as before, new as after)
6008 before.a, after$0.a;
6009"
6010 ), @r"
6011 ╭▸
6012 8 │ with (old as before, new as after)
6013 │ ───── 2. destination
6014 9 │ before.a, after.a;
6015 ╰╴ ─ 1. source
6016 ");
6017 }
6018
6019 #[test]
6020 fn goto_merge_returning_with_aliases_after_column() {
6021 assert_snapshot!(goto("
6022create table x(a int, b int);
6023create table y(c int, d int);
6024merge into x
6025 using y on true
6026 when matched then do nothing
6027 returning
6028 with (old as before, new as after)
6029 before.a, after.a$0;
6030"
6031 ), @r"
6032 ╭▸
6033 2 │ create table x(a int, b int);
6034 │ ─ 2. destination
6035 ‡
6036 9 │ before.a, after.a;
6037 ╰╴ ─ 1. source
6038 ");
6039 }
6040}