55 This source code is licensed under the BSD-style license found in the
66 LICENSE file in the root directory of this source tree.
77-}
8- {-# LANGUAGE CPP, ApplicativeDo #-}
8+ {-# LANGUAGE ApplicativeDo #-}
99module GleanCLI.Merge (MergeCommand ) where
1010
1111import Control.Exception
1212import qualified Data.ByteString as B
1313import qualified Data.ByteString.Lazy.Char8 as L
1414import qualified Data.Aeson as Aeson
15+ import Data.Default
1516import Data.IORef
1617import Options.Applicative
1718import System.Directory
19+ import System.Directory.Extra (listFiles )
1820import System.FilePath
1921import System.IO
2022import System.Process
21- import Data.Text (unpack )
2223import Control.Concurrent.Stream
2324import Util.OptParse
2425import Util.Log ( logInfo )
25- import Glean.Util.ThriftSource (load )
2626import Thrift.Protocol.Compact
2727
2828import Glean.LocalOrRemote (loadDbSchema )
@@ -34,23 +34,16 @@ import qualified Glean.RTS.Foreign.FactSet as FactSet
3434import Glean.RTS.Foreign.Define (DefineFlags (.. ), defineBatch )
3535import qualified Glean.RTS.Foreign.Inventory as Inventory
3636import Glean.RTS.Foreign.Ownership
37+ import qualified Glean.Database.Config as GleanDB
3738import Glean.Database.Schema.Types
38- import Glean.Database.Util (getDbSchemaFromId )
39+ import qualified Glean.Util.ThriftSource as ThriftSource
40+ import Glean.Database.Config (cfgServerConfig )
41+ import Glean.Database.Schema
3942
4043import GleanCLI.Types
4144import GleanCLI.Common (dbOpts , fileFormatOpt , FileFormat (.. ))
4245import Glean.Write (fileToBatches , schemaIdToOpts )
4346import Glean.Write.JSON (buildJsonBatch )
44- import Glean.Database.Config (schemaSourceIndexConfig )
45- import Glean.DefaultConfigs (schemaConfigPath )
46- import System.Directory.Extra (listFiles )
47-
48- #if GLEAN_FACEBOOK
49- import Configerator (
50- withConfigeratorAPI ,
51- defaultConfigeratorOptions ,
52- )
53- #endif
5447
5548
5649data MergeCommand = MergeCommand
@@ -88,11 +81,11 @@ instance Plugin MergeCommand where
8881 fileFormat <- fileFormatOpt BinaryFormat
8982 return MergeCommand {.. }
9083
91- withService _evb _cfgAPI _svc MergeCommand {.. } = do
84+ withService evb cfgAPI svc MergeCommand {.. } = do
9285 (inventory, dbSchema) <- case inventorySource of
9386 Left repo -> do
9487 dbSchema <- Glean. withBackendWithDefaultOptions
95- _evb _cfgAPI _svc Nothing $ \ backend -> do
88+ evb cfgAPI svc Nothing $ \ backend -> do
9689 loadDbSchema backend repo
9790 logInfo(" db's schema ID is: " <> show (schemaId dbSchema))
9891 return (schemaInventory dbSchema, Just dbSchema)
@@ -170,52 +163,24 @@ instance Plugin MergeCommand where
170163
171164 batch <- case fileFormat of
172165 JsonFormat -> do
166+ (batches, schema_id_file) <- fileToBatches file
173167 case dbSchema of
174- Nothing ->
175- # if GLEAN_FACEBOOK
176- withConfigeratorAPI defaultConfigeratorOptions $ \ configAPI ->
177- -- Load the schema index from the schema directory
178- let schemaIndexResult =
179- Right $ schemaSourceIndexConfig schemaConfigPath in do
180- hPutStrLn stderr $
181- " Reading current schema index from default "
182- <> " configerator with path " <> unpack schemaConfigPath
183-
184- -- Get the schema_id from the JSON file
185- (batches, Just schema_id) <- fileToBatches file
186- logInfo(" Schema_ID from JSON file " <> file <> " is "
187- <> show schema_id)
188-
189- -- Try to get the DbSchema from the schema ID string
190- case schemaIndexResult of
191- Left err -> do
192- throwIO $ ErrorCall err
193- Right schemaIndex -> do
194- logInfo $ " Looking up schema with ID: " <>
195- show schema_id
196- -- Materialise the schema index from its Thrift source
197- concreteIndex <- load configAPI schemaIndex
198- dbSchemaResult <-
199- (Right <$>
200- getDbSchemaFromId (Just concreteIndex) schema_id
201- )
202- `catch` \ (e :: SomeException ) ->
203- return $ Left $ " Failed to get schema: " <> show e
204-
205- case dbSchemaResult of
206- Left err -> do
207- throwIO $ ErrorCall (" Error getting dbSchemaResult"
208- <> " - exiting. Error is:\n " <> err)
209- Right dbSchema -> do
210- logInfo $ " Successfully loaded schema with ID: "
211- <> show schema_id
212- buildJsonBatch dbSchema
213- (schemaIdToOpts $ Just (schemaId dbSchema)) batches
214- # else
215- return $ Left $ " Failed to get schema: "
216- # endif
168+ Nothing -> do
169+ schema_id <- case schema_id_file of
170+ Nothing -> throwIO $ ErrorCall " missing schema ID"
171+ Just id -> return id
172+ let cfg = case svc of
173+ Glean. Local cfg _ -> cfg
174+ Glean. Remote {} -> def
175+ serverConfig <- ThriftSource. load cfgAPI (cfgServerConfig cfg)
176+ loc <- GleanDB. schemaLocation cfg serverConfig
177+ let (schemaSource, _) = GleanDB. cfgSchemaHook cfg loc
178+ index <- ThriftSource. load cfgAPI schemaSource
179+ dbSchema <- newDbSchema Nothing index
180+ (SpecificSchemaId schema_id) readWriteContent def
181+ buildJsonBatch dbSchema
182+ (schemaIdToOpts $ Just (schemaId dbSchema)) batches
217183 Just schema -> do
218- (batches, schema_id_file) <- fileToBatches file
219184 if Just (schemaId schema) == schema_id_file then
220185 logInfo(
221186 " Schema matches with db schema. Merging data from "
@@ -224,7 +189,7 @@ instance Plugin MergeCommand where
224189 else
225190 throwIO $ ErrorCall $
226191 " ERROR - ABORTING MERGE\n Schema ID mismatch:\n db: "
227- <> show (schemaId schema) <> " \n vs\n File: "
192+ <> show (schemaId schema) <> " \n vs\n File: "
228193 <> file <> " has " <> show schema_id_file
229194 let getSchemaId theschema = Just (schemaId theschema) in
230195 buildJsonBatch schema
0 commit comments