<commit_before>/**
 * This file is part of the CernVM file system.
 */

#include "cvmfs_config.h"
#include "catalog_sql.h"

#include <cstdlib>
#include <cstring>

#include "catalog.h"
#include "globals.h"
#include "logging.h"
#include "util.h"
#include "xattr.h"

using namespace std;  // NOLINT

namespace catalog {

const float CatalogDatabase::kLatestSchema = 2.5;
const float CatalogDatabase::kLatestSupportedSchema = 2.5;  // + 1.X (r/o)
// ChangeLog
//   0 --> 1: add size column to nested catalog table,
//            add schema_revision property
//   1 --> 2: add xattr column to catalog table
//            add self_xattrs and subtree_xattrs statistics counters
const unsigned CatalogDatabase::kLatestSchemaRevision = 2;


bool CatalogDatabase::CheckSchemaCompatibility() {
  return !( (schema_version() >= 2.0-kSchemaEpsilon)                   &&
            (!IsEqualSchema(schema_version(), kLatestSupportedSchema)) &&
            (!IsEqualSchema(schema_version(), 2.4)           ||
             !IsEqualSchema(kLatestSupportedSchema, 2.5)) );
}


bool CatalogDatabase::LiveSchemaUpgradeIfNecessary() {
  assert(read_write());

  if (IsEqualSchema(schema_version(), 2.5) && (schema_revision() == 0)) {
    LogCvmfs(kLogCatalog, kLogDebug, "upgrading schema revision");

    Sql sql_upgrade(*this, "ALTER TABLE nested_catalogs ADD size INTEGER;");
    if (!sql_upgrade.Execute()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade nested_catalogs");
      return false;
    }

    set_schema_revision(1);
    if (!StoreSchemaRevision()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade schema revision");
      return false;
    }
  }

  if (IsEqualSchema(schema_version(), 2.5) && (schema_revision() == 1)) {
    LogCvmfs(kLogCatalog, kLogDebug, "upgrading schema revision");

    Sql sql_upgrade1(*this, "ALTER TABLE catalog ADD xattr BLOB;");
    Sql sql_upgrade2(*this,
      "INSERT INTO statistics (counter, value) VALUES ('self_xattr', 0);");
    Sql sql_upgrade3(*this,
      "INSERT INTO statistics (counter, value) VALUES ('subtree_xattr', 0);");
    if (!sql_upgrade1.Execute() || !sql_upgrade2.Execute() ||
        !sql_upgrade3.Execute())
    {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade catalogs (1 --> 2)");
      return false;
    }

    set_schema_revision(2);
    if (!StoreSchemaRevision()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed tp upgrade schema revision");
      return false;
    }
  }

  return true;
}


bool CatalogDatabase::CreateEmptyDatabase() {
  assert(read_write());

  // generate the catalog table and index structure
  const bool retval =
  Sql(*this,
    "CREATE TABLE catalog "
    "(md5path_1 INTEGER, md5path_2 INTEGER, parent_1 INTEGER, parent_2 INTEGER,"<commit_msg>FIX: catalog live migration logging<commit_after>/**
 * This file is part of the CernVM file system.
 */

#include "cvmfs_config.h"
#include "catalog_sql.h"

#include <cstdlib>
#include <cstring>

#include "catalog.h"
#include "globals.h"
#include "logging.h"
#include "util.h"
#include "xattr.h"

using namespace std;  // NOLINT

namespace catalog {

const float CatalogDatabase::kLatestSchema = 2.5;
const float CatalogDatabase::kLatestSupportedSchema = 2.5;  // + 1.X (r/o)
// ChangeLog
//   0 --> 1: add size column to nested catalog table,
//            add schema_revision property
//   1 --> 2: add xattr column to catalog table
//            add self_xattrs and subtree_xattrs statistics counters
const unsigned CatalogDatabase::kLatestSchemaRevision = 2;


bool CatalogDatabase::CheckSchemaCompatibility() {
  return !( (schema_version() >= 2.0-kSchemaEpsilon)                   &&
            (!IsEqualSchema(schema_version(), kLatestSupportedSchema)) &&
            (!IsEqualSchema(schema_version(), 2.4)           ||
             !IsEqualSchema(kLatestSupportedSchema, 2.5)) );
}


bool CatalogDatabase::LiveSchemaUpgradeIfNecessary() {
  assert(read_write());

  if (IsEqualSchema(schema_version(), 2.5) && (schema_revision() == 0)) {
    LogCvmfs(kLogCatalog, kLogDebug, "upgrading schema revision (0 --> 1)");

    Sql sql_upgrade(*this, "ALTER TABLE nested_catalogs ADD size INTEGER;");
    if (!sql_upgrade.Execute()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed to upgrade nested_catalogs");
      return false;
    }

    set_schema_revision(1);
    if (!StoreSchemaRevision()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed to upgrade schema revision");
      return false;
    }
  }

  if (IsEqualSchema(schema_version(), 2.5) && (schema_revision() == 1)) {
    LogCvmfs(kLogCatalog, kLogDebug, "upgrading schema revision (1 --> 2)");

    Sql sql_upgrade1(*this, "ALTER TABLE catalog ADD xattr BLOB;");
    Sql sql_upgrade2(*this,
      "INSERT INTO statistics (counter, value) VALUES ('self_xattr', 0);");
    Sql sql_upgrade3(*this,
      "INSERT INTO statistics (counter, value) VALUES ('subtree_xattr', 0);");
    if (!sql_upgrade1.Execute() || !sql_upgrade2.Execute() ||
        !sql_upgrade3.Execute())
    {
      LogCvmfs(kLogCatalog, kLogDebug, "failed to upgrade catalogs (1 --> 2)");
      return false;
    }

    set_schema_revision(2);
    if (!StoreSchemaRevision()) {
      LogCvmfs(kLogCatalog, kLogDebug, "failed to upgrade schema revision");
      return false;
    }
  }

  return true;
}


bool CatalogDatabase::CreateEmptyDatabase() {
  assert(read_write());

  // generate the catalog table and index structure
  const bool retval =
  Sql(*this,
    "CREATE TABLE catalog "
    "(md5path_1 INTEGER, md5path_2 INTEGER, parent_1 INTEGER, parent_2 INTEGER,"
{'fraction_non_alphanumeric': 0.12913074712643677, 'fraction_numerical': 0.015086206896551725, 'mean_word_length': 3.621576763485477, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '768223', 'n_tokens_mistral': 1846, 'n_tokens_neox': 1745, 'n_words': 447}