Skip to content

Commit

Permalink
Delete All Rows Menu Option
Browse files Browse the repository at this point in the history
  • Loading branch information
s0kil committed Jun 7, 2021
1 parent 53e6f51 commit da4a0c6
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
10 changes: 8 additions & 2 deletions IHP/IDE/Data/Controller.hs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ instance Controller DataController where
action EditRowValueAction { tableName, targetName, id } = do
connection <- connectToAppDb
tableNames <- fetchTableNames connection

rows :: [[DynamicField]] <- fetchRows connection tableName

let targetId = cs id
Expand Down Expand Up @@ -144,6 +144,12 @@ instance Controller DataController where
PG.close connection
redirectTo ShowTableRowsAction { .. }

action DeleteTableRowsAction { tableName } = do
connection <- connectToAppDb
let query = "TRUNCATE TABLE " <> tableName
PG.execute_ connection (PG.Query . cs $! query)
PG.close connection
redirectTo ShowTableRowsAction { .. }

connectToAppDb :: (?context :: ControllerContext) => _
connectToAppDb = PG.connectPostgreSQL $ fromConfig databaseUrl
Expand Down Expand Up @@ -188,7 +194,7 @@ fetchRowsPage :: FromRow r => PG.Connection -> Text -> Int -> Int -> IO [r]
fetchRowsPage connection tableName page rows = do
pkFields <- tablePrimaryKeyFields connection tableName
let slice = " OFFSET " <> show (page * rows - rows) <> " ROWS FETCH FIRST " <> show rows <> " ROWS ONLY"
let query = "SELECT * FROM " <> tableName <> " ORDER BY " <> intercalate ", " pkFields <> slice
let query = "SELECT * FROM " <> tableName <> " ORDER BY " <> intercalate ", " pkFields <> slice
PG.query_ connection (PG.Query . cs $! query)
Expand Down
5 changes: 5 additions & 0 deletions IHP/IDE/Data/View/ShowDatabase.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ renderTableSelector tableNames activeTableName = [hsx|
</a>
<div class="custom-menu menu-for-table shadow backdrop-blur" id={contextMenuId}>
<a href={pathTo (ShowTableAction name)}>Show Schema</a>
<a
href={pathTo (DeleteTableRowsAction name)}
class="js-delete"
data-confirm={"Are you sure you want to delete all rows in '" <> name <> "' table?"}
>Delete All Rows</a>
</div>
|]
where
Expand Down
5 changes: 2 additions & 3 deletions IHP/IDE/ToolServer/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,10 @@ data EnumValuesController
| DeleteEnumValueAction { enumName :: Text, valueId :: Int }
deriving (Eq, Show, Data)


data DataController
= ShowDatabaseAction
| ShowTableRowsAction { tableName :: Text }
| DeleteTableRowsAction { tableName :: Text }
| ShowQueryAction
| DeleteEntryAction { primaryKey :: Text, tableName :: Text }
| CreateRowAction
Expand Down Expand Up @@ -117,7 +117,7 @@ data ColumnDefinition = ColumnDefinition
} deriving (Show)


-- | Keeps track of all all available apps in the projects. Used to display
-- | Keeps track of all all available apps in the projects. Used to display
-- the apps inside the sidebar navigation
--
-- Usually this list is like: @["Web"]@ or @["Web", "Admin"]@
Expand All @@ -130,4 +130,3 @@ newtype AppUrl = AppUrl Text
-- | List of all controllers. Used inside e.g. the Schema Designer to decide whether to display
-- the 'Generate Controller' option
newtype WebControllers = WebControllers [Text]

0 comments on commit da4a0c6

Please sign in to comment.