<commit_before>{-# LANGUAGE MultiParamTypeClasses,FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Database.Schema.Migrations.Backend.HDBC
    ()
where

import Database.HDBC ( quickQuery, fromSql, SqlValue(SqlString), IConnection(getTables) )

import Database.Schema.Migrations.Backend
    ( Backend(..) )
import Database.Schema.Migrations.Migration
    ( Migration(..)
    , newMigration
    )

createSql :: String
createSql = "CREATE TABLE installed_migrations (\
            \migration_id TEXT)"

revertSql :: String
revertSql = "DROP TABLE installed_migrations"

instance (IConnection conn) => Backend conn IO where
    isBootstrapped conn = do
      -- if 'installed_migrations' is in the list of tables,
      -- bootstrapping has been performed.
      tables <- getTables conn
      return $ "installed_migrations" `elem` tables

    getBootstrapMigration _ =
        do
          m <- newMigration "root"
          return $ m { mApply = createSql
                     , mRevert = Just revertSql
                     , mDesc = Just "Migration table installation"
                     }

    applyMigration conn m = do
      quickQuery conn (mApply m) []
      quickQuery conn "INSERT INTO installed_migrations (migration_id) VALUES (?)" [SqlString $ mId m]
      return ()

    revertMigration conn m = do
        case mRevert m of
          Nothing -> return ()
          Just query -> quickQuery conn query [] >> return ()
        -- Remove migration from installed_migrations in either case.
        quickQuery conn "DELETE FROM installed_migrations WHERE migration_id = ?" [SqlString $ mId m]
        return ()

    getMigrations conn = do
      results <- quickQuery conn "SELECT migration_id FROM installed_migrations" []
      return $ map (\(h:_) -> fromSql h) results
<commit_msg>HDBC: use toSql and run<commit_after>{-# LANGUAGE MultiParamTypeClasses,FlexibleInstances #-}
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Database.Schema.Migrations.Backend.HDBC
    ()
where

import Database.HDBC ( quickQuery, fromSql, toSql, IConnection(getTables, run) )

import Database.Schema.Migrations.Backend
    ( Backend(..) )
import Database.Schema.Migrations.Migration
    ( Migration(..)
    , newMigration
    )

createSql :: String
createSql = "CREATE TABLE installed_migrations (\
            \migration_id TEXT)"

revertSql :: String
revertSql = "DROP TABLE installed_migrations"

instance (IConnection conn) => Backend conn IO where
    isBootstrapped conn = do
      -- if 'installed_migrations' is in the list of tables,
      -- bootstrapping has been performed.
      tables <- getTables conn
      return $ "installed_migrations" `elem` tables

    getBootstrapMigration _ =
        do
          m <- newMigration "root"
          return $ m { mApply = createSql
                     , mRevert = Just revertSql
                     , mDesc = Just "Migration table installation"
                     }

    applyMigration conn m = do
      quickQuery conn (mApply m) []
      quickQuery conn "INSERT INTO installed_migrations (migration_id) VALUES (?)" [toSql $ mId m]
      return ()

    revertMigration conn m = do
        case mRevert m of
          Nothing -> return ()
          Just query -> run conn query [] >> return ()
        -- Remove migration from installed_migrations in either case.
        quickQuery conn "DELETE FROM installed_migrations WHERE migration_id = ?" [toSql $ mId m]
        return ()

    getMigrations conn = do
      results <- quickQuery conn "SELECT migration_id FROM installed_migrations" []
      return $ map (\(h:_) -> fromSql h) results

{'fraction_non_alphanumeric': 0.08909795240730492, 'fraction_numerical': 0.0, 'mean_word_length': 2.5792079207920793, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '817056', 'n_tokens_mistral': 1035, 'n_tokens_neox': 1025, 'n_words': 305}