\documentclass{article}
\usepackage{ulem}
\usepackage{graphicx}
\usepackage{hyperref}
\pagestyle{headings}
\begin{document}
\part{Horde\_Db Library and Horde Database Migrations}
\section{Horde DB: Usage by Example}
\subsection{Get a Connection object}
\subsection{Selecting Values}
\subsection{Insert}
\subsection{Delete}
\subsection{Getting a DB handle from inside a Horde\_Core\_Application context}
\section{Migrations: Usage by Example}
Migrations are PHP Class files in the /migration/ sub folder of an app or library. They can be run via the horde-db-migrate script or the web UI.<br />
They allow schema and data conversions forward and back.

Schema files prepend the lowercased class name with a version:

The first version's file would be 1\_\$app\_foo (usually 1\_\$app\_base\_tables) which would contain the class named \$<a href="https://wiki.horde.org/AppFoo">AppFoo</a> (\$<a href="https://wiki.horde.org/AppBaseTables">AppBaseTables</a>) derived from Horde\_Db\_Migration\_Base. See <a href="https://github.com/horde/skeleton/blob/master/migration/1\_skeleton\_base\_tables.php">https://github.com/horde/skeleton/blob/master/migration/1\textbackslash\{\}\_skeleton\textbackslash\{\}\_base\textbackslash\{\}\_tables.php</a> for a complete example.

These classes usually contain an up() and down() method to implement upgrades or downgrades to the next/previous schema.

Errors during Schema upgrades need to be handled by the implementer himself. There is no implicit rollback of schema upgrades which fail halfway through the up() method unless you implement an appropriate try/catch block yourself.

\subsection{Adding a new table}
\subsection{Removing a table}
\subsection{Adding a new column to an existing table}
\subsection{Dropping a column from an existing table}
\subsection{Adding an index}
It's possible to use a helper function to check if an index is already set:

<pre><code>
/**
 * Helper function to test if an index already exists
 */
private function hasIndex(\$tableName, \$column)
\{
    \$indexedColumns = array();
    foreach (\$this->indexes(\$tableName) as \$index) \{
        \$indexedColumns = array\_merge(\$indexedColumns, \$index->columns);
    \}

    if (is\_array(\$column)) \{
        foreach (\$column as \$entry) \{
            return in\_array(\$entry, \$indexedColumns);
        \}
    \} else \{
        return in\_array(\$column, \$indexedColumns);
    \}
\}
</code></pre>
Add the index

<pre><code>
if (!\$this->hasIndex('my\_table', array('my\_column', 'my\_column2'))) \{
    \$this->addIndex('my\_table', array('my\_column', 'my\_column2'));
\}
</code></pre>
\subsection{Adding a unique key / unique index}
It's also possible to add unique keys or a self defined names via the options:

<pre><code>
if (!\$this->hasIndex('my\_table', 'my\_column')) \{
    \$this->addIndex('my\_table', 'my\_column', array('unique' => true));
\}
</code></pre>
\subsection{Converting for Non-Horde or Pre-H4 schemas}
Check for all table's existence before adding them

\subsection{Manipulating data while changing fields}
\end{document}
