Skip to content

Commit

Permalink
Added --latest to glean restore
Browse files Browse the repository at this point in the history
Summary: Add ```--latest``` flag to ``` glean restore ```

Reviewed By: simonmar

Differential Revision: D63266005

fbshipit-source-id: 0e3683628430f4e6e9ab2a96bd12d3d0c785c7de
  • Loading branch information
Julia Molin authored and facebook-github-bot committed Sep 26, 2024
1 parent 3d2b791 commit 1492456
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 7 deletions.
39 changes: 33 additions & 6 deletions glean/tools/gleancli/GleanCLI/Restore.hs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ data WhatToRestore
= RestoreLocator Text
| RestoreDb Repo
| RestoreDbOnDay Text Day
| RestoreDbLatest Text

data RestoreDbSpec
= InstanceOpt Text
| DateOpt Day
| LatestOpt

data RestoreCommand
= Restore
Expand Down Expand Up @@ -77,10 +83,12 @@ instance Plugin RestoreCommand where
(RestoreLocator <$> locator) <|>
(RestoreDb <$> dbSlash) <|> do
repoName <- dbNameOpt
spec <- Left <$> dbInstanceOpt <|> Right <$> dayOpt
spec <- InstanceOpt <$> dbInstanceOpt <|> DateOpt <$> dayOpt
<|> latestOpt
return $ case spec of
Left hash -> RestoreDb (Repo repoName hash)
Right day -> RestoreDbOnDay repoName day
InstanceOpt hash -> RestoreDb (Repo repoName hash)
DateOpt day -> RestoreDbOnDay repoName day
LatestOpt -> RestoreDbLatest repoName

parseDay = return .
parseTimeOrError False defaultTimeLocale (iso8601DateFormat Nothing)
Expand All @@ -90,6 +98,10 @@ instance Plugin RestoreCommand where
<> metavar "YYYY-MM-DD"
<> help "find a DB for this date (UTC)")

latestOpt = flag' LatestOpt
(long "latest"
<> help "find latest instance")

runCommand _ _ backend Restore{..} = do
targets <- locatorsToRestore
restore targets
Expand All @@ -115,9 +127,6 @@ instance Plugin RestoreCommand where
RestoreDbOnDay repoName day -> do
databases <- listWithBackups
let
dbTime Database{..} =
fromMaybe database_created_since_epoch
database_repo_hash_time
matchingDay = listToMaybe
[ database_repo
| db@Database{..} <- sortOn dbTime databases
Expand All @@ -134,6 +143,20 @@ instance Plugin RestoreCommand where
Nothing -> die 1 $ unwords
["Cannot find backup locator for", Text.unpack repoName, "on"
, formatTime defaultTimeLocale (iso8601DateFormat Nothing) day ]
RestoreDbLatest repoName -> do
databases <- listWithBackups
let latestRepo = listToMaybe $ reverse
[ database_repo
| Database{..} <- sortOn dbTime databases
, repo_name database_repo == repoName
]
case latestRepo of
Just repo -> do
let deps = if ignoreDependencies
then []
else dependencies databases repo
withLocator databases $ repo : deps
Nothing -> die 1 $ "Cannot find repo for " <> Text.unpack repoName

getDependenciesOneByOne sites db = do
case database_dependencies db of
Expand All @@ -153,6 +176,10 @@ instance Plugin RestoreCommand where

dieHere repo = die 1 $ "Cannot find " <> show repo

dbTime Database{..} =
fromMaybe database_created_since_epoch
database_repo_hash_time

withLocator :: [Database] -> [Repo] -> IO [(Locator, Maybe Repo)]
withLocator databases repos = forM repos $ \repo -> do
locator <- repoLocator databases repo
Expand Down
2 changes: 1 addition & 1 deletion glean/website/docs/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ DB location, see `:list-all` in glean shell.

Alternatively the DB to restore can be specified by:

* `--db NAME/INSTANCE` or `--db-name NAME` and (`--db-instance INSTANCE` or `--date YYY-MM-DD`)
* `--db NAME/INSTANCE` or `--db-name NAME` and (`--db-instance INSTANCE` or `--date YYY-MM-DD` or `--latest`)

### `glean validate`

Expand Down

0 comments on commit 1492456

Please sign in to comment.