Sahi Pro - Storing Log Reports in MySQL

By default Sahi stores its reports in a H2 database which is bundled in Sahi. For custom reporting it may be necessary to store it into a central MySQL database or another database. To store in a MySQL database do the following

Create a Database

Create a database in MySQL using any MySQL client. Let us assume that the name of the database is sahireports

Create Tables

There is a create tables script available at
sahi/userdata/config/db/create_report_tables_mysql.sql
Run this script on sahireports database to create the necessary tables.

Add MySQL Driver Jars to Sahi Classpath

Download the MySQL Connector/J pack,
extract and copy mysql-connector-java-x.x.x-bin.jar to sahi/userdata/extlib/db folder (create if needed).
Then add the jar to Sahi's classpath

Point Sahi to use the MySQL Database

Add the following to userdata.properties
# Specifies database type.
# The inbuilt options are "mysql" and "h2"
# Change jdbc parameters accordingly
db.type=mysql

#JDBC parameters for mysql database
db.driver_name=com.mysql.jdbc.Driver
db.jdbc_url=jdbc:mysql://localhost/sahireports?allowMultiQueries=true&sessionVariables=sql_mode=NO_BACKSLASH_ESCAPES
db.user_name=root
db.password=

Change the user_name, password and database name as required.

Restart and Run

Restart Sahi, run a script and check!

Trouble-shooting

If you see an error like
com.mysql.jdbc.PacketTooBigException: Packet for query is too large
run the following query once on your database
SET GLOBAL max_allowed_packet=16777216

warning MySQL database does not automatically update itself in Sahi.
You need to manually update the database, whenever needed.
For using the MySQL data for your updated version you need to run your database
with the queries between the end tag of your previous version and the end tag
of your updated version in the sql file that follows.

sahi/userdata/config/db/create_report_tables_mysql.sql

eg: If you were using Sahi version 4.5 and want to update your MySQL database for
logging in your updated Sahi version 4.6, you need to run the queries from the
commented tag version 4.5 end to version 4.6 end
in the sahi/userdata/config/db/create_report_tables_mysql.sql file.