<commit_before>module Database.Schema.Migrations.Filesystem
    ( newFilesystemStore

    , FilesystemStore
    , storePath
    , migrationMap

    , MigrationMap
    , migrationFromFile
    )
where

import System.Directory ( getDirectoryContents, doesFileExist )
import System.FilePath ( takeDirectory, takeFileName, (</>) )

import qualified Data.Map as Map
import Data.Time.Clock ( UTCTime )
import Data.Time () -- for UTCTime Show instance
import Data.Maybe ( isNothing )

import Control.Monad ( filterM, when, mapM_ )
import Control.Monad.Trans ( liftIO )
import Control.Monad.State ( StateT, get, put, execStateT )

import Text.ParserCombinators.Parsec ( parse )

import Database.Schema.Migrations.Migration
    ( Migration(..)
    , newMigration
    )
import Database.Schema.Migrations.Filesystem.Parse

-- |Code for parsing and serializing Migrations to disk files, and an
-- instance of MigrationStore for filesystem-backed migrations.

type MigrationMap = Map.Map String Migration
type FieldProcessor = String -> Migration -> Maybe Migration

data FilesystemStore = FSStore { storePath :: FilePath
                               , migrationMap :: MigrationMap }

-- |Create a new filesystem store by loading all migrations at the
-- specified filesystem path.
newFilesystemStore :: FilePath -> IO FilesystemStore
newFilesystemStore path = do
  migrations <- execStateT (loadMigrations path) Map.empty
  return $ FSStore { storePath = path
                   , migrationMap = migrations }

-- |Given a directory path, return a list of all files in the
-- directory, not including the special directories "." and "..".
filesInDirectory :: FilePath -> IO [FilePath]
filesInDirectory path = do
  contents <- getDirectoryContents path
  let withPath = map (path </>) nonSpecial
      nonSpecial = [ f | f <- contents, not (f `elem` [".", ".."]) ]
  liftIO $ filterM doesFileExist withPath

-- |Load migrations recursively from the specified path into the<commit_msg>Add MigrationStore instance for FilesystemStore<commit_after>module Database.Schema.Migrations.Filesystem
    ( newFilesystemStore

    , FilesystemStore
    , storePath
    , migrationMap

    , MigrationMap
    , migrationFromFile
    )
where

import System.Directory ( getDirectoryContents, doesFileExist )
import System.FilePath ( takeDirectory, takeFileName, (</>) )

import qualified Data.Map as Map
import Data.Time.Clock ( UTCTime )
import Data.Time () -- for UTCTime Show instance
import Data.Maybe ( isNothing )

import Control.Monad ( filterM, when, mapM_ )
import Control.Monad.Trans ( liftIO )
import Control.Monad.State ( StateT, get, put, execStateT )

import Text.ParserCombinators.Parsec ( parse )

import Database.Schema.Migrations.Migration
    ( Migration(..)
    , newMigration
    )
import Database.Schema.Migrations.Filesystem.Parse
import Database.Schema.Migrations.Store

-- |Code for parsing and serializing Migrations to disk files, and an
-- instance of MigrationStore for filesystem-backed migrations.

type MigrationMap = Map.Map String Migration
type FieldProcessor = String -> Migration -> Maybe Migration

data FilesystemStore = FSStore { storePath :: FilePath
                               , migrationMap :: MigrationMap }

instance MigrationStore FilesystemStore where
    getMigrations s = return $ Map.elems $ migrationMap s

    -- saveMigration s m = do
    --   let filename = storePath s </> mId m
    --   writeFile filename $ serializeMigration m

-- |Create a new filesystem store by loading all migrations at the
-- specified filesystem path.
newFilesystemStore :: FilePath -> IO FilesystemStore
newFilesystemStore path = do
  migrations <- execStateT (loadMigrations path) Map.empty
  return $ FSStore { storePath = path
                   , migrationMap = migrations }

-- |Given a directory path, return a list of all files in the
-- directory, not including the special directories "." and "..".
filesInDirectory :: FilePath -> IO [FilePath]
filesInDirectory path = do
  contents <- getDirectoryContents path
  let withPath = map (path </>) nonSpecial
      nonSpecial = [ f | f <- contents, not (f `elem` [".", ".."]) ]
  liftIO $ filterM doesFileExist withPath

-- |Load migrations recursively from the specified path into the
{'fraction_non_alphanumeric': 0.08637326429748177, 'fraction_numerical': 0.0, 'mean_word_length': 3.5405982905982905, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '404096', 'n_tokens_mistral': 1171, 'n_tokens_neox': 1131, 'n_words': 385}