def connection(self): if not self.dbc: if self.path != ":memory:": self.path = system.check_database_path(self.path, logging.error) logging.debug("* Database: %s", self.path) self.dbc = sqlite3.connect(self.path) # # To avoid the need to map at hand columns in # a row with the sql schema, which is as error # prone as driving drunk. # self.dbc.row_factory = sqlite3.Row # # Migrate MUST be before table creation. This # is safe because table creation always uses # the IF NOT EXISTS clause. And this is needed # because otherwise we cannot migrate archived # databases (whose version number is old). # The exception is the config table which must # be present because migrate() looks at it. # table_config.create(self.dbc) migrate.migrate(self.dbc) migrate2.migrate(self.dbc) table_speedtest.create(self.dbc) table_geoloc.create(self.dbc) table_bittorrent.create(self.dbc) table_log.create(self.dbc) return self.dbc
def connection(self): ''' Return connection to database ''' if not self.dbc: database_xxx.linux_fixup_databasedir() if self.path != ":memory:": self.path = system.check_database_path(self.path) logging.debug("* Database: %s", self.path) self.dbc = sqlite3.connect(self.path) # # To avoid the need to map at hand columns in # a row with the sql schema, which is as error # prone as driving drunk. # self.dbc.row_factory = sqlite3.Row # # On POSIX systems, neubot (initially) runs as root, to ensure that # database location, ownership and permissions are OK (as well as # to bind privileged ports). But neubot can also be started by # normal users. In this case, mark the database as readonly since # write operation are going to raise exceptions. # if not system.has_enough_privs(): logging.warning('database: opening database in readonly mode') self.readonly = True return self.dbc # # Migrate MUST be before table creation. This # is safe because table creation always uses # the IF NOT EXISTS clause. And this is needed # because otherwise we cannot migrate archived # databases (whose version number is old). # The exception is the config table which must # be present because migrate() looks at it. # table_config.create(self.dbc) migrate.migrate(self.dbc) migrate2.migrate(self.dbc) table_speedtest.create(self.dbc) table_geoloc.create(self.dbc) table_bittorrent.create(self.dbc) table_log.create(self.dbc) table_raw.create(self.dbc) return self.dbc
def connection(self): if not self.dbc: if self.path != ":memory:": self.path = system.check_database_path(self.path, logging.error) logging.debug("* Database: %s" % self.path) self.dbc = sqlite3.connect(self.path) # # To avoid the need to map at hand columns in # a row with the sql schema, which is as error # prone as driving drunk. # self.dbc.row_factory = sqlite3.Row table_config.create(self.dbc) table_speedtest.create(self.dbc) table_geoloc.create(self.dbc) table_bittorrent.create(self.dbc) table_log.create(self.dbc) migrate.migrate(self.dbc) return self.dbc
def runTest(self): """Make sure bittorrent table works as expected""" connection = sqlite3.connect(":memory:") connection.row_factory = sqlite3.Row table_bittorrent.create(connection) table_bittorrent.create(connection) v = map(None, ResultIterator()) for d in v: table_bittorrent.insert(connection, d, override_timestamp=False) v1 = table_bittorrent.listify(connection) self.assertEquals(sorted(v), sorted(v1)) since = utils.timestamp() - 7 * 24 * 60 * 60 until = utils.timestamp() - 3 * 24 * 60 * 60 v2 = table_bittorrent.listify(connection, since=since, until=until) self.assertTrue(len(v2) < len(v)) table_bittorrent.prune(connection, until) self.assertTrue(len(table_bittorrent.listify(connection)) < len(v1))