code: def setup():
      
      try:
          db.bind(**config.database_config)
      except OSError:
          # Attempted to connect to a file-based database where the file didn't
          # exist
          db.bind(**config.database_config, create_db=True)
  
      rebuild = True
  
      try:
          db.generate_mapping(create_tables=True)
          with orm.db_session:
              version = GlobalConfig.get(key='schema_version')
              if version and version.int_value != SCHEMA_VERSION:
                  logger.info("Existing database has schema version %d",
                              version.int_value)
              else:
                  rebuild = False
      except:  # pylint:disable=bare-except
          logger.exception("Error mapping schema")
  
      if rebuild:
          logger.info("Rebuilding schema")
          try:
              db.drop_all_tables(with_all_data=True)
              db.create_tables()
          except:
              raise RuntimeError("Unable to upgrade schema automatically; please " +
                                 "delete the existing database and try again.")
  
      with orm.db_session:
          if not GlobalConfig.get(key='schema_version'):
              logger.info("setting schema version to %d", SCHEMA_VERSION)
              GlobalConfig(key='schema_version',
                           int_value=SCHEMA_VERSION)
              orm.commit()
docs: Set up the database