Browse Source

feature(db): index on ticker,timeframe

master
Denis Tereshkin 6 years ago
parent
commit
b8fcee4c84
  1. 2
      src/ATrade/MDS/Database.hs
  2. 15
      test/Integration/Database.hs

2
src/ATrade/MDS/Database.hs

@ -52,10 +52,12 @@ initDatabase config = do @@ -52,10 +52,12 @@ initDatabase config = do
infoM "DB" $ "Initializing DB"
conn <- connectSqlite3 (T.unpack $ dbPath config)
makeSchema conn
makeIndex conn
infoM "DB" $ "Schema updated"
return conn
where
makeSchema conn = runRaw conn "CREATE TABLE IF NOT EXISTS bars (id SERIAL PRIMARY KEY, ticker TEXT, timestamp BIGINT, timeframe INTEGER, open NUMERIC(20, 10), high NUMERIC(20, 10), low NUMERIC(20, 10), close NUMERIC(20,10), volume BIGINT);"
makeIndex conn = runRaw conn "CREATE INDEX IF NOT EXISTS idx_bars ON bars (ticker, timeframe);"
closeDatabase :: MdsHandle -> IO ()
closeDatabase = disconnect

15
test/Integration/Database.hs

@ -11,13 +11,15 @@ import ATrade.MDS.Database @@ -11,13 +11,15 @@ import ATrade.MDS.Database
import ATrade.Types
import Control.Exception
import Data.DateTime
import Data.Time.Clock
import qualified Data.Text as T
import Data.Time.Clock
import qualified Data.Vector as V
import System.IO.Temp
testDatabase :: TestTree
testDatabase = testGroup "Database tests" [ testOpenClose
testDatabase = testGroup "Database tests" [
testOpenClose
, testOpenCloseTwice
, testPutGet
, testGetReturnsSorted ]
@ -28,6 +30,15 @@ testOpenClose = testCase "Open/Close" $ @@ -28,6 +30,15 @@ testOpenClose = testCase "Open/Close" $
db <- initDatabase dbConfig
closeDatabase db
testOpenCloseTwice :: TestTree
testOpenCloseTwice = testCase "Open/Close twice" $
withSystemTempDirectory "test" $ \fp -> do
let dbConfig = DatabaseConfig (T.pack $ fp ++ "/test.db") T.empty T.empty T.empty
db <- initDatabase dbConfig
closeDatabase db
db2 <- initDatabase dbConfig
closeDatabase db2
bar :: UTCTime -> Price -> Price -> Price -> Price -> Integer -> Bar
bar dt o h l c v = Bar { barSecurity = "FOO",

Loading…
Cancel
Save