Welcome Guest, Not a member yet? Register   Sign In
Wrapping multiple dbforge calls into a single transaction
#1

[eluser]Unknown[/eluser]
Hi,

For my job, I was tasked with writing a custom migration tool for a very specific setup that the default migration tool with codeigniter wasn't built to do.

In doing so, I shortcutted some of my work by using the dbforge functions. I wrap all the migration calls in a manual transaction, and if any validation in my migration fails, it performs a rollback. To summarize my migration class, it looks something like this:

Code:
class CustomMigration
{
private $CI = false;
private $in_transaction = false;
public function __construct()
{
  $this->CI =& get_instance(); // Gets the codeigniter instance.
  // .. set up database
  // load dbforge and dbutils
  // other inits
  $this->CI->db->trans_begin()
  $this->in_transaction = true;
}

public function __destruct()
{
  if ( $in_transaction )
  {
    if ($this->CI->db->trans_status() === FALSE)
    {
      $this->CI->db->trans_rollback();
    }
    else
    {
      $this->CI->db->trans_commit();
    }
  }

public function fail_transaction()
{
  // Do some logging to the console
  $this->CI->db->trans_rollback();
  $this->in_transaction = false;
}
}

When fail_transaction() gets triggered, it automatically exits from the migration, but when I look at my mysql server with phpmyadmin after a known situation that the transation should fail and rollback, I see the changes that my migration should have reversed (for instance, creating a table in the database). This makes fixing my migration and retrying it impossible.

Does dbforge somehow block or use transactions internally in a way that would disable/trigger my transactions early?
#2

[eluser]jmadsen[/eluser]
Are you using InnoDB tables? MyISAM doesn't support transactions, and I don't believe CI gives any notice of the fact. Just something to check
#3

[eluser]Unknown[/eluser]
Yes, I can confirm that I'm using InnoDB.




Theme © iAndrew 2016 - Forum software by © MyBB