public function migrateTo(int $target_version)
    {
        if ($this->max_version === null)
            $this->scanMigrations();

        $current_version = $this->getCurrentVersion();
        $trajectory = $this->plan($current_version, $target_version);

        $db = $this->db;

        foreach ($trajectory as $migration)
        {
            $migration['module'] = $this->module;
            $filename = $migration['file'];
            $ext = strtolower(substr($filename, -4));

            $db->beginTransaction();
            try
            {
                static::$logger->info("Migrating module {module} from {from} to {to} using file {file}", $migration);

                if ($ext === ".php")
                {
                    executePHP($db, $filename);
                }
                elseif ($ext === ".sql")
                {
                    $db->executeSQL($filename);
                }

                // If no exceptions were thrown, we're going to assume that the
                // upgrade succeeded, so update the version number in the
                // database and commit the changes.

                // Clear the database schema cache
                $db->clearCache();
                $this->dao = $this->db->getDAO(DBVersion::class);

                $version = new DBVersion;
                $version->module = $this->module;
                $version->from_version = (int)$migration['from'];
                $version->to_version = (int)$migration['to'];
                $version->migration_date = new \DateTime();
                $version->filename = $filename;
                $version->md5sum = md5(file_get_contents($filename));

                $this->dao->save($version);
                static::$logger->info("Succesfully migrated module {module} from {from} to {to} using file {file}", $migration);
                $db->commit();
            }
            catch (\Exception $e)
            {
                // Upgrade failed, roll back to previous state
                static::$logger->error("Migration of module {module} from {from} to {to} using file {file}", $migration);
                static::$logger->error("Exception: {0}", [$e]);
                $db->rollback();
                throw $e;
            }
        }
    }