MyNixOS website logo
Description

A pure haskell backend for the persistent library using MySQL database server.

This package contains a backend for persistent using the MySQL database server. Internally it uses the mysql-pure package in order to access the database. See README.md for more.

This package supports only MySQL 5.1 and above. However, it has been tested only on MySQL 5.5. Only the InnoDB storage engine is officially supported.

Known problems:

  • This package does not support statements inside other statements.

persistent-mysql-pure

hackage version

This is a fork of persistent-mysql-haskell. Using the latest branch from chordify.

A pure haskell backend for persistent using the MySQL database server. Internally it uses the mysql-pure driver in order to access the database.

See example/Main.hs for how this MySQL backend can be used with Persistent.

Motivation

persistent-mysql uses mysql (via mysql-simple) as the database driver. mysql is a haskell FFI wrapper for mysqlclient written in C.

Reasons to use a pure haskell driver:

Personal experience on replacing mysql-simple with mysql-pure in a project:

  • Performance gains consistent with benchmark.

  • Smoother deployment to AWS, since mysql appears to have a hard dependency on the oracle version of libmysqlclient that does not work with the open source variant that is available by default on Amazon Linux (and possibly on other Linux distros).

Potential issues moving from persistent-mysql to persistent-mysql-pure

ConnectInfo and defaultConnectInfo are not the same between mysql and mysql-pure, therefore this package is not a 100% drop in replacement for persistent-mysql from the connection configuration perspective.

  • mysql-pure does not allow provide an API for the entirety of mysqlclient options. Therefore neither can this package.

  • Given the inevitable incompatibility with persistent-mysql, and in the interest of providing a forward-compatible API, ConnectInfo internals and defaultConnectInfo have been deprecated. However the similar utility can be achieved like so:

    import Database.Persist.MySQL
    
    connectInfo :: MySQLConnectInfo
    - connectInfo = defaultConnectInfo
    -             { connectHost     = "localhost"
    -             , connectUser     = "test"
    -             , connectPassword = "test"
    -             , connectDatabase = "test"
    -             }
    + connectInfo = mkMySQLConnectInfo "localhost" "test" "test" "test"
    
    connectInfoNewPort :: MySQLConnectInfo
    - connectInfoNewPort = connectInfo { connectPort = 3307 }
    + connectInfoNewPort = setMySQLConnectInfoPort 3307 connectInfo
    
    connectInfoNewCharSet :: MySQLConnectInfo
    - connectInfoNewCharSet = connectInfo { connectOptions = [CharsetName "utf8"] }
    + connectInfoNewCharSet = setMySQLConnectInfoCharset 33 connectInfo
    
    
  • mysql-pure and mysql have different APIs/mechanisms for securing the connection to MySQL. persistent-mysql-pure exposes an API to utilize TLS client params that ships with mysql-pure.

    connectInfoCustomCaStore :: MySQLConnectInfo
    - connectInfoCustomCaStore = connectInfo { connectSSL = Just customCaParams }
    + connectInfoCustomCaStore = setMySQLConnectInfoTLS customCaParams connectInfo
        where
    -         customCaParams = defaultSSLInfo { sslCAPath = "foobar.pem" }
    +         customCaParams = makeClientParams $ CustomCAStore "foobar.pem"
    

Aside from connection configuration, persistent-mysql-pure is functionally on par with persistent-mysql (as of writing this). This can be seen by comparing persistent-test between this fork and upstream.

Yesod

In order to use persistent-mysql-pure with yesod you have to modify Settings.hs:

- import Database.Persist.MySQL     (MySQLConf (..))
+ import Database.Persist.MySQL     (MySQLConf, mkMySQLConf, myConnInfo, myPoolSize, setMySQLConnectInfoCharset)
- import qualified Database.MySQL.Base as MySQL
-         -- This code enables MySQL's strict mode, without which MySQL will truncate data.
-         -- See https://github.com/yesodweb/persistent/wiki/Database-Configuration#strict-mode for details
-         -- If you choose to keep strict mode enabled, it's recommended that you enable it in your my.cnf file so that it's also enabled for your MySQL console sessions.
-         -- (If you enable it in your my.cnf file, you can delete this code).
-         let appDatabaseConf = fromYamlAppDatabaseConf { myConnInfo = (myConnInfo fromYamlAppDatabaseConf) {
-                 MySQL.connectOptions =
-                   ( MySQL.connectOptions (myConnInfo fromYamlAppDatabaseConf)) ++ [MySQL.InitCommand "SET SESSION sql_mode = 'STRICT_ALL_TABLES';\0"]
-               }
-             }

And in Application.hs:

- import qualified Database.MySQL.Base as MySQL
  import Network.Wai.Handler.Warp             (Settings, defaultSettings,
                                               defaultShouldDisplayException,
                                               runSettings, setHost,
-                                              setFork, setOnOpen, setOnClose,
+                                              setFork,
                                               setOnException, setPort, getPort)
-     -- See http://www.yesodweb.com/blog/2016/11/use-mysql-safely-in-yesod
-     MySQL.initLibrary
-     $ setOnOpen (const $ MySQL.initThread >> return True)
-     $ setOnClose (const MySQL.endThread)

Optionally you may enable the MYSQL strict mode (in each transaction) by modifying Foundation.hs (or editing the my.cnf server configuration):

- import Database.Persist.Sql (ConnectionPool, runSqlPool)
+ import Database.Persist.Sql (ConnectionPool, rawExecute, runSqlPool)
-         runSqlPool action $ appConnPool master
+         runSqlPool
+           (rawExecute "SET SESSION sql_mode = 'STRICT_ALL_TABLES'" [] >> action)
+           (appConnPool master)

FAQs

Why isn't this part of the main/upstream persistent repo?

persistent-mysql supports X but persistent-mysql-pure API doesn't. Why?

  • Internals (getters/setters) of MySQLConnectInfo and defaultConnectInfo are intentionally masked for forward compatibility.

  • For all others, feel free to open an issue and/or submit a PR.

Does persistent-mysql-pure ship with tests?

  • It does! :) persistent-test is fully re-used with an additional flag to specifically test persistent-mysql-pure.

    stack test persistent-test --flag persistent-test:mysql_haskell --exec persistent-test
    
Metadata

Version

1.0.2

License

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows