aboutsummaryrefslogtreecommitdiff
path: root/bpkg/database.cxx
blob: 1b7612fcd2f9ae7222c5a6218dab496660b466b0 (plain)
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
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
// file      : bpkg/database.cxx -*- C++ -*-
// license   : MIT; see accompanying LICENSE file

#include <bpkg/database.hxx>

#include <map>

#include <odb/schema-catalog.hxx>
#include <odb/sqlite/exceptions.hxx>

#include <libbutl/sha256.mxx>

#include <bpkg/package.hxx>
#include <bpkg/package-odb.hxx>
#include <bpkg/diagnostics.hxx>

using namespace std;

namespace bpkg
{
  namespace sqlite = odb::sqlite;

  // Register the data migration functions.
  //
  // NOTE: remember to qualify table names with \"main\". if using native
  // statements.
  //
  template <odb::schema_version v>
  using migration_entry = odb::data_migration_entry<v, DB_SCHEMA_VERSION_BASE>;

  static const migration_entry<8>
  migrate_v8 ([] (odb::database& db)
  {
    for (shared_ptr<repository> r: pointer_result (db.query<repository> ()))
    {
      if (!r->name.empty ()) // Non-root repository?
      {
        r->local = r->location.local ();
        db.update (r);
      }
    }
  });

  static const migration_entry<9>
  migrate_v9 ([] (odb::database& db)
  {
    // Add the unnamed self-association of the target type.
    //
    shared_ptr<configuration> sc (
      make_shared<configuration> (optional<string> (), "target"));

    db.persist (sc);
    db.execute ("UPDATE selected_package_prerequisites SET configuration = '" +
                sc->uuid.string () + "'");
  });

  static inline path
  cfg_path (const dir_path& d, bool create)
  {
    path f (d / bpkg_dir / "bpkg.sqlite3");

    if (!create && !exists (f))
      fail << d << " does not look like a bpkg configuration directory";

    return f;
  }

  // The BPKG_OPEN_CONFIGS environment variable.
  //
  // Automatically set it to the configuration directory path and clear in the
  // main database constructor and destructor, respectively. Also append the
  // attached database configuration paths in their constructors and clear
  // them in detach_all(). The paths are absolute, normalized, double-quoted,
  // and separated with spaces.
  //
  static const string open_name ("BPKG_OPEN_CONFIGS");

  struct database::impl
  {
    sqlite::connection_ptr conn; // Main connection.

    map<dir_path, database> attached_map;

    impl (sqlite::connection_ptr&& c): conn (move (c)) {}
  };

  database::
  database (const dir_path& d,
            configuration* create,
            odb::tracer& tr,
            bool pre_attach,
            bool sys_rep,
            const dir_path* pre_assoc)
      : sqlite::database (
          cfg_path (d, create != nullptr).string (),
          SQLITE_OPEN_READWRITE | (create != nullptr ? SQLITE_OPEN_CREATE : 0),
          true,                  // Enable FKs.
          "",                    // Default VFS.
          unique_ptr<sqlite::connection_factory> (
            new sqlite::serial_connection_factory)), // Single connection.
        config (normalize (d, "configuration")),
        config_orig (d)
  {
    bpkg::tracer trace ("database");

    // Cache the (single) main connection we will be using.
    //
    unique_ptr<impl> ig ((impl_ = new impl (connection ())));

    try
    {
      tracer_guard tg (*this, trace);

      // Lock the database for as long as the connection is active. First we
      // set locking_mode to EXCLUSIVE which instructs SQLite not to release
      // any locks until the connection is closed. Then we force SQLite to
      // acquire the write lock by starting exclusive transaction. See the
      // locking_mode pragma documentation for details. This will also fail if
      // the database is inaccessible (e.g., file does not exist, already used
      // by another process, etc).
      //
      // Note that here we assume that any database that is ATTACHED within an
      // exclusive transaction gets the same treatment.
      //
      using odb::schema_catalog;

      impl_->conn->execute ("PRAGMA locking_mode = EXCLUSIVE");

      add_env (true /* reset */);
      auto g (make_exception_guard ([] () {unsetenv (open_name);}));

      {
        sqlite::transaction t (impl_->conn->begin_exclusive ());

        if (create != nullptr)
        {
          // Create the new schema and persist the self-association.
          //
          if (schema_version () != 0)
            fail << sqlite::database::name () << ": already has database "
                 << "schema";

          schema_catalog::create_schema (*this);

          persist (*create); // Also assigns association id.

          // Cache the configuration information.
          //
          cache_config (create->uuid, create->name, create->type);
        }
        else
        {
          // Migrate the associated databases cluster.
          //
          migrate ();

          // Cache the configuration information.
          //
          shared_ptr<configuration> c (load<configuration> (0));
          cache_config (c->uuid, move (c->name), move (c->type));

          // Load the system repository, if requested.
          //
          if (sys_rep)
            load_system_repository ();
        }

        // Migrate the pre-associated database and the database cluster it
        // belongs to.
        //
        if (pre_assoc != nullptr)
          attach (*pre_assoc).migrate ();

        t.commit ();
      }

      // Detach potentially attached during migration the (pre-)associated
      // databases.
      //
      detach_all ();

      if (pre_attach)
      {
        sqlite::transaction t (begin_exclusive ());
        attach_explicit (sys_rep);
        t.commit ();
      }
    }
    catch (odb::timeout&)
    {
      fail << "configuration " << d << " is already used by another process";
    }
    catch (const sqlite::database_exception& e)
    {
      fail << sqlite::database::name () << ": " << e.message ();
    }

    tracer (tr);

    // Note: will be leaked if anything further throws.
    //
    ig.release ();
  }

