Hi all,
> Jesse Erlbaum wrote
> >
> > > I'm not sure why you would like to add this [--destroy] option. What I expect
> > > krang_create_list to do, is to keep the DB in sync with list.conf.
> > > Maybe this is a biased view, but it seems natural to me. Having to
> > > specify '--destroy' might even be misleading, since in fact you don't
> > > destroy anything, you just avoid to create list_groups, lists and
> > > list_items that already exist. The only disadvantage I see is that you
> > > get new IDs for your lists.
> >
> >
> > How about only deleting list groups with the same name as lists in your
> > lists.conf file? This would preserve lists which are managed outside of
> > lists.conf (in a KDS file, for example).
>
> This sounds ok for me. Actually, while you had production in mind (KDS
> file), my view was too development-centered. Your proposal seems the
> right way to go, not a compromise, but satisfying both production and
> developments requirements. Before providing a patch, I'd like to know
> whether this approach meets consensus.
Further reflection convinced me that a more defensive approach along the
lines suggested by Jesse is more appropriate, since data for tables
'list_group', 'list' and 'list_item' may come from two sources,
lists.conf and a list KDS file.
So I'd propose two changes to bin/krang_create_lists
1. Two new cmdline options along the lines of bin/krang_createdb (the
difference being that the tables are not deleted, but cleared. This
avoids the DB cmdline options --password etc. Are there any problems
with this approach?)
* --destroy # the DB tables will only be cleared if --destroy is specified
* --no_prompt # don't ask whether we should really clear the tables
2. To avoid duplicates, we check each list_group, list and list_item
present in lists.conf to verify whether it already exists, skipping
creation if the list_group, list or list_item already exists.
Does this meet consensus?
Wishing you all a happy new year, health und success
Bodo
<patch>
Index: krang_create_lists
===================================================================
RCS file: /usr/local/krang-cvs/krang/bin/krang_create_lists,v
retrieving revision 1.7
diff -w -u -r1.7 krang_create_lists
--- krang_create_lists 13 Dec 2005 00:42:00 -0000 1.7
+++ krang_create_lists 26 Dec 2005 16:31:44 -0000
@@ -77,30 +77,53 @@
use Krang::ClassLoader 'ListGroup';
use Krang::ClassLoader 'List';
use Krang::ClassLoader 'ListItem';
+use Krang::ClassLoader 'File';
use XML::Simple;
+use Term::Prompt qw(prompt);
# For script commands
use Getopt::Long;
use Pod::Usage;
-our ($help, $man, $verbose);
+our ($help, $man, $verbose, $destroy, $no_prompt);
$verbose = 0;
pod2usage(2) unless
GetOptions(help => \$help,
man => \$man,
- 'verbose+' => \$verbose );
+ 'verbose+' => \$verbose,
+ destroy => \$destroy,
+ no_prompt => \$no_prompt,
+ );
pod2usage(1) if $help;
pod2usage(-verbose => 2) if $man;
+# Clear tables list_group, list and list_item
+if ($destroy) {
-# Find path to element library for InstanceElementSet
-my $addon = InstanceElementSet;
+ # make sure they really meant --destroy
+ unless ($no_prompt) {
+ $destroy = 0
+ unless prompt("y", "This program will delete the Krang database tables\n"
+ ."'list_group', 'list' and 'list_item' for KRANG_INSTANCE '"
+ .InstanceElementSet
+ ."' before creating the lists specified in '"
+ .pkg('File')->find(catfile('element_lib', InstanceElementSet, 'lists.conf'))
+ ."'\n\nAre you sure you want to proceed?", "y/n", "y");
+ }
-# Are we in the new /krang/addons/ directory, or legacy /krang/element_lib/?
-my $element_lib_dir = catdir(KrangRoot, 'addons', $addon, 'element_lib', $addon);
-$element_lib_dir = catdir(KrangRoot, 'element_lib', $addon) unless (-d $element_lib_dir);
-die("Can't find element_lib/$addon/ in krang/addons/ or krang/element_lib/") unless (-d $element_lib_dir);
+ if ($destroy) {
+ print STDERR "Deleting DB tables 'list_group', 'list' and 'list_item'\n";
+ $_->delete for pkg('ListGroup')->find();
+ $_->delete for pkg('List')->find();
+ $_->delete for pkg('ListItem')->find();
+ }
+}
+
+# Find path to element library for InstanceElementSet
+my $elementset = InstanceElementSet;
+my $element_lib_dir = pkg('File')->find(catdir('element_lib', $elementset));
+die("Can't find element_lib/$elementset/ in krang/addons/ or krang/element_lib/") unless (-d $element_lib_dir);
# Location of our file, if we have one.
my $list_conf_file = catfile($element_lib_dir, 'lists.conf');
@@ -114,26 +137,41 @@
foreach my $list_group (@{$config->{list_group}}) {
my $list_group_name = $list_group->{list_group_name};
+ my ($lg) = pkg('ListGroup')->find(name => $list_group_name);
+
+ if ($lg) {
+ print STDERR "ListGroup '$list_group_name' already exists\n" if $verbose;
+ } else {
print STDERR "Adding ListGroup '$list_group_name'\n" if $verbose;
- my $lg = pkg('ListGroup')->new( name => $list_group_name, description => $list_group->{description} );
+ $lg = pkg('ListGroup')->new( name => $list_group_name, description => $list_group->{description} );
$lg->save;
+ }
my $prev_list_id;
foreach my $list (@{$list_group->{list}}) {
my %params;
$params{name} = $list->{list_name};
$params{list_group_id} = $lg->list_group_id;
$params{parent_list_id} = $prev_list_id if $prev_list_id;
+
+ my ($new_list) = pkg('List')->find(%params);
+
+ if ($new_list) {
+ print STDERR " List '$params{name}' already exists\n" if $verbose;
+ } else {
print STDERR " Adding List '". $params{name} ."'\n" if $verbose;
- my $new_list = pkg('List')->new( %params );
+ $new_list = pkg('List')->new( %params );
$new_list->save;
+ }
# Iterate through items
my $order = 1;
foreach my $item (@{$list->{list_item}}) {
my $item_name = $item->{item_name};
my $parent_item_name = $item->{parent_item_name};
- print STDERR " Adding item '$item_name'". ($parent_item_name ? " (parent $parent_item_name)" : "") ."\n"
- if $verbose > 1;
+
+ # Set up find params
+ my %existing_item = ( list_id => $new_list->list_id,
+ data => $item_name );
# Set up new item
my %item_params = ( list => $new_list,
@@ -144,14 +182,29 @@
if ($parent_item_name) {
my ($parent_item) = pkg('ListItem')->find( list_id => $prev_list_id,
data => $parent_item_name );
- die ("Cannot find parent item '$parent_item_name' in list_id '$prev_list_id'") unless $parent_item;
+ die ("Cannot find parent item '$parent_item_name' in list_id '$prev_list_id'")
+ unless $parent_item;
$item_params{parent_list_item} = $parent_item;
+ $existing_item{parent_list_item_id} = $parent_item->list_item_id;
}
- # Create and save
- my $new_item = pkg('ListItem')->new(%item_params);
+ # Create and save unless exists
+ my ($new_item) = pkg('ListItem')->find(%existing_item);
+
+ if ($new_item) {
+ print STDERR " Item '$item_name'". ($parent_item_name
+ ? " (parent $parent_item_name)"
+ : "") . " already exists\n"
+ if $verbose > 1;
+ } else {
+ $new_item = pkg('ListItem')->new(%item_params);
$new_item->save();
+ print STDERR " Adding item '$item_name'". ($parent_item_name
+ ? " (parent $parent_item_name)"
+ : "") ."\n"
+ if $verbose > 1;
+ }
}
# Set up previous list ID for next list
</patch>
|