runReplicationScript runs the script given on the postgres connection provided
( ctx context.Context, t *testing.T, script ReplicationTest, server *pgserver.Server, replicaConn *pgx.Conn, primaryDns string, )
| 736 | |
| 737 | // runReplicationScript runs the script given on the postgres connection provided |
| 738 | func runReplicationScript( |
| 739 | ctx context.Context, |
| 740 | t *testing.T, |
| 741 | script ReplicationTest, |
| 742 | server *pgserver.Server, |
| 743 | replicaConn *pgx.Conn, |
| 744 | primaryDns string, |
| 745 | ) { |
| 746 | r := newReplicator(server.NewInternalCtx(), t, primaryDns) |
| 747 | defer r.Stop() |
| 748 | |
| 749 | if script.Skip { |
| 750 | t.Skip("Skip has been set in the script") |
| 751 | } |
| 752 | |
| 753 | connections := map[string]*pgx.Conn{ |
| 754 | "replica": replicaConn, |
| 755 | } |
| 756 | |
| 757 | defer func() { |
| 758 | for _, conn := range connections { |
| 759 | if conn != nil { |
| 760 | conn.Close(ctx) |
| 761 | } |
| 762 | } |
| 763 | }() |
| 764 | |
| 765 | // Run the setup |
| 766 | for _, query := range script.SetUpScript { |
| 767 | // handle logic for special pseudo-queries |
| 768 | if handlePseudoQuery(t, server, query, r) { |
| 769 | continue |
| 770 | } |
| 771 | |
| 772 | conn := connectionForQuery(t, query, connections, primaryDns) |
| 773 | |
| 774 | target, _ := clientSpecFromQueryComment(query) |
| 775 | if target == "replica" && configuration.IsReplicationWithoutIndex() { |
| 776 | // Remove the primary key index from the replica table |
| 777 | query = strings.Replace(query, " primary key,", ",", 1) |
| 778 | } |
| 779 | |
| 780 | log.Println("Running setup query:", query) |
| 781 | _, err := conn.Exec(ctx, query) |
| 782 | require.NoError(t, err) |
| 783 | } |
| 784 | |
| 785 | // Run the assertions |
| 786 | for _, assertion := range script.Assertions { |
| 787 | t.Run(assertion.Query, func(t *testing.T) { |
| 788 | if assertion.Skip { |
| 789 | t.Skip("Skip has been set in the assertion") |
| 790 | } |
| 791 | |
| 792 | // handle logic for special pseudo-queries |
| 793 | if handlePseudoQuery(t, server, assertion.Query, r) { |
| 794 | return |
| 795 | } |
no test coverage detected