An unfinished system to manage all your paper documentation in an easy way.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

dev_cleanup_db.pl 1.2KB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #!/usr/bin/perl
  2. use strict;
  3. use JSON;
  4. use DBI;
  5. use warnings;
  6. my $conf = load_conf("../etc/autodoc.json");
  7. my $dbh = sqlconnect($conf->{sql});
  8. my @del = (
  9. 'pages',
  10. 'documents',
  11. 'tags',
  12. 'words'
  13. );
  14. foreach ( @del ) {
  15. print "DELETE FROM $_\n";
  16. sqlquery($dbh, "DELETE FROM $_");
  17. }
  18. my $q = sqlquery($dbh, "SHOW TABLE STATUS");
  19. while(my $h = $q->fetchrow_hashref()) {
  20. print "="x80 . "\n";
  21. foreach my $name ( sort keys %{$h} ) {
  22. next if $name !~ /^(Name|Rows)$/;
  23. printf("%10s: %s\n",
  24. $name,
  25. defined $h->{$name} ? $h->{$name} : 'NULL'
  26. );
  27. }
  28. }
  29. sub load_conf {
  30. my($file) = @_;
  31. my $x='';
  32. open(F,"$file") || fatal_api_error(500,"Failed to load configuration file");
  33. while(<F>) { $x.=$_; }
  34. close(F);
  35. return from_json($x);
  36. }
  37. sub sqlconnect {
  38. my($sql) = @_;
  39. my $dsn = "DBI:mysql:database=$sql->{base};host=$sql->{host}";
  40. my $dbh = DBI->connect($dsn, $sql->{user}, $sql->{pass}) || \\
  41. print STDERR "Failed to connect to database\n";
  42. return $dbh;
  43. }
  44. sub sqlquery {
  45. my $dbh = shift;
  46. my $query = shift;
  47. my @args = @_;
  48. my $sth = $dbh->prepare($query) || print STDERR $dbh->error();
  49. $sth->execute(@args) || print STDERR $sth->error();
  50. return $sth;
  51. }