@@ -18,9 +18,8 @@ import (
1818// are visited in sorted order so that later tables reference earlier tables.
1919//
2020// The visiting is controlled by two callbacks:
21- // - visitPreFn should tell the visitor whether the FKs of the given tables
22- // should be explored (i.e. whether the visitor should "recurse" into FK
23- // reference tables of the given one).
21+ // - recurseFn should tell the visitor whether it should recurse into FK
22+ // reference tables of the given one.
2423// - visitFn allows the caller to do any work on the table being visited.
2524//
2625// In both functions:
@@ -35,39 +34,39 @@ func VisitFKReferenceTables(
3534 ctx context.Context ,
3635 catalog cat.Catalog ,
3736 tables []TableMeta ,
38- visitPreFn func (_ cat.Table , fk cat.ForeignKeyConstraint ) ( exploreFKs bool ) ,
37+ recurseFn func (_ cat.Table , fk cat.ForeignKeyConstraint ) bool ,
3938 visitFn func (_ cat.Table , fk cat.ForeignKeyConstraint ),
4039) {
41- // tableExplored tracks which tables we've already explored FKs of . Once a
42- // table is explored , it is considered "fully processed" and we effectively
43- // ignore it from now on. If a table has already been visited but is not
44- // explored , we still might want to explore it later (because we might get
45- // to it via a different FK that requires exploration ).
46- var tableExplored intsets.Fast
40+ // tableRecursed tracks which tables we've already recursed into . Once a
41+ // table is recursed into , it is considered "fully processed" and we
42+ // effectively ignore it from now on. If a table has already been visited
43+ // but is not recursed into , we still might want to recurse into it later
44+ // (because we might get to it via a different FK that requires recursion ).
45+ var tableRecursed intsets.Fast
4746 var visitForeignKeyReferencedTables func (tab cat.Table )
4847 var visitForeignKeyReferencingTables func (tab cat.Table )
49- visitTable := func (table cat.Table , fk cat.ForeignKeyConstraint , exploreFKs bool ) {
48+ visitTable := func (table cat.Table , fk cat.ForeignKeyConstraint , recurse bool ) {
5049 tabID := table .ID ()
51- if exploreFKs {
52- tableExplored .Add (int (tabID ))
50+ if recurse {
51+ tableRecursed .Add (int (tabID ))
5352 }
5453 // The order of visiting here is important: namely, we want to visit
5554 // all tables that we reference first, then ourselves, and only then
5655 // tables that reference us.
57- if exploreFKs {
56+ if recurse {
5857 visitForeignKeyReferencedTables (table )
5958 }
6059 visitFn (table , fk )
61- if exploreFKs {
60+ if recurse {
6261 visitForeignKeyReferencingTables (table )
6362 }
6463 }
6564 // handleRelatedTables is a helper function that processes the given table
66- // if it hasn't been explored yet by visiting all referenced and referencing
67- // table of the given one, including via transient (recursive) FK
68- // relationships.
65+ // if it hasn't been recursed into yet by visiting all referenced and
66+ // referencing table of the given one, including via transient (recursive)
67+ // FK relationships.
6968 handleRelatedTables := func (tabID cat.StableID , fk cat.ForeignKeyConstraint ) {
70- if ! tableExplored .Contains (int (tabID )) {
69+ if ! tableRecursed .Contains (int (tabID )) {
7170 ds , _ , err := catalog .ResolveDataSourceByID (ctx , cat.Flags {}, tabID )
7271 if err != nil {
7372 // This is a best-effort attempt to get all the tables, so don't
@@ -80,8 +79,8 @@ func VisitFKReferenceTables(
8079 // error.
8180 return
8281 }
83- exploreFKs := visitPreFn (refTab , fk )
84- visitTable (refTab , fk , exploreFKs )
82+ recurse := recurseFn (refTab , fk )
83+ visitTable (refTab , fk , recurse )
8584 }
8685 }
8786 visitForeignKeyReferencedTables = func (tab cat.Table ) {
@@ -98,9 +97,9 @@ func VisitFKReferenceTables(
9897 }
9998 for _ , tabMeta := range tables {
10099 tabID := tabMeta .Table .ID ()
101- if ! tableExplored .Contains (int (tabID )) {
102- exploreFKs := visitPreFn (tabMeta .Table , nil /* fk */ )
103- visitTable (tabMeta .Table , nil /* fk */ , exploreFKs )
100+ if ! tableRecursed .Contains (int (tabID )) {
101+ recurse := recurseFn (tabMeta .Table , nil /* fk */ )
102+ visitTable (tabMeta .Table , nil /* fk */ , recurse )
104103 }
105104 }
106105}
0 commit comments