  // NOTE: if we ever load/persist any dynamically allocated objects in this
  // constructor, make sure such objects do not use the session or the session
  // is temporarily suspended in the attach() function (see its implementation
  // for the reasoning note) since the database will be moved.
  //
  database::
  database (impl* i,
            const dir_path& d,
            std::string schema,
            bool sys_rep)
      : sqlite::database (i->conn,
                          cfg_path (d, false /* create */).string (),
                          move (schema)),
        config (d),
        impl_ (i)
  {
    bpkg::tracer trace ("database");

    // Derive the configuration original directory path.
    //
    database& mdb (main_database ());

    if (mdb.config_orig.relative ())
    {
      // Fallback to absolute path if the configuration is on a different
      // drive on Windows.
      //
      if (optional<dir_path> c = config.try_relative (current_directory ()))
        config_orig = move (*c);
      else
        config_orig = config;
    }
    else
      config_orig = config;

    try
    {
      tracer_guard tg (*this, trace);

      // Cache the configuration information.
      //
      shared_ptr<configuration> c (load<configuration> (0));
      cache_config (c->uuid, move (c->name), move (c->type));

      // Load the system repository, if requested.
      //
      if (sys_rep)
        load_system_repository ();
    }
    catch (const sqlite::database_exception& e)
    {
      fail << sqlite::database::name () << ": " << e.message ();
    }

    add_env ();

    // Set the tracer used by the associated configurations cluster.
    //
    sqlite::database::tracer (mdb.tracer ());
  }

  database::
  ~database ()
  {
    if (impl_ != nullptr && // Not a moved-from database?
        main ())
    {
      delete impl_;

      unsetenv (open_name);
    }
  }

  database::
  database (database&& db)
      : sqlite::database (move (db)),
        uuid (db.uuid),
        type (move (db.type)),
        config (move (db.config)),
        config_orig (move (db.config_orig)),
        system_repository (move (db.system_repository)),
        impl_ (db.impl_),
        explicit_associations_ (move (db.explicit_associations_)),
        implicit_associations_ (move (db.implicit_associations_))
  {
    db.impl_ = nullptr; // See ~database().
  }

  void database::
  add_env (bool reset) const
  {
    using std::string;

    string v;

    if (!reset)
    {
      if (optional<string> e = getenv (open_name))
        v = move (*e);
    }

    v += (v.empty () ? "\"" : " \"") + config.string () + '"';

    setenv (open_name, v);
  }

  void database::
  tracer (tracer_type* t)
  {
    main_database ().sqlite::database::tracer (t);

    for (auto& db: impl_->attached_map)
      db.second.sqlite::database::tracer (t);
  }

  void database::
  migrate ()
  {
    using odb::schema_catalog;

    odb::schema_version sv  (schema_version ());
    odb::schema_version scv (schema_catalog::current_version (*this));

    if (sv != scv)
    {
      if (sv < schema_catalog::base_version (*this))
        fail << "configuration " << config_orig << " is too old";

      if (sv > scv)
        fail << "configuration " << config_orig << " is too new";

      // Note that we need to migrate the current database before the
      // associated ones to properly handle association cycles.
      //
      schema_catalog::migrate (*this);

      for (auto& c: query<configuration> (odb::query<configuration>::id != 0))
      {
        dir_path d (c.effective_path (config));

        // Remove the dangling implicit association.
        //
        if (!c.expl && !exists (d))
        {
          warn << "implicit association " << c.path << " of configuration "
               << config_orig << " does not exist, removing";

          erase (c);
          continue;
        }

        attach (d).migrate ();
      }
    }
  }

  void database::
  cache_config (const uuid_type& u, optional<std::string> n, std::string t)
  {
    uuid = u;
    name = move (n);
    type = move (t);
  }

