File: cmd_db.cc

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (683 lines) | stat: -rw-r--r-- 19,606 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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
// Copyright (C) 2002 Graydon Hoare <graydon@pobox.com>
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#include "base.hh"
#include <iostream>
#include <utility>

#include "charset.hh"
#include "cmd.hh"
#include "revision.hh"
#include "roster.hh"
#include "constants.hh"
#include "app_state.hh"
#include "database.hh"
#include "project.hh"
#include "keys.hh"
#include "key_store.hh"
#include "work.hh"
#include "rev_height.hh"
#include "transforms.hh"
#include "ui.hh"
#include "vocab_cast.hh"
#include "migration.hh"

using std::cin;
using std::cout;
using std::make_pair;
using std::pair;
using std::set;
using std::string;
using std::vector;

CMD_GROUP(db, "db", "", CMD_REF(database),
          N_("Deals with the database"),
          "");

CMD(db_init, "init", "", CMD_REF(db), "",
    N_("Initializes a database"),
    N_("Creates a new database file and initializes it."),
    options::opts::none)
{
  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  database db(app);
  db.initialize();
}

CMD(db_info, "info", "", CMD_REF(db), "",
    N_("Shows information about the database"),
    "",
    options::opts::full)
{
  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  database db(app);
  db.info(cout, app.opts.full);
}

CMD(db_version, "version", "", CMD_REF(db), "",
    N_("Shows the database's version"),
    "",
    options::opts::none)
{
  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  database db(app);
  db.version(cout);
}

CMD(db_fix_certs, "fix_certs", "", CMD_REF(db), "",
    N_("Attempt to fix bad certs"),
    N_("Older monotone versions could sometimes associate certs with "
       "the wrong key. This fixes such certs if you have the correct key, "
       "and can optionally drop any certs that you don't have the "
       "correct key for. This should only be needed if you had such certs "
       "in your db when upgrading from 0.44 or earlier, or if you loaded "
       "such certs with 'mtn read'."),
    options::opts::drop_bad_certs)
{
  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  database db(app);
  db.fix_bad_certs(app.opts.drop_bad_certs);
}

CMD(db_dump, "dump", "", CMD_REF(db), "",
    N_("Dumps the contents of the database"),
    N_("Generates a list of SQL instructions that represent the whole "
       "contents of the database.  The resulting output is useful to later "
       "restore the database from a text file that serves as a backup."),
    options::opts::none)
{
  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  database db(app);
  db.dump(cout);
}

CMD(db_load, "load", "", CMD_REF(db), "",
    N_("Loads the contents of the database"),
    N_("Reads a list of SQL instructions that regenerate the contents of "
       "the database.  This is supposed to be used in conjunction with the "
       "output generated by the 'dump' command."),
    options::opts::none)
{
  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  database db(app);
  db.load(cin);
}

CMD(db_migrate, "migrate", "", CMD_REF(db), "",
    N_("Migrates the database to a newer schema"),
    N_("Updates the database's internal schema to the most recent one.  "
       "Needed to automatically resolve incompatibilities that may be "
       "introduced in newer versions of monotone."),
    options::opts::none)
{
  key_store keys(app);

  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  migration_status mstat;
  {
    database db(app);
    db.migrate(keys, mstat);
    database::reset_cache();
  }

  if (mstat.need_regen())
    {
      database db(app);
      regenerate_caches(db);
    }

  if (mstat.need_flag_day())
    {
      P(F("NOTE: because this database was last used by a rather old version\n"
          "of monotone, you're not done yet.  If you're a project leader, then\n"
          "see the file UPGRADE for instructions on running '%s db %s'")
        % prog_name % mstat.flag_day_name());
    }
}

CMD(db_execute, "execute", "", CMD_REF(db), "",
    N_("Executes an SQL command on the database"),
    N_("Directly executes the given SQL command on the database"),
    options::opts::none)
{
  if (args.size() != 1)
    throw usage(execid);

  database db(app);
  db.debug(idx(args, 0)(), cout);
}

