<<

Bugzilla::DB::Schema

NAME

Bugzilla::DB::Schema - Abstract database schema for Bugzilla

SYNOPSIS

  # Obtain MySQL database schema.
  # Do not do this. Use Bugzilla::DB instead.
  use Bugzilla::DB::Schema;
  my $mysql_schema = new Bugzilla::DB::Schema('Mysql');

  # Recommended way to obtain database schema.
  use Bugzilla::DB;
  my $dbh = Bugzilla->dbh;
  my $schema = $dbh->_bz_schema();

  # Get the list of tables in the Bugzilla database.
  my @tables = $schema->get_table_list();

  # Get the SQL statements need to create the bugs table.
  my @statements = $schema->get_table_ddl('bugs');

  # Get the database-specific SQL data type used to implement
  # the abstract data type INT1.
  my $db_specific_type = $schema->sql_type('INT1');

DESCRIPTION

This module implements an object-oriented, abstract database schema. It should be considered package-private to the Bugzilla::DB module. That means that CGI scripts should never call any function in this module directly, but should instead rely on methods provided by Bugzilla::DB.

NEW TO SCHEMA.PM?

If this is your first time looking at Schema.pm, especially if you are making changes to the database, please take a look at https://www.bugzilla.org/docs/developer.html#sql-schema to learn more about how this integrates into the rest of Bugzilla.

CONSTANTS

SCHEMA_VERSION

The 'version' of the internal schema structure. This version number is incremented every time the the fundamental structure of Schema internals changes.

This is NOT changed every time a table or a column is added. This number is incremented only if the internal structures of this Schema would be incompatible with the internal structures of a previous Schema version.

In general, unless you are messing around with serialization and deserialization of the schema, you don't need to worry about this constant.

ABSTRACT_SCHEMA

The abstract database schema structure consists of a hash reference in which each key is the name of a table in the Bugzilla database.

The value for each key is a hash reference containing the keys FIELDS and INDEXES which in turn point to array references containing information on the table's fields and indexes.

A field hash reference should must contain the key TYPE. Optional field keys include PRIMARYKEY, NOTNULL, and DEFAULT.

The INDEXES array reference contains index names and information regarding the index. If the index name points to an array reference, then the index is a regular index and the array contains the indexed columns. If the index name points to a hash reference, then the hash must contain the key FIELDS. It may also contain the key TYPE, which can be used to specify the type of index such as UNIQUE or FULLTEXT.

Referential Integrity

Bugzilla::DB::Schema supports "foreign keys", a way of saying that "Column X may only contain values from Column Y in Table Z". For example, in Bugzilla, bugs.resolution should only contain values from the resolution.values field.

It does this by adding an additional item to a column, called REFERENCES. This is a hash with the following members:

TABLE

The table the foreign key points at

COLUMN

The column pointed at in that table.

DELETE

What to do if the row in the parent table is deleted. Choices are RESTRICT, CASCADE, or SET NULL.

RESTRICT means the deletion of the row in the parent table will be forbidden by the database if there is a row in this table that still refers to it. This is the default, if you don't specify DELETE.

CASCADE means that this row will be deleted along with that row.