  void database::
  load_system_repository ()
  {
    assert (!system_repository); // Must only be loaded once.

    system_repository = bpkg::system_repository ();

    // Query for all the packages with the system substate and enter their
    // versions into system_repository as non-authoritative. This way an
    // available_package (e.g., a stub) will automatically "see" system
    // version, if one is known.
    //
    assert (transaction::has_current ());

    for (const auto& p: query<selected_package> (
           odb::query<selected_package>::substate == "system"))
      system_repository->insert (p.name,
                                 p.version,
                                 false /* authoritative */);
  }

  database& database::
  attach (const dir_path& d, bool sys_rep)
  {
    assert (d.absolute () && d.normalized ());

    // Check if we are trying to attach the main database.
    //
    database& md (main_database ());
    if (d == md.config)
      return md;

    auto& am (impl_->attached_map);

    auto i (am.find (d));

    if (i == am.end ())
    {
      // We know from the implementation that 4-character schema names are
      // optimal. So try to come up with a unique abbreviated hash that is 4
      // or more characters long.
      //
      std::string schema;
      {
        butl::sha256 h (d.string ());

        for (size_t n (4);; ++n)
        {
          schema = h.abbreviated_string (n);

          if (find_if (am.begin (), am.end (),
                       [&schema] (const map<dir_path, database>::value_type& v)
                       {
                         return v.second.schema () == schema;
                       }) == am.end ())
            break;
        }
      }

      // If attaching out of an exclusive transaction (all our transactions
      // are exclusive), start one to force database locking (see the above
      // locking_mode discussion for details).
      //
      sqlite::transaction t;
      if (!sqlite::transaction::has_current ())
        t.reset (begin_exclusive ());

      try
      {
        // NOTE: we need to be careful here not to bind any persistent objects
        // the database constructor may load/persist to the temporary database
        // object in the session cache.
        //
        i = am.insert (
          make_pair (d, database (impl_, d, move (schema), sys_rep))).first;
      }
      catch (odb::timeout&)
      {
        fail << "configuration " << d << " is already used by another process";
      }

      if (!t.finalized ())
        t.commit ();
    }

    return i->second;
  }

  void database::
  detach_all ()
  {
    assert (main ());

    explicit_associations_.clear ();
    implicit_associations_.clear ();

    for (auto i (impl_->attached_map.begin ());
         i != impl_->attached_map.end (); )
    {
      i->second.detach ();
      i = impl_->attached_map.erase (i);
    }

    // Remove the detached databases from the environment.
    //
    add_env (true /* reset */);
  }

  void database::
  verify_association (const configuration& ac, database& adb)
  {
    const dir_path& c (adb.config_orig);

    if (ac.uuid != adb.uuid)
      fail << "configuration " << c << " uuid mismatch" <<
        info << "uuid " << adb.uuid <<
        info << (!ac.expl ? "implicitly " : "") << "associated with "
             << config_orig << " as " << ac.uuid;

    if (ac.type != adb.type)
      fail << "configuration " << c << " type mismatch" <<
        info << "type " << adb.type <<
        info << (!ac.expl ? "implicitly " : "") << "associated with "
             << config_orig << " as " << ac.type;

    if (ac.effective_path (config) != adb.config)
      fail << "configuration " << c << " path mismatch" <<
        info << (!ac.expl ? "implicitly " : "") << "associated with "
           << config_orig << " as " << ac.path;
  }

  void database::
  attach_explicit (bool sys_rep)
  {
    assert (transaction::has_current ());

    if (explicit_associations_.empty ())
    {
      // Note that the self-association is implicit.
      //
      explicit_associations_.push_back (associated_config {0, name, *this});

      for (auto& ac: query<configuration> (odb::query<configuration>::expl))
      {
        database& db (attach (ac.effective_path (config), sys_rep));
        verify_association (ac, db);

        explicit_associations_.push_back (associated_config {*ac.id,
                                                             move (ac.name),
                                                             db});
        db.attach_explicit (sys_rep);
      }
    }
  }