CMD(db_kill_rev_locally, "kill_rev_locally", "", CMD_REF(db), "ID",
    N_("Kills a revision from the local database"),
    "",
    options::opts::none)
{
  if (args.size() != 1)
    throw usage(execid);

  revision_id revid;

  database db(app);
  project_t project(db);
  complete(app.opts, app.lua, project, idx(args, 0)(), revid);

  // Check that the revision does not have any children
  std::set<revision_id> children;
  db.get_revision_children(revid, children);
  E(!children.size(), origin::user,
    F("revision %s already has children. We cannot kill it.")
    % revid);

  // If we're executing this in a workspace, check if the workspace parent
  // revision is the one to kill. If so, write out the changes made in this
  // particular revision to _MTN/revision to allow the user redo his (fixed)
  // commit afterwards. Of course we can't do this at all if
  //
  // a) the user is currently not inside a workspace
  // b) the user has updated the current workspace to another revision already
  //    thus the working revision is no longer based on the revision we're
  //    trying to kill
  // c) there are uncomitted changes in the working revision of this workspace.
  //    this *eventually* could be handled with a workspace merge scenario, but
  //    is left out for now
  if (workspace::found)
    {
      workspace work(app);
      revision_t old_work_rev;
      work.get_work_rev(old_work_rev);

      for (edge_map::const_iterator i = old_work_rev.edges.begin();
           i != old_work_rev.edges.end(); i++)
        {
          if (edge_old_revision(i) != revid)
            continue;

          E(!work.has_changes(db), origin::user,
            F("Cannot kill revision %s,\n"
              "because it would leave the current workspace in an invalid\n"
              "state, from which monotone cannot recover automatically since\n"
              "the workspace contains uncommitted changes.\n"
              "Consider updating your workspace to another revision first,\n"
              "before you try to kill this revision again.")
              % revid);

          P(F("applying changes from %s on the current workspace")
            % revid);

          revision_t new_work_rev;
          db.get_revision(revid, new_work_rev);
          new_work_rev.made_for = made_for_workspace;
          work.put_work_rev(new_work_rev);
          work.maybe_update_inodeprints(db);

          // extra paranoia... we _should_ never run this section twice
          // since a merged workspace would fail early with work.has_changes()
          break;
        }
    }

  db.delete_existing_rev_and_certs(revid);
}

CMD(db_kill_branch_certs_locally, "kill_branch_certs_locally", "", CMD_REF(db),
    "BRANCH",
    N_("Kills branch certificates from the local database"),
    "",
    options::opts::none)
{
  if (args.size() != 1)
    throw usage(execid);

  database db(app);
  db.delete_branch_named(typecast_vocab<cert_value>(idx(args, 0)));
}

CMD(db_kill_tag_locally, "kill_tag_locally", "", CMD_REF(db), "TAG",
    N_("Kills a tag from the local database"),
    "",
    options::opts::none)
{
  if (args.size() != 1)
    throw usage(execid);

  database db(app);
  db.delete_tag_named(typecast_vocab<cert_value>(idx(args, 0)));
}

CMD(db_check, "check", "", CMD_REF(db), "",
    N_("Does some sanity checks on the database"),
    N_("Ensures that the database is consistent by issuing multiple "
       "checks."),
    options::opts::none)
{
  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  database db(app);
  check_db(db);
}

CMD(db_changesetify, "changesetify", "", CMD_REF(db), "",
    N_("Converts the database to the changeset format"),
    "",
    options::opts::none)
{
  database db(app);
  key_store keys(app);
  project_t project(db);

  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  db.ensure_open_for_format_changes();
  db.check_is_not_rosterified();

  // early short-circuit to avoid failure after lots of work
  cache_user_key(app.opts, app.lua, db, keys, project);

  build_changesets_from_manifest_ancestry(db, keys, project, set<string>());
}

CMD(db_rosterify, "rosterify", "", CMD_REF(db), "",
    N_("Converts the database to the rosters format"),
    "",
    options::opts::drop_attr)
{
  database db(app);
  key_store keys(app);
  project_t project(db);

  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  db.ensure_open_for_format_changes();
  db.check_is_not_rosterified();

  // early short-circuit to avoid failure after lots of work
  cache_user_key(app.opts, app.lua, db, keys, project);

  build_roster_style_revs_from_manifest_style_revs(db, keys, project,
                                                   app.opts.attrs_to_drop);
}

CMD(db_regenerate_caches, "regenerate_caches", "", CMD_REF(db), "",
    N_("Regenerates the caches stored in the database"),
    "",
    options::opts::none)
{
  E(args.size() == 0, origin::user,
    F("no arguments needed"));

  database db(app);
  regenerate_caches(db);
}

CMD_HIDDEN(clear_epoch, "clear_epoch", "", CMD_REF(db), "BRANCH",
    N_("Clears the branch's epoch"),
    "",
    options::opts::none)
{
  if (args.size() != 1)
    throw usage(execid);

  database db(app);
  db.clear_epoch(typecast_vocab<branch_name>(idx(args, 0)));
}

CMD(db_set_epoch, "set_epoch", "", CMD_REF(db), "BRANCH EPOCH",
    N_("Sets the branch's epoch"),
    "",
    options::opts::none)
{
  if (args.size() != 2)
    throw usage(execid);

  E(idx(args, 1)().size() == constants::epochlen, origin::user,
    F("The epoch must be %s characters") % constants::epochlen);

  epoch_data ed(decode_hexenc_as<epoch_data>(idx(args, 1)(), origin::user));
  database db(app);
  db.set_epoch(branch_name(idx(args, 0)(), origin::user), ed);
}