SET NULL means that the column will be set to NULL when the parent row is deleted. Note that this is only valid if the column can actually be set to NULL. (That is, the column isn't NOT NULL.)

UPDATE

What to do if the value in the parent table is updated. You can set this to CASCADE or RESTRICT, which mean the same thing as they do for "DELETE". This variable defaults to CASCADE, which means "also update this column in this table."

METHODS

Note: Methods which can be implemented generically for all DBs are implemented in this module. If needed, they can be overridden with DB-specific code in a subclass. Methods which are prefixed with _ are considered protected. Subclasses may override these methods, but other modules should not invoke these methods directly.

new
 Description: Public constructor method used to instantiate objects of this
              class. However, it also can be used as a factory method to
              instantiate database-specific subclasses when an optional
              driver argument is supplied.
 Parameters:  $driver (optional) - Used to specify the type of database.
              This routine C<die>s if no subclass is found for the specified
              driver.
              $schema (optional) - A reference to a hash. Callers external
                  to this package should never use this parameter.
 Returns:     new instance of the Schema class or a database-specific subclass
_initialize
 Description: Protected method that initializes an object after
              instantiation with the abstract schema. All subclasses should
              override this method. The typical subclass implementation
              should first call the C<_initialize> method of the superclass,
              then do any database-specific initialization (especially
              define the database-specific implementation of the all
              abstract data types), and then call the C<_adjust_schema>
              method.
 Parameters:  $abstract_schema (optional) - A reference to a hash. If 
                  provided, this hash will be used as the internal
                  representation of the abstract schema instead of our
                  default abstract schema. This is intended for internal 
                  use only by deserialize_abstract.
 Returns:     the instance of the Schema class
_adjust_schema
 Description: Protected method that alters the abstract schema at
              instantiation-time to be database-specific. It is a generic
              enough routine that it can be defined here in the base class.
              It takes the abstract schema and replaces the abstract data
              types with database-specific data types.
 Parameters:  none
 Returns:     the instance of the Schema class
get_type_ddl
Description

Public method to convert abstract (database-generic) field specifiers to database-specific data types suitable for use in a CREATE TABLE or ALTER TABLE SQL statment. If no database-specific field type has been defined for the given field type, then it will just return the same field type.

Parameters
$def - A reference to a hash of a field containing the following keys: TYPE (required), NOTNULL (optional), DEFAULT (optional), PRIMARYKEY (optional), REFERENCES (optional)
Returns

A DDL string suitable for describing a field in a CREATE TABLE or ALTER TABLE SQL statement

_get_fk_ddl
Description

Protected method. Translates the REFERENCES item of a column into SQL.

Params
$table - The name of the table the reference is from. =item $column - The name of the column the reference is from =item $references - The REFERENCES hashref from a column.
Returns

SQL for to define the foreign key, or an empty string if $references is undefined.

convert_type

Converts a TYPE from the "ABSTRACT_SCHEMA" format into the real SQL type.

get_column($table, $column)
 Description: Public method to get the abstract definition of a column.
 Parameters:  $table - the table name
              $column - a column in the table
 Returns:     a hashref containing information about the column, including its
              type (C<TYPE>), whether or not it can be null (C<NOTNULL>),
              its default value if it has one (C<DEFAULT), etc.
              Returns undef if the table or column does not exist.
get_table_list
 Description: Public method for discovering what tables should exist in the
              Bugzilla database.

 Parameters:  none

 Returns:     An array of table names, in alphabetical order.
get_table_columns
 Description: Public method for discovering what columns are in a given
              table in the Bugzilla database.
 Parameters:  $table - the table name
 Returns:     array of column names
get_table_ddl
 Description: Public method to generate the SQL statements needed to create
              the a given table and its indexes in the Bugzilla database.
              Subclasses may override or extend this method, if needed, but
              subclasses probably should override C<_get_create_table_ddl>
              or C<_get_create_index_ddl> instead.
 Parameters:  $table - the table name
 Returns:     an array of strings containing SQL statements
_get_create_table_ddl
 Description: Protected method to generate the "create table" SQL statement
              for a given table.
 Parameters:  $table - the table name
 Returns:     a string containing the DDL statement for the specified table
_get_create_index_ddl
 Description: Protected method to generate a "create index" SQL statement
              for a given table and index.
 Parameters:  $table_name - the name of the table
              $index_name - the name of the index
              $index_fields - a reference to an array of field names
              $index_type (optional) - specify type of index (e.g., UNIQUE)
 Returns:     a string containing the DDL statement
get_add_column_ddl($table, $column, \%definition, $init_value)
 Description: Generate SQL to add a column to a table.
 Params:      $table - The table containing the column.
              $column - The name of the column being added.
              \%definition - The new definition for the column,
                  in standard C<ABSTRACT_SCHEMA> format.
              $init_value - (optional) An initial value to set 
                            the column to. Should already be SQL-quoted
                            if necessary.
 Returns:     An array of SQL statements.
get_add_index_ddl
 Description: Gets SQL for creating an index.
              NOTE: Subclasses should not override this function. Instead,
              if they need to specify a custom CREATE INDEX statement, 
              they should override C<_get_create_index_ddl>
 Params:      $table - The name of the table the index will be on.
              $name  - The name of the new index.
              $definition - An index definition. Either a hashref 
                            with FIELDS and TYPE or an arrayref 
                            containing a list of columns.
 Returns:     An array of SQL statements that will create the 
              requested index.
get_alter_column_ddl($table, $column, \%definition)
 Description: Generate SQL to alter a column in a table.
              The column that you are altering must exist,
              and the table that it lives in must exist.
 Params:      $table - The table containing the column.
              $column - The name of the column being changed.
              \%definition - The new definition for the column,
                  in standard C<ABSTRACT_SCHEMA> format.
              $set_nulls_to - A value to set NULL values to, if
                  your new definition is NOT NULL and contains
                  no DEFAULT, and when there is a possibility
                  that the column could contain NULLs. $set_nulls_to
                  should be already SQL-quoted if necessary.
 Returns:     An array of SQL statements.
get_drop_index_ddl($table, $name)
 Description: Generates SQL statements to drop an index.
 Params:      $table - The table the index is on.
              $name  - The name of the index being dropped.
 Returns:     An array of SQL statements.
get_drop_column_ddl($table, $column)
 Description: Generate SQL to drop a column from a table.
 Params:      $table - The table containing the column.
              $column - The name of the column being dropped.
 Returns:     An array of SQL statements.
get_drop_table_ddl($table)
 Description: Generate SQL to drop a table from the database.
 Params:      $table - The name of the table to drop.
 Returns:     An array of SQL statements.
get_rename_column_ddl($table, $old_name, $new_name)
 Description: Generate SQL to change the name of a column in a table.
              NOTE: ANSI SQL contains no simple way to rename a column,
                    so this function is ABSTRACT and must be implemented
                    by subclasses.
 Params:      $table - The table containing the column to be renamed.
              $old_name - The name of the column being renamed.
              $new_name - The name the column is changing to.
 Returns:     An array of SQL statements.
get_rename_table_sql
Description

Gets SQL to rename a table in the database.

Params
$old_name - The current name of the table.
$new_name - The new name of the table.
Returns: An array of SQL statements to rename a table.
delete_table($name)
 Description: Deletes a table from this Schema object.
              Dies if you try to delete a table that doesn't exist.
 Params:      $name - The name of the table to delete.
 Returns:     nothing
get_column_abstract($table, $column)
 Description: A column definition from the abstract internal schema.
              cross-database format.
 Params:      $table - The name of the table
              $column - The name of the column that you want
 Returns:     A hash reference. For the format, see the docs for
              C<ABSTRACT_SCHEMA>.
              Returns undef if the column or table does not exist.
get_indexes_on_column_abstract($table, $column)
 Description: Gets a list of indexes that are on a given column.
 Params:      $table - The table the column is on.
              $column - The name of the column.
 Returns:     Indexes in the standard format of an INDEX
              entry on a table. That is, key-value pairs
              where the key is the index name and the value
              is the index definition.
              If there are no indexes on that column, we return
              undef.
get_index_abstract($table, $index)
 Description: Returns an index definition from the internal abstract schema.
 Params:      $table - The table the index is on.
              $index - The name of the index.
 Returns:     A hash reference representing an index definition.
              See the C<ABSTRACT_SCHEMA> docs for details.
              Returns undef if the index does not exist.
get_table_abstract($table)
 Description: Gets the abstract definition for a table in this Schema
              object.
 Params:      $table - The name of the table you want a definition for.
 Returns:     An abstract table definition, or undef if the table doesn't
              exist.
add_table($name, \%definition)
 Description: Creates a new table in this Schema object.
              If you do not specify a definition, we will
              simply create an empty table.
 Params:      $name - The name for the new table.
              \%definition (optional) - An abstract definition for
                  the new table.
 Returns:     nothing
rename_table

Renames a table from $old_name to $new_name in this Schema object.

delete_column($table, $column)
 Description: Deletes a column from this Schema object.
 Params:      $table - Name of the table that the column is in.
                       The table must exist, or we will fail.
              $column  - Name of the column to delete.
 Returns:     nothing
rename_column($table, $old_name, $new_name)
 Description: Renames a column on a table in the Schema object.
              The column that you are renaming must exist.
 Params:      $table - The table the column is on.
              $old_name - The current name of the column.
              $new_name - The new name of hte column.
 Returns:     nothing
set_column($table, $column, \%new_def)
 Description: Changes the definition of a column in this Schema object.
              If the column doesn't exist, it will be added.
              The table that you specify must already exist in the Schema.
              NOTE: This does not affect the database on the disk.
              Use the C<Bugzilla::DB> "Schema Modification Methods"
              if you want to do that.
 Params:      $table - The name of the table that the column is on.
              $column - The name of the column.
              \%new_def - The new definition for the column, in 
                  C<ABSTRACT_SCHEMA> format.
 Returns:     nothing
set_index($table, $name, $definition)
 Description: Changes the definition of an index in this Schema object.
              If the index doesn't exist, it will be added.
              The table that you specify must already exist in the Schema.
              NOTE: This does not affect the database on the disk.
              Use the C<Bugzilla::DB> "Schema Modification Methods"
              if you want to do that.
 Params:      $table      - The table the index is on.
              $name       - The name of the index.
              $definition - A hashref or an arrayref. An index 
                            definition in C<ABSTRACT_SCHEMA> format.
 Returns:     nothing
delete_index($table, $name)
 Description: Removes an index definition from this Schema object.
              If the index doesn't exist, we will fail.
              The table that you specify must exist in the Schema.
              NOTE: This does not affect the database on the disk.
              Use the C<Bugzilla::DB> "Schema Modification Methods"
              if you want to do that.
 Params:      $table - The table the index is on.
              $name  - The name of the index that we're removing.
 Returns:     nothing
columns_equal($col_one, $col_two)
 Description: Tells you if two columns have entirely identical definitions.
              The TYPE field's value will be compared case-insensitive.
              However, all other fields will be case-sensitive.
 Params:      $col_one, $col_two - The columns to compare. Hash 
                  references, in C<ABSTRACT_SCHEMA> format.
 Returns:     C<1> if the columns are identical, C<0> if they are not.

SERIALIZATION/DESERIALIZATION

serialize_abstract()
 Description: Serializes the "abstract" schema into a format
              that deserialize_abstract() can read in. This is
              a method, called on a Schema instance.
 Parameters:  none
 Returns:     A scalar containing the serialized, abstract schema.
              Do not attempt to manipulate this data directly,
              as the format may change at any time in the future.
              The only thing you should do with the returned value
              is either store it somewhere (coupled with appropriate 
              SCHEMA_VERSION) or deserialize it.
deserialize_abstract($serialized, $version)
 Description: Used for when you've read a serialized Schema off the disk,
              and you want a Schema object that represents that data.
 Params:      $serialized - scalar. The serialized data.
              $version - A number in the format X.YZ. The "version"
                  of the Schema that did the serialization.
                  See the docs for C<SCHEMA_VERSION> for more details.
 Returns:     A Schema object. It will have the methods of (and work 
              in the same fashion as) the current version of Schema. 
              However, it will represent the serialized data instead of
              ABSTRACT_SCHEMA.

CLASS METHODS

These methods are generally called on the class instead of on a specific object.

get_empty_schema()
 Description: Returns a Schema that has no tables. In effect, this
              Schema is totally "empty."
 Params:      none
 Returns:     A "empty" Schema object.

ABSTRACT DATA TYPES

The size and range data provided here is only intended as a guide. See your database's Bugzilla module (in this directory) for the most up-to-date values for these data types. The following abstract data types are used:

BOOLEAN

Logical value 0 or 1 where 1 is true, 0 is false.

INT1

Integer values (-128 - 127 or 0 - 255 unsigned).

INT2

Integer values (-32,768 - 32767 or 0 - 65,535 unsigned).

INT3

Integer values (-8,388,608 - 8,388,607 or 0 - 16,777,215 unsigned)

INT4

Integer values (-2,147,483,648 - 2,147,483,647 or 0 - 4,294,967,295 unsigned)

SMALLSERIAL

An auto-increment "INT1"

MEDIUMSERIAL

An auto-increment "INT3"

INTSERIAL

An auto-increment "INT4"

TINYTEXT

Variable length string of characters up to 255 (2^8 - 1) characters wide.

MEDIUMTEXT

Variable length string of characters up to 4000 characters wide. May be longer on some databases.

LONGTEXT

Variable length string of characters up to 16M (2^24 - 1) characters wide.

LONGBLOB

Variable length string of binary data up to 4M (2^32 - 1) bytes wide

DATETIME

DATETIME support varies from database to database, however, it's generally safe to say that DATETIME entries support all date/time combinations greater than 1900-01-01 00:00:00. Note that the format used is YYYY-MM-DD hh:mm:ss to be safe, though it's possible that your database may not require leading zeros. For greatest compatibility, however, please make sure dates are formatted as above for queries to guarantee consistent results.

Database-specific subclasses should define the implementation for these data types as a hash reference stored internally in the schema object as db_specific. This is typically done in overridden _initialize method.

The following abstract boolean values should also be defined on a database-specific basis:

TRUE
FALSE

SEE ALSO

Bugzilla::DB

https://www.bugzilla.org/docs/developer.html#sql-schema

<<