Skip to content

Commit 815b162

Browse files
committed
excluding with statements from dependencies
1 parent b0cd1ca commit 815b162

File tree

4 files changed

+53
-17
lines changed

4 files changed

+53
-17
lines changed

src/MigLib/DeclarativeMigrations/SqlParser.fs

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,13 @@ type SqlVisitor() =
1919
let rec childrenToText (x: Tree.IParseTree) =
2020
seq {
2121
if x.ChildCount = 0 then
22+
2223
yield x.GetText()
2324
else
2425
for n in { 0 .. x.ChildCount - 1 } do
25-
yield! childrenToText (x.GetChild(n))
26+
yield! n |> x.GetChild |> childrenToText
2627
}
2728

28-
let selectDependencies (context: SQLiteParser.Select_stmtContext) =
29-
context.select_core ()
30-
|> Seq.head
31-
|> _.table_or_subquery()
32-
|> Seq.map (fun t -> t.table_name().GetText())
33-
|> Seq.toList
34-
3529
override this.VisitSql_stmt_list(context: SQLiteParser.Sql_stmt_listContext) =
3630
context.sql_stmt ()
3731
|> Array.choose (this.Visit >> Option.ofObj)
@@ -131,18 +125,38 @@ type SqlVisitor() =
131125
constraints = constraints } //TODO parse table constraints
132126

133127

134-
override this.VisitCreate_view_stmt(context: SQLiteParser.Create_view_stmtContext) =
128+
override _.VisitCreate_view_stmt(context: SQLiteParser.Create_view_stmtContext) =
129+
let selectDependencies (context: SQLiteParser.Select_stmtContext) =
130+
context.select_core ()
131+
|> Seq.head
132+
|> _.table_or_subquery()
133+
|> Seq.map (fun t -> t.table_name().GetText())
134+
|> Seq.toList
135+
136+
// FIXME it's possible the remaining UNION clauses are ignored because of a bug in the grammar
135137
let sql = context.children |> Seq.map childrenToText |> Seq.concat
136138

137139
let name = context.view_name().GetText().Trim '"'
138140

139141
// FIXME assumes a query in the form `FROM table0, table1, …`
140142
let tables = context.select_stmt () |> selectDependencies
141143

144+
let withTables, withDeps =
145+
match context.select_stmt().common_table_stmt () with
146+
| null -> [], []
147+
| ct ->
148+
ct.common_table_expression ()
149+
|> Array.map (fun x -> x.table_name().GetText(), selectDependencies (x.select_stmt ()))
150+
|> Array.unzip
151+
|> fun (xs, yss) -> Array.toList xs, yss |> Array.toList |> List.concat
152+
153+
// FIXME for WITH statements inside WITH statements this trick might not work
154+
let topLevelTables = set tables + set withDeps - set withTables |> Set.toList
155+
142156
View
143157
{ name = name
144158
sqlTokens = sql
145-
dependencies = tables }
159+
dependencies = topLevelTables }
146160

147161
override this.VisitCreate_index_stmt(context: SQLiteParser.Create_index_stmtContext) =
148162
let name = context.index_name().GetText().Trim '"'
@@ -197,6 +211,6 @@ let parse (_file: string, sql: string) =
197211
triggers = t :: acc.triggers }
198212
| StatList ys -> failwith $"unexpected statement list {ys}")
199213
emptyFile
200-
|> Ok
201-
| Some expr -> Error $"expecting statement list, got {expr}"
202-
| None -> Ok emptyFile
214+
|> Result.Ok
215+
| Some expr -> Result.Error $"expecting statement list, got {expr}"
216+
| None -> Result.Ok emptyFile

src/MigLib/MigLib.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<GenerateDocumentationFile>true</GenerateDocumentationFile>
77

88
<PackageId>MigLib</PackageId>
9-
<Version>1.0.4</Version>
9+
<Version>1.0.5</Version>
1010
<PackageOutputPath>./nupkg</PackageOutputPath>
1111
<InvariantGlobalization>true</InvariantGlobalization>
1212
<RootNamespace>migrate</RootNamespace>

src/Test/ViewMigration.fs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,35 @@ let view0 =
2020

2121
let schema0 = student
2222

23-
let schema1 =
23+
let schema0' =
2424
$"
2525
{student};
2626
{view0}
2727
"
2828

29-
let cases = [ schema0, schema1, Ok [ view0 ] ]
29+
let view1 =
30+
"CREATE VIEW view0 AS WITH bla AS( SELECT * FROM coco) SELECT * FROM bla b, student s"
31+
32+
let schema1 =
33+
$"
34+
{student};
35+
{view1}
36+
"
37+
38+
// let view2 = "CREATE VIEW view0 AS SELECT * FROM coco UNION SELECT * FROM pepe"
39+
40+
// let schema2 =
41+
// $"
42+
// {student};
43+
// {view2}
44+
// "
45+
46+
let cases =
47+
[ schema0, schema0', Ok [ view0 ]
48+
schema0, schema1, Ok [ view1 ]
49+
// schema0, schema2, Ok [ view2 ] ]
50+
]
51+
3052

3153
let testViewMigration (case: int) (left: string, right: string, r: Result<string list, string>) =
3254
result {

src/mig/mig.fsproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
<PackageId>migtool</PackageId>
1111
<RootNamespace>migrate</RootNamespace>
1212
<AssemblyName>migrate</AssemblyName>
13-
<Version>1.0.4</Version>
13+
<Version>1.0.5</Version>
1414
<PackageOutputPath>./nupkg</PackageOutputPath>
1515
<InvariantGlobalization>true</InvariantGlobalization>
1616

0 commit comments

Comments
 (0)