CMD(set, "set", "", CMD_REF(variables), N_("DOMAIN NAME VALUE"),
    N_("Sets a database variable"),
    N_("This command modifies (or adds if it did not exist before) the "
       "variable named NAME, stored in the database, and sets it to the "
       "given value in VALUE.  The variable is placed in the domain DOMAIN."),
    options::opts::none)
{
  if (args.size() != 3)
    throw usage(execid);

  var_domain d = typecast_vocab<var_domain>(idx(args, 0));
  var_name n;
  var_value v;
  n = typecast_vocab<var_name>(idx(args, 1));
  v = typecast_vocab<var_value>(idx(args, 2));

  database db(app);
  db.set_var(make_pair(d, n), v);
}

CMD(unset, "unset", "", CMD_REF(variables), N_("DOMAIN NAME"),
    N_("Unsets a database variable"),
    N_("This command removes the variable NAME from domain DOMAIN, which "
       "was previously stored in the database."),
    options::opts::none)
{
  if (args.size() != 2)
    throw usage(execid);

  var_domain d = typecast_vocab<var_domain>(idx(args, 0));
  var_name n;
  n = typecast_vocab<var_name>(idx(args, 1));
  var_key k(d, n);

  database db(app);
  E(db.var_exists(k), origin::user,
    F("no var with name %s in domain %s") % n % d);
  db.clear_var(k);
}

CMD(register_workspace, "register_workspace",  "", CMD_REF(variables),
    N_("[WORKSPACE_PATH]"),
    N_("Registers a new workspace for the current database"),
    N_("This command adds WORKSPACE_PATH to the list of `known-workspaces'."),
    options::opts::none)
{
  if (args.size() > 1)
    throw usage(execid);

  E(args.size() == 1 || workspace::found, origin::user,
    F("No workspace given"));

  system_path workspace;
  if (args.size() == 1)
    workspace = system_path(idx(args, 0)(), origin::user);
  else
    get_current_workspace(workspace);

  database db(app);
  db.register_workspace(workspace);
}

CMD(unregister_workspace, "unregister_workspace", "", CMD_REF(variables),
    N_("[WORKSPACE_PATH]"),
    N_("Unregisters an existing workspace for the current database"),
    N_("This command removes WORKSPACE_PATH to the list of `known-workspaces'."),
    options::opts::none)
{
  if (args.size() > 1)
    throw usage(execid);

  E(args.size() == 1 || workspace::found, origin::user,
    F("No workspace given"));

  system_path workspace;
  if (args.size() == 1)
    workspace = system_path(idx(args, 0)(), origin::user);
  else
    get_current_workspace(workspace);

  database db(app);
  db.unregister_workspace(workspace);
}

CMD(cleanup_workspace_list, "cleanup_workspace_list", "", CMD_REF(variables), "",
    N_("Removes all invalid, registered workspace paths for the current database"),
    "",
    options::opts::none)
{
  if (args.size() != 0)
    throw usage(execid);

  vector<system_path> original_workspaces, valid_workspaces;

  database db(app);
  db.get_registered_workspaces(original_workspaces);

  database_path_helper helper(app.lua);

  for (vector<system_path>::const_iterator i = original_workspaces.begin();
       i != original_workspaces.end(); ++i)
    {
      system_path workspace_path(*i);
      if (!directory_exists(workspace_path / bookkeeping_root_component))
        {
          L(FL("ignoring missing workspace '%s'") % workspace_path);
          continue;
        }

      options workspace_opts;
      workspace::get_options(workspace_path, workspace_opts);

      system_path workspace_db_path;
      helper.get_database_path(workspace_opts, workspace_db_path);

      if (workspace_db_path != db.get_filename())
        {
          L(FL("ignoring workspace '%s', expected database %s, "
               "but has %s configured in _MTN/options")
              % workspace_path % db.get_filename() % workspace_db_path);
          continue;
        }

      valid_workspaces.push_back(workspace_path);
    }

  db.set_registered_workspaces(valid_workspaces);
}

