def __init__(self, clientLabel, kw=None):
    """
    clientLabel: this *relatively short* string will be used to construct the
      temporary database name. It shouldn't contain any characters that would
      make it inappropriate for a database name (no spaces, etc.)
    kw: name of keyword argument to add to the decorated function(s). Its value
      will be a reference to this instance of ManagedTempRepository. Ignored
      when this instance is used as context manager. Defaults to kw=None to
      avoid having it added to the keyword args.
    """
    self._kw = kw

    self._unaffiliatedEngine = repository.getUnaffiliatedEngine(
      htmengine.APP_CONFIG)

    dbNameFromConfig = htmengine.APP_CONFIG.get(self.REPO_SECTION_NAME,
                                                self.REPO_DATABASE_ATTR_NAME)
    self.tempDatabaseName = "{original}_{label}_{uid}".format(
      original=dbNameFromConfig,
      label=clientLabel,
      uid=uuid.uuid1().hex)

    # Create a Config patch to override the Repository database name
    self._configPatch = ConfigAttributePatch(
      self.REPO_CONFIG_NAME,
      self.REPO_BASE_CONFIG_DIR,
      values=((self.REPO_SECTION_NAME, self.REPO_DATABASE_ATTR_NAME,
               self.tempDatabaseName),))
    self._configPatchApplied = False

    self._attemptedToCreateDatabase = False
  def __init__(self, clientLabel, kw=None):
    """
    clientLabel: this *relatively short* string will be used to construct the
      temporary database name. It shouldn't contain any characters that would
      make it inappropriate for a database name (no spaces, etc.)
    kw: name of keyword argument to add to the decorated function(s). Its value
      will be a reference to this instance of ManagedTempRepository. Ignored
      when this instance is used as context manager. Defaults to kw=None to
      avoid having it added to the keyword args.
    """
    self._kw = kw

    self._unaffiliatedEngine = repository.getUnaffiliatedEngine(
      htmengine.APP_CONFIG)

    dbNameFromConfig = htmengine.APP_CONFIG.get(self.REPO_SECTION_NAME,
                                                self.REPO_DATABASE_ATTR_NAME)
    self.tempDatabaseName = "{original}_{label}_{uid}".format(
      original=dbNameFromConfig,
      label=clientLabel,
      uid=uuid.uuid1().hex)

    # Create a Config patch to override the Repository database name
    self._configPatch = ConfigAttributePatch(
      self.REPO_CONFIG_NAME,
      self.REPO_BASE_CONFIG_DIR,
      values=((self.REPO_SECTION_NAME, self.REPO_DATABASE_ATTR_NAME,
               self.tempDatabaseName),))
    self._configPatchApplied = False

    self._attemptedToCreateDatabase = False
Beispiel #3
0
def reset():
  """
  Reset the htmengine database; upon successful completion, the necessary schema
  are created, but the tables are not populated
  """
  # Make sure we have the latest version of configuration
  config = Config("application.conf",
                  os.environ.get("APPLICATION_CONFIG_PATH"))
  dbName = config.get("repository", "db")

  resetDatabaseSQL = (
      "DROP DATABASE IF EXISTS %(database)s; "
      "CREATE DATABASE %(database)s;" % {"database": dbName})
  statements = resetDatabaseSQL.split(";")

  engine = getUnaffiliatedEngine(config)
  with engine.connect() as connection:
    for s in statements:
      if s.strip():
        connection.execute(s)

  migrate()