  associated_databases& database::
  implicit_associations (bool ath, bool sys_rep)
  {
    assert (transaction::has_current ());

    // Note that cached implicit associations must at least contain the self-
    // association, if the databases are already attached and cached.
    //
    if (implicit_associations_.empty () && ath)
    {
      implicit_associations_.push_back (*this);

      using q = odb::query<configuration>;

      for (const auto& ac: query<configuration> (q::id != 0))
      {
        dir_path d (ac.effective_path (config));

        // Skip the dangling implicit association.
        //
        if (!ac.expl && !exists (d))
          continue;

        database& db (attach (d, sys_rep));

        // Verify the association integrity.
        //
        verify_association (ac, db);

        // If the association is explicit, also check if it is also implicit
        // (see cfg_add() for details) and skip if it is not.
        //
        if (ac.expl)
        {
          shared_ptr<configuration> cf (
            db.query_one<configuration> (q::uuid == uuid.string ()));

          if (cf == nullptr)
            fail << "configuration " << db.config_orig << " is associated "
                 << "with " << config_orig << " but latter is not "
                 << "implicitly associated with former";

          // While at it, verify the integrity of the other end of the
          // association.
          //
          db.verify_association (*cf, *this);

          if (!cf->expl)
            continue;
        }

        // If the explicitly associated databases are pre-attached, normally
        // to make the selected packages loadable, then we also pre-attach
        // explicit associations of the database being attached implicitly, by
        // the same reason. Indeed, think of loading the package dependent
        // from the implicitly associated database as a selected package.
        //
        if (!explicit_associations_.empty ())
          db.attach_explicit (sys_rep);

        implicit_associations_.push_back (db);
      }
    }

    return implicit_associations_;
  }

  associated_databases database::
  dependent_configs (bool sys_rep)
  {
    associated_databases r;

    // Add the associated database to the resulting list if it is of the
    // associating database type or this type is 'host'. Call itself
    // recursively for the implicitly associated configurations.
    //
    auto add = [&r, sys_rep] (database& db,
                              const std::string& t,
                              const auto& add)
    {
      if (!(db.type == t || t == "host") ||
          std::find (r.begin (), r.end (), db) != r.end ())
        return;

      r.push_back (db);

      const associated_databases& ads (
        db.implicit_associations (true /* attach */, sys_rep));

      // Skip the self-association.
      //
      for (auto i (ads.begin () + 1); i != ads.end (); ++i)
        add (*i, db.type, add);
    };

    add (*this, type, add);
    return r;
  }

  associated_databases database::
  dependency_configs (optional<bool> buildtime)
  {
    associated_databases r;

    bool allow_own_type  (!buildtime || !*buildtime);
    bool allow_host_type (!buildtime || *buildtime);

    // Add the associated database to the resulting list if it is of the
    // associating database type and allow_own_type is true or if it is of the
    // host type and allow_host_type is true. Call itself recursively for the
    // explicitly associated configurations.
    //
    // Note that the associated database of the associating database type is
    // not added if allow_own_type is false but its own associated databases
    // of the host type are added, if allow_host_type is true.
    //
    associated_databases descended; // Note: we may not add but still descend.
    auto add = [&r, allow_own_type, allow_host_type, &descended]
               (database& db, const std::string& t, const auto& add)
    {
      if (std::find (descended.begin (), descended.end (), db) !=
          descended.end ())
        return;

      descended.push_back (db);

      bool own  (db.type == t);
      bool host (db.type == "host");

      if (!own && !(allow_host_type && host))
        return;

      if ((allow_own_type && own) || (allow_host_type && host))
        r.push_back (db);

      const associated_configs& acs (db.explicit_associations ());

      // Skip the self-association.
      //
      for (auto i (acs.begin () + 1); i != acs.end (); ++i)
        add (i->db, db.type, add);
    };

    add (*this, type, add);
    return r;
  }

  database& database::
  find_attached (uint64_t id)
  {
    assert (!explicit_associations_.empty ());

    // Note that there shouldn't be too many databases, so the linear search
    // is OK.
    //
    auto r (find_if (explicit_associations_.begin (),
                     explicit_associations_.end (),
                     [&id] (const associated_config& ac)
                     {
                       return ac.id == id;
                     }));

    if (r == explicit_associations_.end ())
      fail << "no configuration with id " << id << " is associated with "
           << config_orig;

    return r->db;
  }

  database& database::
  find_attached (const std::string& name)
  {
    assert (!explicit_associations_.empty ());

    auto r (find_if (explicit_associations_.begin (),
                     explicit_associations_.end (),
                     [&name] (const associated_config& ac)
                     {
                       return ac.name && *ac.name == name;
                     }));

    if (r == explicit_associations_.end ())
      fail << "no configuration with name '" << name << "' is associated "
           << "with " << config_orig;

    return r->db;
  }

  database& database::
  find_dependency_config (const uuid_type& uid)
  {
    for (database& adb: dependency_configs ())
    {
      if (uid == adb.uuid)
        return adb;
    }

    fail << "no configuration with uuid " << uid << " is associated with "
         << config_orig << endf;
  }

  bool database::
  main ()
  {
    return *this == main_database ();
  }

  string database::
  string ()
  {
    return main () ? empty_string : '[' + config_orig.representation () + ']';
  }
}