CMD(complete, "complete", "", CMD_REF(informative),
    N_("(revision|file|key) PARTIAL-ID"),
    N_("Completes a partial identifier"),
    "",
    options::opts::verbose)
{
  if (args.size() != 2)
    throw usage(execid);

  database db(app);
  project_t project(db);

  bool verbose = app.opts.verbose;

  E(idx(args, 1)().find_first_not_of("abcdef0123456789") == string::npos,
    origin::user,
    F("non-hex digits in partial id"));

  if (idx(args, 0)() == "revision")
    {
      set<revision_id> completions;
      db.complete(idx(args, 1)(), completions);
      for (set<revision_id>::const_iterator i = completions.begin();
           i != completions.end(); ++i)
        {
          if (!verbose) cout << *i << '\n';
          else cout << describe_revision(app.opts, app.lua, project, *i) << '\n';
        }
    }
  else if (idx(args, 0)() == "file")
    {
      set<file_id> completions;
      db.complete(idx(args, 1)(), completions);
      for (set<file_id>::const_iterator i = completions.begin();
           i != completions.end(); ++i)
        cout << *i << '\n';
    }
  else if (idx(args, 0)() == "key")
    {
      typedef set< pair<key_id, utf8 > > completions_t;
      completions_t completions;
      db.complete(idx(args, 1)(), completions);
      for (completions_t::const_iterator i = completions.begin();
           i != completions.end(); ++i)
        {
          cout << i->first;
          if (verbose) cout << ' ' << i->second();
          cout << '\n';
        }
    }
  else
    throw usage(execid);
}

CMD_HIDDEN(test_migration_step, "test_migration_step", "", CMD_REF(db),
           "SCHEMA",
           N_("Runs one step of migration on the specified database"),
           N_("This command migrates the given database from the specified "
              "schema in SCHEMA to its successor."),
           options::opts::none)
{
  database db(app);
  key_store keys(app);

  if (args.size() != 1)
    throw usage(execid);
  db.test_migration_step(keys, idx(args,0)());
}

CMD_HIDDEN(rev_height, "rev_height", "", CMD_REF(informative), N_("REV"),
           N_("Shows a revision's height"),
           "",
           options::opts::none)
{
  if (args.size() != 1)
    throw usage(execid);

  revision_id rid(decode_hexenc_as<revision_id>(idx(args, 0)(), origin::user));
  database db(app);
  E(db.revision_exists(rid), origin::user,
    F("no such revision '%s'") % rid);
  rev_height height;
  db.get_rev_height(rid, height);
  P(F("cached height: %s") % height);
}

// loading revisions is relatively fast

CMD_HIDDEN(load_revisions, "load_revisions", "", CMD_REF(db), "",
    N_("load all revisions from the database"),
    N_("This command loads all revisions from the database and is "
       "intended to be used for timing revision loading performance."),
    options::opts::none)
{
  database db(app);
  set<revision_id> ids;
  vector<revision_id> revisions;

  db.get_revision_ids(ids);
  toposort(db, ids, revisions);

  P(F("loading revisions"));
  ticker loaded(_("revisions"), "r", 64);
  loaded.set_total(revisions.size());

  typedef vector<revision_id>::const_iterator revision_iterator;

  for (revision_iterator i = revisions.begin(); i != revisions.end(); ++i)
    {
      revision_t revision;
      db.get_revision(*i, revision);
      ++loaded;
    }
}

// loading rosters is slow compared with files, revisions or certs

CMD_HIDDEN(load_rosters, "load_rosters", "", CMD_REF(db), "",
    N_("load all roster versions from the database"),
    N_("This command loads all roster versions from the database and is "
       "intended to be used for timing roster reconstruction performance."),
    options::opts::none)
{
  database db(app);
  set<revision_id> ids;
  vector<revision_id> rosters;

  db.get_revision_ids(ids);
  toposort(db, ids, rosters);

  P(F("loading rosters"));
  ticker loaded(_("rosters"), "r", 1);
  loaded.set_total(rosters.size());
  typedef vector<revision_id>::const_iterator roster_iterator;

  for (roster_iterator i = rosters.begin(); i != rosters.end(); ++i)
    {
      roster_t roster;
      db.get_roster(*i, roster);
      ++loaded;
    }
}

// loading files is slower than revisions but faster than rosters

CMD_HIDDEN(load_files, "load_files", "", CMD_REF(db), "",
    N_("load all file versions from the database"),
    N_("This command loads all files versions from the database and is "
       "intended to be used for timing file reconstruction performance."),
    options::opts::none)
{
  database db(app);
  set<file_id> files;
  db.get_file_ids(files);

  P(F("loading files"));
  ticker loaded(_("files"), "f", 1);
  loaded.set_total(files.size());

  typedef set<file_id>::const_iterator file_iterator;

  for (file_iterator i = files.begin(); i != files.end(); ++i)
    {
      file_data file;
      db.get_file_version(*i, file);
      ++loaded;
    }
}

// loading certs is fast

CMD_HIDDEN(load_certs, "load_certs", "", CMD_REF(db), "",
    N_("load all certs from the database"),
    N_("This command loads all certs from the database and is "
       "intended to be used for timing cert loading performance."),
    options::opts::none)
{
  database db(app);
  vector<cert> certs;

  P(F("loading certs"));
  db.get_revision_certs(certs);
  P(F("loaded %d certs") % certs.size());
}

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: