dbconfig-common

packager's reference

copyright © 2004 sean finney <seanius@debian.org>

this document is licensed under the Academic Free License, Version 2.1

abstract

this document is a packager's reference for developers who want to use dbconfig-common to help them manage databases used by the debian packages they maintain. before reading this document, it's highly recommended that you read the best practices for database applications draft, as the implementation design is based on rationale detailed in that document. if you are a developer who is curious in how dbconfig-common works or want to possibly work on dbconfig-common itself, you should read the technical reference for dbconfig-common.

table of contents

quick and dirty: what to do

there are three things you will have to do as a package maintainer if you want to use dbconfig-common: provide the database code/scripts to setup the database, source the maintainer script libraries and launch dbconfig-common. dbconfig-common will take care of everything else, include all debconf related questions, database/database-user creation, upgrade/remove/purge logic, et c. after all, the goal of dbconfig-common is to make life easier for both the local admin and the package maintainer :)

putting hooks into the maintainer scripts

in the postinst, preinst, prerm, and postrm scripts for your package, you will need to source the libraries which perform most of the work for you. if you are not currently using debconf in your package, you will be now, and the debconf libraries need to be sourced first. for example, here's an what it should look like in a postinst script for an imaginary "foo-mysql" package:


#!/bin/sh
# postinst maintainer script for foo-mysql

# source debconf stuff
. /usr/share/debconf/confmodule
# source dbconfig-common stuff
. /usr/share/dbconfig-common/dpkg/postinst.mysql 
dbc_go foo-mysql $@

# ... rest of your code ...


