(ctx *sql.Context)
| 27 | var subscriptionMap = sync.Map{} |
| 28 | |
| 29 | func UpdateSubscriptions(ctx *sql.Context) error { |
| 30 | rows, err := adapter.QueryCatalog(ctx, catalog.InternalTables.PgSubscription.SelectAllStmt()) |
| 31 | if err != nil { |
| 32 | return err |
| 33 | } |
| 34 | defer rows.Close() |
| 35 | |
| 36 | var subMap = make(map[string]*Subscription) |
| 37 | for rows.Next() { |
| 38 | var name, conn, pub, lsn string |
| 39 | var enabled bool |
| 40 | if err := rows.Scan(&name, &conn, &pub, &lsn, &enabled); err != nil { |
| 41 | return err |
| 42 | } |
| 43 | subMap[name] = &Subscription{ |
| 44 | Subscription: name, |
| 45 | Conn: conn, |
| 46 | Publication: pub, |
| 47 | LsnStr: lsn, |
| 48 | Enabled: enabled, |
| 49 | Replicator: nil, |
| 50 | } |
| 51 | } |
| 52 | |
| 53 | if err = rows.Err(); err != nil { |
| 54 | return err |
| 55 | } |
| 56 | |
| 57 | for tempName, tempSub := range subMap { |
| 58 | if _, loaded := subscriptionMap.LoadOrStore(tempName, tempSub); !loaded { |
| 59 | replicator, err := NewLogicalReplicator(tempName, tempSub.Conn) |
| 60 | if err != nil { |
| 61 | return fmt.Errorf("failed to create logical replicator: %v", err) |
| 62 | } |
| 63 | |
| 64 | if sub, ok := subscriptionMap.Load(tempName); ok { |
| 65 | if subscription, ok := sub.(*Subscription); ok { |
| 66 | subscription.Replicator = replicator |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | err = replicator.CreateReplicationSlotIfNotExists(tempSub.Publication) |
| 71 | if err != nil { |
| 72 | return fmt.Errorf("failed to create replication slot: %v", err) |
| 73 | } |
| 74 | if tempSub.Enabled { |
| 75 | go replicator.StartReplication(ctx, tempSub.Publication) |
| 76 | } |
| 77 | } else { |
| 78 | if sub, ok := subscriptionMap.Load(tempName); ok { |
| 79 | if subscription, ok := sub.(*Subscription); ok { |
| 80 | if tempSub.Enabled != subscription.Enabled { |
| 81 | subscription.Enabled = tempSub.Enabled |
| 82 | if subscription.Enabled { |
| 83 | go subscription.Replicator.StartReplication(ctx, subscription.Publication) |
| 84 | } else { |
| 85 | subscription.Replicator.Stop() |
| 86 | } |
no test coverage detected