aboutsummaryrefslogtreecommitdiff
path: root/bpkg/database.cxx
blob: 68a2a34a2a9dcc245e13137549610ea2d7730b92 (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
// file      : bpkg/database.cxx -*- C++ -*-
// copyright : Copyright (c) 2014-2019 Code Synthesis Ltd
// license   : MIT; see accompanying LICENSE file

#include <bpkg/database.hxx>

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

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

using namespace std;

namespace bpkg
{
  using namespace odb::sqlite;
  using odb::schema_catalog;

  // Use a custom connection factory to automatically set and clear the
  // BPKG_OPEN_CONFIG environment variable. A bit heavy-weight but seems like
  // the best option.
  //
  static const string open_name ("BPKG_OPEN_CONFIG");

  class conn_factory: public single_connection_factory // No need for pool.
  {
  public:
    conn_factory (const dir_path& d)
    {
      setenv (open_name, normalize (d, "configuration").string ());
    }

    virtual
    ~conn_factory ()
    {
      unsetenv (open_name);
    }
  };

  // Register the data migration functions.
  //
  template <odb::schema_version v>
  using migration_entry = odb::data_migration_entry<v, DB_SCHEMA_VERSION_BASE>;

  static const migration_entry<6>
  migrate_v6 ([] (odb::database& db)
  {
    // Set the zero version revision to NULL.
    //
    auto migrate_rev = [&db] (const char* table, const char* column)
    {
      db.execute (string ("UPDATE ") + table + " SET " + column + " = NULL " +
                          "WHERE " + column + " = 0");
    };

    // The version package manifest value. Note: is not part of a primary key.
    //
    migrate_rev ("selected_package", "version_revision");

    // The depends package manifest value endpoint versions.
    //
    // Note that previously the zero and absent revisions had the same
    // semantics. Now the semantics differs and the zero revision is preserved
    // (see libbpkg/manifest.hxx for details).
    //
    migrate_rev ("selected_package_prerequisites", "min_version_revision");
    migrate_rev ("selected_package_prerequisites", "max_version_revision");

    migrate_rev ("available_package_dependency_alternatives",
                 "dep_min_version_revision");

    migrate_rev ("available_package_dependency_alternatives",
                 "dep_max_version_revision");
  });

  database
  open (const dir_path& d, tracer& tr, bool create)
  {
    tracer trace ("open");

    path f (d / bpkg_dir / "bpkg.sqlite3");

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

    try
    {
      database db (f.string (),
                   SQLITE_OPEN_READWRITE | (create ? SQLITE_OPEN_CREATE : 0),
                   true,                  // Enable FKs.
                   "",                    // Default VFS.
                   unique_ptr<connection_factory> (new conn_factory (d)));

      db.tracer (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).
      //
      using odb::sqlite::transaction; // Skip the wrapper.

      try
      {
        db.connection ()->execute ("PRAGMA locking_mode = EXCLUSIVE");
        transaction t (db.begin_exclusive ());

        if (create)
        {
          // Create the new schema.
          //
          if (db.schema_version () != 0)
            fail << f << ": already has database schema";

          schema_catalog::create_schema (db);
        }
        else
        {
          // Migrate the database if necessary.
          //
          schema_catalog::migrate (db);
        }

        t.commit ();
      }
      catch (odb::timeout&)
      {
        fail << "configuration " << d << " is already used by another process";
      }

      // 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.
      //
      transaction t (db.begin ());

      for (const auto& p:
             db.query<selected_package> (
               query<selected_package>::substate == "system"))
        system_repository.insert (p.name, p.version, false);

      t.commit ();

      db.tracer (tr); // Switch to the caller's tracer.
      return db;
    }
    catch (const database_exception& e)
    {
      fail << f << ": " << e.message () << endf;
    }
  }
}