The DatabaseSchema component provides a unified data structure to describe
relational databases. DatabaseSchema can read and write the
structure from and to several databases (such as MySQL, SQLite and Oracle) and
also read and write in two web language files (XML and PHP).

This contains the structure of a database. A ezcDbSchema object can be
created from files containing a description or directly from the database. A
ezcDbSchema object can also be written to a file in different formats and converted
to SQL DDL. It is also possible to create a database structure directly from
an ezcDbSchema object.

This provides one method to validate a ezcDbSchema class. It
checks whether indexes are on existing columns and if the types used are
correct. It provides an easy-to-use API so that other validation checks can
be added.

Databases can be generated from two different types of sources. The first
source is the file, where there are multiple format handlers available. There
is a format for reading and writing XML files, and one for reading and writing files
that store the ezcDbSchema structure in a PHP array.

This example shows how to create a database schema from the two different
sources. With the first parameter in ezcDbSchema::createFromFile(), you define
which type to use ('xml' or 'array'). The second parameter is the
file that you are reading the schema definition from.

Once a ezcDbSchema object has been created, its tables and fields can be
manipulated in different ways. Tables can be created, edited and deleted.
Fields can be added to tables, edited and deleted. See the example below:

Once a ezcDbSchema object has been created, it can be manipulated in any way.
Then, there are different possible ways to save the
database schema. The ezcDbSchema->writeToFile() method stores an ezcDbSchema
object to a file on disk. As stated, you can use the XML and PHP array formats.
This is shown in the first part of this example:

The second and third methods both deal directly with a database. The second
method in the example uses the database connection $db for creating a schema.
This method only saves to the tables in the schema definition. The schemas that
are defined will be overwritten.

The third method merely uses the database connection to determine which SQL DLL
dialect to use for the query to create the schema in the database.

First, we create the two different schemas: one from the database, and one from
a file. In line 10, we utilize the ezcDbSchemaComparator::compareSchemas()
function to compare the two schemas. This process returns the differences as
the $diffSchema variable.

You can perform multiple actions with the generated differences. These three
methods are explained in the inline comments in the above example.

The last feature that the DatabaseSchema component offers is the validation of
schemas. The ezcDbSchemaValidator::validate() method accepts an ezcDbSchema
object and returns an array of strings describing potential problems with the
schema. This is shown in the following example: