File: db_admin.rules

package info (click to toggle)
polymake 3.2r4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 22,564 kB
  • sloc: cpp: 153,464; perl: 40,590; ansic: 2,829; java: 2,654; python: 589; sh: 219; xml: 117; makefile: 63
file content (170 lines) | stat: -rw-r--r-- 6,392 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
#  Copyright (c) 1997-2018
#  Ewgenij Gawrilow, Michael Joswig (Technische Universitaet Berlin, Germany)
#  http://www.polymake.org
#
#  This program is free software; you can redistribute it and/or modify it
#  under the terms of the GNU General Public License as published by the
#  Free Software Foundation; either version 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#-------------------------------------------------------------------------------
#
#  This file is part of the polymake database interface polyDB.
#
#   @author Silke Horn, Andreas Paffenholz
#   http://solros.de
#   http://www.mathematik.tu-darmstadt.de/~paffenholz
#

use namespaces qw(+ PolyDB);

CREDIT polyDB

declare $test_col_re = qr{test-.*?-\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}}xo;


# @category Database Admin
# This function cleans up everything (failed) testcases may have left in the Test database.
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option String db database name
# @option String username database username
# @option String password database password
# @option String col column id to clean up
function db_clean_up_test ({local=>0, db=>"Test", username=>$PolyDB::default::db_user, password=>$PolyDB::default::db_pwd, collection=>undef}) {
    my ($options) = @_;

    my $client = Client::get_client($options);
    my $db = $client->get_database($options->{db});

    my @cols = defined($options->{collection}) ? ($options->{collection}) : $db->collection_names;
    my $i = 0;

    # drop collections that were created by testcases
    foreach (@cols) {
        if ($_ =~ $test_col_re) {
            $db->get_collection($_)->drop();
        }
    }

    # remove type information created by testcases
    my $collection = $db->get_collection("$PolyDB::default::db_type_information");
    $collection->delete_one({"col" => defined($options->{collection}) ? $options->{collection} : $test_col_re});
}

# @category Database Admin
# This function cleans up everything (failed) testcases may have left in the Test database.
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option String db database name
# @option String username database username
# @option String password database password
# @option String col column id to clean up
function db_drop_test_db ({local=>0, db=>"Test", username=>$PolyDB::default::db_user, password=>$PolyDB::default::db_pwd}){
    my ($options) = @_;

    my $client = Client::get_client($options);
    my $db = $client->get_database($options->{db})->drop();
}

# @category Database Admin
# This function cleans up everything (failed) testcases may have left in the Test database.
# @option Bool local set to 1 if you want to use a local database (on localhost), default 0
# @option String db database name
# @option String username database username
# @option String password database password
# @option String col column id to clean up
function db_drop_test_collection ({local=>0, db=>undef, collection=>undef, username=>$PolyDB::default::db_user, password=>$PolyDB::default::db_pwd}){
   my ($options) = @_;

   my $client = Client::get_client($options);
   if ( defined($options->{db}) && defined($options->{collection}) ) {
      my $db = $client->get_database($options->{db});
      $db->get_collection($options->{collection})->drop();
   }
}


function db_remove_db_info ({local=>0, info_db=>"polyDB_testing", db=>undef, id=>undef, polyDB_version=>undef,  username=>$PolyDB::default::db_user, password=>$PolyDB::default::db_pwd }) {
   my ($options) = @_;

   my $client = Client::get_client($options->{local},"","");
   my $db = $client->get_database($options->{info_db});
   my $collection = $db->get_collection($PolyDB::default::db_info_collection);

   my %db_query = ('$or' => [{"users"=>$options->{username}}, {"users"=>"polymake"}] );
   $db_query{"col"} = "db_info";
   if (defined($options->{db})) {
      $db_query{"db"} = $options->{db};
   }
   if (defined($options->{id})) {
      $db_query{"_id"} = $options->{id};
   }
   if (defined($options->{polyDB_version})) {
      $db_query{"polyDB_version"} = $options->{polyDB_version};
   }

   my $count = $collection->count(\%db_query);
   if ( $count == 0 ) {
      die "no such entry in info database\n";
   }
   if ( $count > 1 ) {
      die "entry not uniquely defined: count=$count\n";
   }

   $collection->delete_one(\%db_query);
}

function db_remove_collection_info ({local=>0, info_db=>"polyDB_testing", db=>undef, id=>undef, polyDB_version=>undef, collection=>"undef", username=>$PolyDB::default::db_user, password=>$PolyDB::default::db_pwd }) {
   my ($options) = @_;

   my $client = Client::get_client($options->{local},"","");
   my $db = $client->get_database($options->{info_db});
   my $collection = $db->get_collection($PolyDB::default::db_info_collection);

   my %db_query = ();
   if (defined($options->{db})) {
      $db_query{"db"} = $options->{db};
   }
   if (defined($options->{collection})) {
      $db_query{"col"} = $options->{collection};
   }
   if (defined($options->{id})) {
      $db_query{"_id"} = $options->{id};
   }
   if (defined($options->{polyDB_version})) {
      $db_query{"polyDB_version"} = $options->{polyDB_version};
   }

   my $count = $collection->count(\%db_query);
   if ( $count == 0 ) {
      die "no such entry in info database\n";
   }
   if ( $count > 1 ) {
      die "entry not uniquely defined: count=$count\n";
   }

   $collection->delete_one(\%db_query);
}


function read_json_for_id ( { local=>0, db=>"Test", username=>$PolyDB::default::db_user, password=>$PolyDB::default::db_pwd, collection=>undef, id=>undef } ) {
   my $options = shift;

   my $client = Client::get_client($options);
   my $db = $client->get_database($options->{db});
   my $query = { "_id" => $options->{"id"} };
   my $collection = $db->get_collection($options->{"collection"});
   my $output = $collection->find_one($query)
      // die "No such object in the database.\n";
   return $output;
}


# Local Variables:
# mode: perl
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: