Skip to content

Commit 352b4d6

Browse files
ellemoutonyyforyongyu
authored andcommitted
graph/db: freeze sql migration queries
1 parent 5db82a4 commit 352b4d6

File tree

11 files changed

+4141
-17
lines changed

11 files changed

+4141
-17
lines changed

config_builder.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import (
3737
"github.com/lightningnetwork/lnd/funding"
3838
graphdb "github.com/lightningnetwork/lnd/graph/db"
3939
graphdbmig1 "github.com/lightningnetwork/lnd/graph/db/migration1"
40+
graphmig1sqlc "github.com/lightningnetwork/lnd/graph/db/migration1/sqlc"
4041
"github.com/lightningnetwork/lnd/htlcswitch"
4142
"github.com/lightningnetwork/lnd/invoices"
4243
"github.com/lightningnetwork/lnd/keychain"
@@ -1141,7 +1142,8 @@ func (d *DefaultDatabaseBuilder) BuildDatabase(
11411142
QueryCfg: queryCfg,
11421143
}
11431144
err := graphdbmig1.MigrateGraphToSQL(
1144-
ctx, cfg, dbs.ChanStateDB.Backend, tx,
1145+
ctx, cfg, dbs.ChanStateDB.Backend,
1146+
graphmig1sqlc.New(tx.GetTx()),
11451147
)
11461148
if err != nil {
11471149
return fmt.Errorf("failed to migrate "+

graph/db/migration1/sql_migration.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ import (
1414

1515
"github.com/btcsuite/btcd/chaincfg/chainhash"
1616
"github.com/lightningnetwork/lnd/graph/db/migration1/models"
17+
"github.com/lightningnetwork/lnd/graph/db/migration1/sqlc"
1718
"github.com/lightningnetwork/lnd/kvdb"
1819
"github.com/lightningnetwork/lnd/lnwire"
1920
"github.com/lightningnetwork/lnd/routing/route"
2021
"github.com/lightningnetwork/lnd/sqldb"
21-
"github.com/lightningnetwork/lnd/sqldb/sqlc"
2222
"golang.org/x/time/rate"
2323
)
2424

graph/db/migration1/sql_store.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ import (
1717
"github.com/btcsuite/btcd/wire"
1818
"github.com/lightningnetwork/lnd/fn/v2"
1919
"github.com/lightningnetwork/lnd/graph/db/migration1/models"
20+
"github.com/lightningnetwork/lnd/graph/db/migration1/sqlc"
2021
"github.com/lightningnetwork/lnd/lnwire"
2122
"github.com/lightningnetwork/lnd/routing/route"
2223
"github.com/lightningnetwork/lnd/sqldb"
23-
"github.com/lightningnetwork/lnd/sqldb/sqlc"
2424
"github.com/lightningnetwork/lnd/tlv"
2525
"github.com/lightningnetwork/lnd/tor"
2626
)

graph/db/migration1/sqlc/db.go

Lines changed: 31 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
1+
package sqlc
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// makeQueryParams generates a string of query parameters for a SQL query. It is
9+
// meant to replace the `?` placeholders in a SQL query with numbered parameters
10+
// like `$1`, `$2`, etc. This is required for the sqlc /*SLICE:<field_name>*/
11+
// workaround. See scripts/gen_sqlc_docker.sh for more details.
12+
func makeQueryParams(numTotalArgs, numListArgs int) string {
13+
if numListArgs == 0 {
14+
return ""
15+
}
16+
17+
var b strings.Builder
18+
19+
// Pre-allocate a rough estimation of the buffer size to avoid
20+
// re-allocations. A parameter like $1000, takes 6 bytes.
21+
b.Grow(numListArgs * 6)
22+
23+
diff := numTotalArgs - numListArgs
24+
for i := 0; i < numListArgs; i++ {
25+
if i > 0 {
26+
// We don't need to check the error here because the
27+
// WriteString method of strings.Builder always returns
28+
// nil.
29+
_, _ = b.WriteString(",")
30+
}
31+
32+
// We don't need to check the error here because the
33+
// Write method (called by fmt.Fprintf) of strings.Builder
34+
// always returns nil.
35+
_, _ = fmt.Fprintf(&b, "$%d", i+diff+1)
36+
}
37+
38+
return b.String()
39+
}
40+
41+
// ChannelAndNodes is an interface that provides access to a channel and its
42+
// two nodes.
43+
type ChannelAndNodes interface {
44+
// Channel returns the GraphChannel associated with this interface.
45+
Channel() GraphChannel
46+
47+
// Node1 returns the first GraphNode associated with this channel.
48+
Node1() GraphNode
49+
50+
// Node2 returns the second GraphNode associated with this channel.
51+
Node2() GraphNode
52+
}
53+
54+
// Channel returns the GraphChannel associated with this interface.
55+
//
56+
// NOTE: This method is part of the ChannelAndNodes interface.
57+
func (r GetChannelsByPolicyLastUpdateRangeRow) Channel() GraphChannel {
58+
return r.GraphChannel
59+
}
60+
61+
// Node1 returns the first GraphNode associated with this channel.
62+
//
63+
// NOTE: This method is part of the ChannelAndNodes interface.
64+
func (r GetChannelsByPolicyLastUpdateRangeRow) Node1() GraphNode {
65+
return r.GraphNode
66+
}
67+
68+
// Node2 returns the second GraphNode associated with this channel.
69+
//
70+
// NOTE: This method is part of the ChannelAndNodes interface.
71+
func (r GetChannelsByPolicyLastUpdateRangeRow) Node2() GraphNode {
72+
return r.GraphNode_2
73+
}
74+
75+
// ChannelAndNodeIDs is an interface that provides access to a channel and its
76+
// two node public keys.
77+
type ChannelAndNodeIDs interface {
78+
// Channel returns the GraphChannel associated with this interface.
79+
Channel() GraphChannel
80+
81+
// Node1Pub returns the public key of the first node as a byte slice.
82+
Node1Pub() []byte
83+
84+
// Node2Pub returns the public key of the second node as a byte slice.
85+
Node2Pub() []byte
86+
}
87+
88+
// Channel returns the GraphChannel associated with this interface.
89+
//
90+
// NOTE: This method is part of the ChannelAndNodeIDs interface.
91+
func (r GetChannelsBySCIDWithPoliciesRow) Channel() GraphChannel {
92+
return r.GraphChannel
93+
}
94+
95+
// Node1Pub returns the public key of the first node as a byte slice.
96+
//
97+
// NOTE: This method is part of the ChannelAndNodeIDs interface.
98+
func (r GetChannelsBySCIDWithPoliciesRow) Node1Pub() []byte {
99+
return r.GraphNode.PubKey
100+
}
101+
102+
// Node2Pub returns the public key of the second node as a byte slice.
103+
//
104+
// NOTE: This method is part of the ChannelAndNodeIDs interface.
105+
func (r GetChannelsBySCIDWithPoliciesRow) Node2Pub() []byte {
106+
return r.GraphNode_2.PubKey
107+
}
108+
109+
// Node1 returns the first GraphNode associated with this channel.
110+
//
111+
// NOTE: This method is part of the ChannelAndNodes interface.
112+
func (r GetChannelsBySCIDWithPoliciesRow) Node1() GraphNode {
113+
return r.GraphNode
114+
}
115+
116+
// Node2 returns the second GraphNode associated with this channel.
117+
//
118+
// NOTE: This method is part of the ChannelAndNodes interface.
119+
func (r GetChannelsBySCIDWithPoliciesRow) Node2() GraphNode {
120+
return r.GraphNode_2
121+
}
122+
123+
// Channel returns the GraphChannel associated with this interface.
124+
//
125+
// NOTE: This method is part of the ChannelAndNodeIDs interface.
126+
func (r GetChannelsByOutpointsRow) Channel() GraphChannel {
127+
return r.GraphChannel
128+
}
129+
130+
// Node1Pub returns the public key of the first node as a byte slice.
131+
//
132+
// NOTE: This method is part of the ChannelAndNodeIDs interface.
133+
func (r GetChannelsByOutpointsRow) Node1Pub() []byte {
134+
return r.Node1Pubkey
135+
}
136+
137+
// Node2Pub returns the public key of the second node as a byte slice.
138+
//
139+
// NOTE: This method is part of the ChannelAndNodeIDs interface.
140+
func (r GetChannelsByOutpointsRow) Node2Pub() []byte {
141+
return r.Node2Pubkey
142+
}
143+
144+
// Channel returns the GraphChannel associated with this interface.
145+
//
146+
// NOTE: This method is part of the ChannelAndNodeIDs interface.
147+
func (r GetChannelsBySCIDRangeRow) Channel() GraphChannel {
148+
return r.GraphChannel
149+
}
150+
151+
// Node1Pub returns the public key of the first node as a byte slice.
152+
//
153+
// NOTE: This method is part of the ChannelAndNodeIDs interface.
154+
func (r GetChannelsBySCIDRangeRow) Node1Pub() []byte {
155+
return r.Node1PubKey
156+
}
157+
158+
// Node2Pub returns the public key of the second node as a byte slice.
159+
//
160+
// NOTE: This method is part of the ChannelAndNodeIDs interface.
161+
func (r GetChannelsBySCIDRangeRow) Node2Pub() []byte {
162+
return r.Node2PubKey
163+
}

0 commit comments

Comments
 (0)