Hi Anton,
here is how i would do it:
1. Create a new config file for your other db, lets say config/otherDB.yml
2. Parse that config file in makeFoundation of Application.hs
3. Initialize your new db
* Either keep a separate pool of connections in your foundation datatype (App)
* Open/Close connection whenever you need an access to the database (and perhaps manager the pool with an application in between Yesod and the DB)
4. In Foundation.hs create a new runDB function (perhaps name it runDB2) to run your other db actions.
5. ???
6. profit
so taking those steps in consideration, here is my proposal for the code using the approach of opening and closing the connection (if you choose to keep another separate pool of connections then it should be easier since it really ammounts to copy paste what currently exists).
(Assuming postgresql with postgresql-simple):
import Database.PostgreSQL.Simple
1. copy/paste your config/postgresql.yml to config/postgresql2.yml and change the options to match your 2nd DB
2. in makeFoundation, parse the config/postgresql2.yml right bellow dbconf is setup:
pgconf <- withYamlEnvironment "config/postgresql2.yml" (appEnv conf)
(\(Object o) ->do
database <- o .: "database"
host <- o .: "host"
port <- o .: "port"
user <- o .: "user"
password <- o .: "password"
-- no need for pool size
-- the pool can be defined in pgbouncer or other fancy tool
return $ ConnectInfo { connectHost = host
, connectPort = port
, connectUser = user
, connectPassword = password
, connectDatabase = database
})
3. Add pgconf to the App datatype:
a) in Foundation.hs change your foundation datatype to something like:
data App = App
{ settings :: AppConfig DefaultEnv Extra
, getLogger :: Logger
, getStatic :: Static -- ^ Settings for static file serving.
, connPool :: Database.Persist.Store.PersistConfigPool Settings.PersistConfig -- ^ Database connection pool.
, httpManager :: Manager
, persistConfig :: Settings.PersistConfig
-- New stuff here:
, pgConn :: ConnectInfo -- ^ 2nd db connection
}
b) Initialize the DB and add the connectionInfo to the foundation datatype instance in makeFoundation of Application.hs:
c <- connect pgconf
-- Initializae your sql tables here
close c
return $ App conf setLogger s p manager dbconf pgconf -- add the pgconf created in 2. to the App datatype
4. Create a new runDB2 to ease the usage of this new DB:
Dump it anywhere in your Foundation.hs:
runDB2 f = do
y <- getYesod
liftIO $ do
conn <- connect $ pgConn y
res <- f conn
close conn
return res
Again, all of this should be easier if you want yesod to manage your connection pools, since it would only be necessary to replicate (by copy pasting) the existing code. As for the migration just don't call runMigration migrateAll in your makeFoundation if you don't want it.
I hope this is useful somehow to you,
good luck :)