|
NAMEDBIx::Admin::DSNManager - Manage a file of DSNs, for both testing and productionSynopsis#!/usr/bin/env perl use strict; use warnings; use DBIx::Admin::DSNManager; us Try::Tiny; # -------------------------- try { my($man1) = DBIx::Admin::DSNManager -> new ( config => {'Pg.1' => {dsn => 'dbi:Pg:dbname=test', username => 'me', active => 1} }, verbose => 1, ); my($file_name) = '/tmp/dsn.ini'; $man1 -> write($file_name); my($man2) = DBIx::Admin::DSNManager -> new ( file_name => $file_name, verbose => 1, ); $man2 -> report; } catch { print "DBIx::Admin::DSNManager died. Error: $_"; }; See scripts/synopsis.pl. DescriptionDBIx::Admin::DSNManager manages a file of DSNs, for both testing and production.The INI-style format was selected, rather than, say, using an SQLite database, so that casual users could edit the file without needing to know SQL and without having to install the command line program sqlite3. Each DSN is normally for something requiring manual preparation, such as creating the database named in the DSN. In the case of SQLite, etc, where manual intervention is not required, you can still put the DSN in dsn.ini. One major use of this module is to avoid environment variable overload, since it is common to test Perl modules by setting the env vars $DBI_DSN, $DBI_USER and $DBI_PASS. But then the problem becomes: What do you do when you want to run tests against a set of databases servers? Some modules define sets of env vars, one set per database server, with awkward and hard-to-guess names. This is messy and obscure. DBIx::Admin::DSNManager is a solution to this problem. Database CreationBy design, DBIx::Admin::DSNManager does not provide a create-database option.For database servers like Postgres, MySQL, etc, you must create users, and give them the createdb privilege. Such actions are outside the scope of this module. For database servers like SQLite, any code can create a database anyway, but you can use options in dsn.ini to indicate the DSN is inactive, or not to be used for testing. See "The Format of dsn.ini" below. Testing 'v' ProductionOf course, you may have DSNs in dsn.ini which you don't want to be used for testing.Here's a policy for handling such situations:
The Format of dsn.iniOn disk, dsn.ini is a typical INI-style file. In RAM it is a hashref of config options. E.g.:config => {'Pg.1' => {dsn => 'dbi:Pg:dbname=test', ...}, 'Pg.2' => {...} } where config is the name of the module getter/setter which provides access to the hashref.
So, a sample dsn.ini file looks like: [Pg.1] dsn=dbi:Pg:dbname=test1 username=user1 password=pass1 attributes = {AutoCommit => 1, PrintError => 0, RaiseError => 1} use_for_testing = 0 [Pg.2] dsn=dbi:Pg:dbname=test2 username=user2 password=pass2 active = 0 use_for_testing = 1 [SQLite.1] dsn=dbi:SQLite:dbname=/tmp/test.module.sqlite This file is read by Config::Tiny. Check its docs for details, but there is one thing to be aware of: Config::Tiny does not recognize comments at the ends of lines. So: key = value # A comment. sets key to 'value # A comment.', which is probably not what you intended. Constructor and InitializationCalling "new()" returns a object of type DBIx::Admin::DSNManager, or dies."new()" takes a hash of key/value pairs, some of which might be mandatory. Further, some combinations might be mandatory. The keys are listed here in alphabetical order. They are lower-case because they are (also) method names, meaning they can be called to set or get the value at any time. But a warning: In some cases, setting them after this module has used the previous value, will have no effect. All such cases are documented (or should be).
Methodsconfig([{...}])Here, the [] indicate an optional parameter.Get or set the internal config hashref holding all the DSN data. If called as config({...}), set the config hashref to the parameter. If called as config(), return the config hashref. hashref2string($hashref)Returns a string corresponding to the $hashref.{} is converted to '{}'. read($file_name)Read $file_name using Config::Tiny and set the config hashref.report([{...}])Here, the [] indicate an optional parameter.If called as $object -> report, print both $object -> file_name, and the contents of the config hashref, to STDERR. If called as $object -> report({...}), print just the contents of the hashref, to STDERR. string2hashref($s)Returns a hashref built from the string.The string is expected to be something like '{AutoCommit => 1, PrintError => 0}'. The empty string is returned as {}. validate([{...}])Here, the [] indicate an optional parameter.Validate the given or config hashref. Returns the validated hashref. If a hashref is not supplied, validate the config one. Currently, the checks are:
write([$file_name,][{...}])Here, the [] indicate an optional parameter.Write the given or config hashref to $file_name. The [] mean a parameter is optional. If called as $object -> write('dsn.ini'), write the config hashref to $file_name. If called as $object -> write('dsn.ini', {...}), write the given hashref to $file_name. If called as $object -> write({...}), write the given hashref to $object -> file_name. File::Slurp is used to write this file, since these hashes are not of type "Config::Tiny". See AlsoDBIx::Admin::CreateTable.DBIx::Admin::TableInfo. Version NumbersVersion numbers < 1.00 represent development versions. From 1.00 up, they are production versions.Repository<https://github.com/ronsavage/DBIx-Admin-DSNManager>SupportBugs should be reported via the CPAN bug tracker at<https://github.com/ronsavage/DBIx-Admin-DSNManager/issues> AuthorDBIx::Admin::DSNManager was written by Ron Savage <ron@savage.net.au> in 2010.Home page: <http://savage.net.au/index.html>. CopyrightAustralian copyright (c) 2010, Ron Savage.All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Artistic License, a copy of which is available at: http://www.opensource.org/licenses/index.html
Visit the GSP FreeBSD Man Page Interface. |