dbc_go is a function defined in every maintainer script hook to execute the appropriate code based on which maintainer script is being run. note that it is passed two arguments. foo-mysql, the name of the package (there's sadly no clean way to figure this out automatically), and $@ (the arguments which were passed to the maintainer script).

supplying the data/code for your databases

there are three directories in which you can place code for installing the databases of your package. the first directory is for the majority of situations, in which the database can be constructed from it's native language (SQL for mysql/postgresql, for example). the data will be fed to the underlying database using the credentials of the database user. the second directory is like the first directory, but will be run using the credentials of the database administrator. Warning: use of this second directory should only be done when there are excerpts of database code that must be run as the database administrator (such as some language constructs in postgresql) and should otherwise be avoided. the third location is for databases that require a more robust solution, in which executable shell scripts (or anything else) can be placed. these directories can be found under the following format:

where PACKAGE is the name of the package, DBTYPE is the type of data (mysql, postgresql, et c). this code will only be executed on new installs and reconfiguration of failed installs. in the case of sql databases, in the data directory you would find the simple create and insert statements needed to create tables and populate the database. you do not need to create the underlying database, only populate it. the scripts directory contains shell/perl/python/whatever scripts, which are passed the same arguments as dbc_go. if you need database connection information (username, password, etc) in your scripts, you can source the /bin/sh format package config file, or you can instruct dbconfig-common to generate one in your programming language of choice (see the advanced tips section).

if files exist in both data and scripts, they will both be executed in an unspecified order.

that's it! the rest of what needs to be done is handled by dbconfig-common, which should keep all the work (and bugs) in one place. happy packaging!

advanced usage

generating config files in other programming languages

your database application will probably require a username and password in order to function. every package that uses dbconfig-common already has a /bin/sh includable format config file, but it may be more convenient to have something in the native language of the package. for example, packaging a php/mysql web app would be a lot easier if there were already a file existing with all the information in php includable format.

using dbconfig-common, you can do this with little effort. in your postinst script, define the variable dbc_generate_include to a value that follows the form format:location where format is one of the supported output formats of dbconfig-generate-include (list them with -h) and location is the absolute location where you want your config file to go. there are also some extra variables dbc_generate_include_owner, dbc_generate_include_perms, and dbc_generate_include_args which do what you would expect them to. note: you will be responsible for removing this file in your postrm script. when your scripts are run, this environment variable will be exported to your scripts, as well as a variable dbc_config_include which has the same value, but with the leading format: stripped away for convenience.

importing dbconfig-common into an existing package

if your package is already part of debian, dbconfig-common provides some support to load pre-existing settings from a specified config by setting two variables: dbc_first_version and dbc_load_include.

dbc_load_include should be specified in the config script and be of the format format:inputfile. format is one of the languages understood by dbconfig-load-include, and inputfile is either the config file in format language, or a script file in format language that otherwise determines the values and sets them.

dbc_first_version should be specified in both the config and postinst scripts, and should contain the first version in which dbconfig-common was introduced. when the package is installed, if it is being upgraded from a version less than this value it will attempt to bootstrap itself with the values.

database changes in new versions of your package

occassionaly, the upstream authors will modify the underlying databases between versions of their software. for example, in mysql applications column names may change, move to new tables, or the data itself may need to be modified in newer upstream versions of a package.

in order to cope with this, a second set of directories exists for providing packagers ways to modify the databases during package upgrades:

where VERSION is the version at which the upgrade should be applied. when a package upgrade occurs, all instances of VERSION which are newer than the previously installed version will be applied, in order. there is also an automatically included set of safeguards and behaviour provided by dbconfig-common, so as the packager you shouldn't need to worry about most of the error-handling.

as with installation, scripts will be passed the same cmdline arguments as were passed to dbc_go.

packages that support multiple types of databases

sometimes, a particular package may support multiple database types. this is common with perl or php based web applications, which frequently use some form of database abstraction layer (pear DB for php, DBD::MySQL for perl).

dbconfig-common provides support for such applications in a relatively straightforward fashion, allowing the local admin to select which database type to use when configuring a database for a package

to take advantage of this feature, you will want to use the "generic" maintainer script hooks, and additionally hint the debconf pre-config script with the types of databases your package supports. for example, the postinst script would now look like this:


#!/bin/sh
# postinst maintainer script for foo-mysql

# source debconf stuff
. /usr/share/debconf/confmodule
# source dbconfig-common stuff
. /usr/share/dbconfig-common/dpkg/postinst 
dbc_go foo-mysql $@

# ... rest of your code ...


and the config script would contain an additional variable called "dbc_dbtypes", which is a comma-seperated list of supported database types:

#!/bin/sh
# config maintainer script for foo-mysql

# source debconf stuff
. /usr/share/debconf/confmodule
# we support mysql and pgsql
dbc_dbtypes="mysql, pgsql"
# source dbconfig-common stuff
. /usr/share/dbconfig-common/dpkg/config 
dbc_go foo-mysql $@

# ... rest of your code ...


hinting defaults and advanced control of configuration/installation

dbconfig-common has a set of pre-defined default values for most of the questions with which it prompts the user, most of which are variations on the name of the package. however, as a packager you can override some these values and set defaults that you feel are more appropriate, as well as otherwise modify the behavior of some parts of dbconfig-common.

the following table lists the variables you can hint in your config script, as well as some other variables you can use to have a finer level of control over dbconfig-common. you must use these variables exactly (and only) where directed in this table

variable name location(s) specified purpose default value
dbc_dbuser config name to use when connecting to database package name
dbc_dbname config name of database resource to which to connect package name
dbc_dbtypes config database types supported by the package empty
dbc_generate_include postinst format:outputfile pair for an extra config to be generated by dbconfig-generate-include. empty
dbc_generate_include_owner postinst set the owner:group of include files generated by dbconfig-generate-include empty
dbc_generate_include_perms postinst set the permissions of include files generated by dbconfig-generate-include empty
dbc_generate_include_args postinst arguments passed directly to dbconfig-generate-include empty
dbc_first_version config,postinst the first version in which dbconfig-common was introduced in the package empty
dbc_load_include config format:includefile pair for a config to be read in by dbconfig-load-include empty
dbc_load_include_args config arguments passed directly to dbconfig-load-include empty