aboutsummaryrefslogtreecommitdiff
path: root/migrate
diff options
context:
space:
mode:
authorKaren Arutyunov <karen@codesynthesis.com>2017-04-04 20:53:00 +0300
committerKaren Arutyunov <karen@codesynthesis.com>2017-04-19 22:16:46 +0300
commitdbbc19b77dcf6ea828aabd64d7aa8cab9635aaf5 (patch)
treec0b9b449b7064dff3613628022224e6c18148c3e /migrate
parentefb9c3e0e6b612d5bfadc7a2b984c14b5439335c (diff)
Implement build task, result and log requests handling
Diffstat (limited to 'migrate')
-rw-r--r--migrate/migrate.cli10
-rw-r--r--migrate/migrate.cxx48
2 files changed, 40 insertions, 18 deletions
diff --git a/migrate/migrate.cli b/migrate/migrate.cli
index 42a1c2e..54edd70 100644
--- a/migrate/migrate.cli
+++ b/migrate/migrate.cli
@@ -17,13 +17,14 @@ include <cstdint>; // uint16_t
\cb{brep-migrate --help}\n
\cb{brep-migrate --version}\n
- \c{\b{brep-migrate} [<options>]}
+ \c{\b{brep-migrate} [<options>] <schema>}
\h|DESCRIPTION|
In its default mode \cb{brep-migrate} creates the database schema if it
doesn't already exist. Otherwise, it migrates the existing schema and data
- to the current version, if needed.
+ to the current version, if needed. The valid schema names are \cb{package}
+ and \cb{build}.
If the \cb{--recreate} option is specified, then \cb{brep-migrate} instead
recreates the database schema. That is, it drops all the existing tables
@@ -66,10 +67,11 @@ class options
expected to work."
}
- std::string --db-name|-n = "brep"
+ std::string --db-name|-n
{
"<name>",
- "Database name. If not specified, then '\cb{brep}' is used by default."
+ "Database name. If not specified, then it is implicitly derived by
+ prefixing the schema name with \cb{brep_}."
}
std::string --db-host|-h
diff --git a/migrate/migrate.cxx b/migrate/migrate.cxx
index 98bdfbd..c700efe 100644
--- a/migrate/migrate.cxx
+++ b/migrate/migrate.cxx
@@ -41,7 +41,7 @@ class schema
{
public:
explicit
- schema (const char* extra);
+ schema (const char* extra, string name);
void
create (database&) const;
@@ -50,12 +50,14 @@ public:
drop (database&) const;
private:
+ string name_;
strings drop_statements_;
strings create_statements_;
};
schema::
-schema (const char* s)
+schema (const char* s, string name)
+ : name_ (move (name))
{
// Remove comments, saving the cleaned SQL code into statements.
//
@@ -176,7 +178,7 @@ drop (database& db) const
//
db.execute (s);
- schema_catalog::drop_schema (db);
+ schema_catalog::drop_schema (db, name_);
}
void schema::
@@ -184,7 +186,7 @@ create (database& db) const
{
drop (db);
- schema_catalog::create_schema (db);
+ schema_catalog::create_schema (db, name_);
for (const auto& s: create_statements_)
db.execute (s);
@@ -205,6 +207,7 @@ try
{
cout << "brep-migrate " << BREP_VERSION_STR << endl
<< "libbrep " << LIBBREP_VERSION_STR << endl
+ << "libbbot " << LIBBBOT_VERSION_STR << endl
<< "libbpkg " << LIBBPKG_VERSION_STR << endl
<< "libbutl " << LIBBUTL_VERSION_STR << endl
<< "Copyright (c) 2014-2017 Code Synthesis Ltd" << endl
@@ -229,7 +232,19 @@ try
return p.wait () ? 0 : 1;
}
- if (argc > 1)
+ if (!scan.more ())
+ {
+ cerr << "error: no database schema specified" << endl
+ << help_info << endl;
+ return 1;
+ }
+
+ const string db_schema (scan.next ());
+
+ if (db_schema != "package" && db_schema != "build")
+ throw cli::unknown_argument (db_schema);
+
+ if (scan.more ())
{
cerr << "error: unexpected argument encountered" << endl
<< help_info << endl;
@@ -246,7 +261,9 @@ try
odb::pgsql::database db (
ops.db_user (),
ops.db_password (),
- ops.db_name (),
+ !ops.db_name ().empty ()
+ ? ops.db_name ()
+ : "brep_" + db_schema,
ops.db_host (),
ops.db_port (),
"options='-c default_transaction_isolation=serializable'");
@@ -261,7 +278,7 @@ try
// transaction useless as all consequitive queries in that transaction will
// be ignored by PostgreSQL.
//
- schema_version schema_version (db.schema_version ());
+ schema_version schema_version (db.schema_version (db_schema));
// It is impossible to operate with the database which is out of the
// [base_version, current_version] range due to the lack of the knowlege
@@ -269,13 +286,13 @@ try
//
if (schema_version > 0)
{
- if (schema_version < schema_catalog::base_version (db))
+ if (schema_version < schema_catalog::base_version (db, db_schema))
{
cerr << "error: database schema is too old" << endl;
throw failed ();
}
- if (schema_version > schema_catalog::current_version (db))
+ if (schema_version > schema_catalog::current_version (db, db_schema))
{
cerr << "error: database schema is too new" << endl;
throw failed ();
@@ -292,7 +309,7 @@ try
// database (followed with the database creation for the --recreate option).
//
if ((create || drop) && schema_version != 0 &&
- schema_version != schema_catalog::current_version (db))
+ schema_version != schema_catalog::current_version (db, db_schema))
{
cerr << "error: database schema requires migration" << endl
<< " info: either migrate the database first or drop the entire "
@@ -304,11 +321,14 @@ try
if (create || drop)
{
- static const char extras[] = {
+ static const char package_extras[] = {
#include <brep/package-extra>
, '\0'};
- schema s (extras);
+ schema s (db_schema == "package"
+ ? package_extras
+ : "",
+ db_schema);
if (create)
s.create (db);
@@ -319,10 +339,10 @@ try
{
// Register the data migration functions.
//
- // static const data_migration_entry<2, LIBBREP_SCHEMA_VERSION_BASE>
+ // static const data_migration_entry<2, LIBBREP_XXX_SCHEMA_VERSION_BASE>
// migrate_v2_entry (&migrate_v2);
//
- schema_catalog::migrate (db);
+ schema_catalog::migrate (db, 0, db_schema);
}
t.commit ();