@@ -10,7 +10,6 @@ defmodule Beacon.Migrations.GraphQLMigrator do
1010 Beacon.Migrations.GraphQLMigrator.dry_run(:my_site)
1111 """
1212
13- import Ecto.Query
1413 require Logger
1514
1615 @ doc """
@@ -22,42 +21,38 @@ defmodule Beacon.Migrations.GraphQLMigrator do
2221 config = Beacon.Config . fetch! ( site )
2322 repo = config . repo
2423
25- # Find pages with data_sources in their extra field
24+ % { rows: page_rows } = repo . query! (
25+ "SELECT id, path, extra FROM beacon_pages WHERE site = $1" ,
26+ [ to_string ( site ) ]
27+ )
28+
2629 pages_with_data_sources =
27- repo . all (
28- Ecto.Query . from ( p in "beacon_pages" ,
29- where: p . site == ^ to_string ( site ) ,
30- select: % { id: p . id , path: p . path , extra: p . extra }
31- )
32- )
33- |> Enum . filter ( fn page ->
34- extra = page . extra || % { }
30+ page_rows
31+ |> Enum . filter ( fn [ _id , _path , extra ] ->
32+ extra = extra || % { }
3533 ds = Map . get ( extra , "data_sources" , [ ] )
3634 is_list ( ds ) and ds != [ ]
3735 end )
36+ |> Enum . map ( fn [ id , path , extra ] ->
37+ % { id: id , path: path , data_sources: Map . get ( extra || % { } , "data_sources" , [ ] ) }
38+ end )
39+
40+ % { rows: handler_rows } = repo . query! (
41+ "SELECT id, name, format FROM beacon_event_handlers WHERE site = $1" ,
42+ [ to_string ( site ) ]
43+ )
3844
39- # Find event handlers using elixir format (candidates for conversion)
4045 elixir_handlers =
41- repo . all (
42- Ecto.Query . from ( eh in "beacon_event_handlers" ,
43- where: eh . site == ^ to_string ( site ) ,
44- select: % { id: eh . id , name: eh . name , format: eh . format }
45- )
46- )
47- |> Enum . filter ( fn h -> h . format == "elixir" or is_nil ( h . format ) end )
46+ handler_rows
47+ |> Enum . filter ( fn [ _id , _name , format ] -> format == "elixir" or is_nil ( format ) end )
48+ |> Enum . map ( fn [ id , name , _format ] -> % { id: id , name: name } end )
4849
4950 % {
5051 site: site ,
5152 pages_with_legacy_data_sources: length ( pages_with_data_sources ) ,
52- pages: Enum . map ( pages_with_data_sources , fn p ->
53- % {
54- id: p . id ,
55- path: p . path ,
56- data_sources: Map . get ( p . extra || % { } , "data_sources" , [ ] )
57- }
58- end ) ,
53+ pages: pages_with_data_sources ,
5954 elixir_event_handlers: length ( elixir_handlers ) ,
60- handlers: Enum . map ( elixir_handlers , & % { id: & 1 . id , name: & 1 . name } )
55+ handlers: elixir_handlers
6156 }
6257 end
6358
@@ -85,7 +80,6 @@ defmodule Beacon.Migrations.GraphQLMigrator do
8580 steps ++ [
8681 "== Legacy Data Sources ==" ,
8782 "#{ report . pages_with_legacy_data_sources } page(s) have data_sources in their extra field." ,
88- "These were configured via the old DataStore system and need to be converted to page queries." ,
8983 "Steps:" ,
9084 " 1. Create a GraphQL endpoint in Beacon admin pointing to your host app's API" ,
9185 " 2. For each data source, create a page query with the equivalent GraphQL query" ,
@@ -104,8 +98,7 @@ defmodule Beacon.Migrations.GraphQLMigrator do
10498 "" ,
10599 "== Elixir Event Handlers ==" ,
106100 "#{ report . elixir_event_handlers } handler(s) use raw Elixir code: #{ handler_names } " ,
107- "These can optionally be converted to declarative actions format." ,
108- "Use the Event Handler editor's format toggle to switch between Elixir and Actions."
101+ "These can optionally be converted to declarative actions format."
109102 ]
110103 else
111104 steps
@@ -116,20 +109,16 @@ defmodule Beacon.Migrations.GraphQLMigrator do
116109
117110 @ doc """
118111 Clean up legacy data_sources from page extra fields.
119- This removes the data_sources key from the extra map of all pages on the site.
120112 """
121113 @ spec clean_legacy_extra! ( atom ( ) ) :: :ok
122114 def clean_legacy_extra! ( site ) do
123115 config = Beacon.Config . fetch! ( site )
124116 repo = config . repo
125117
126- { count , _ } =
127- repo . update_all (
128- Ecto.Query . from ( p in "beacon_pages" ,
129- where: p . site == ^ to_string ( site )
130- ) ,
131- set: [ extra: Ecto.Query . dynamic ( [ p ] , fragment ( "? - 'data_sources'" , p . extra ) ) ]
132- )
118+ % { num_rows: count } = repo . query! (
119+ "UPDATE beacon_pages SET extra = extra - 'data_sources' WHERE site = $1 AND extra ? 'data_sources'" ,
120+ [ to_string ( site ) ]
121+ )
133122
134123 Logger . info ( "[GraphQLMigrator] Cleaned data_sources from #{ count } pages on site #{ site } " )
135124 :ok
0 commit comments