def test_drop_nopermission(self): weedb.create(mysql1_dict) with self.assertRaises(weedb.PermissionError): weedb.drop(mysql2_dict) weedb.create(sqdb1_dict) # Can't really test this one without setting up a file where # we have no write permission with self.assertRaises(weedb.NoDatabaseError): weedb.drop(sqdb2_dict)
def setUp(self): """Drop the old databases, in preparation for running a test.""" try: weedb.drop(mysql1_dict) except weedb.NoDatabase: pass try: weedb.drop(sqdb1_dict) except weedb.NoDatabase: pass
def reconfigMainDatabase(config_dict, db_binding): """Create a new database, then populate it with the contents of an old database""" manager_dict = weewx.manager.get_manager_dict_from_config(config_dict, db_binding) # Make a copy for the new database (we will be modifying it) new_database_dict = dict(manager_dict['database_dict']) # Now modify the database name new_database_dict['database_name'] = manager_dict['database_dict']['database_name']+'_new' # First check and see if the new database already exists. If it does, check # with the user whether it's ok to delete it. try: weedb.create(new_database_dict) except weedb.DatabaseExists: ans = None while ans not in ['y', 'n']: ans = raw_input("New database '%s' already exists. Delete it first (y/n)? " % (new_database_dict['database_name'],)) if ans == 'y': weedb.drop(new_database_dict) elif ans == 'n': print "Nothing done." return # Get the unit system of the old archive: with weewx.manager.Manager.open(manager_dict['database_dict']) as old_dbmanager: old_unit_system = old_dbmanager.std_unit_system # Get the unit system of the new archive: try: target_unit_nickname = config_dict['StdConvert']['target_unit'] except KeyError: target_unit_system = None else: target_unit_system = weewx.units.unit_constants[target_unit_nickname.upper()] ans = None while ans not in ['y', 'n']: print "Copying Weewx-WD archive database '%s' to '%s'" % (manager_dict['database_dict']['database_name'], new_database_dict['database_name']) if target_unit_system is None or old_unit_system==target_unit_system: print "The new archive will use the same unit system as the old ('%s')." % (weewx.units.unit_nicknames[old_unit_system],) else: print "Units will be converted from the '%s' system to the '%s' system." % (weewx.units.unit_nicknames[old_unit_system], weewx.units.unit_nicknames[target_unit_system]) ans = raw_input("Are you sure you wish to proceed (y/n)? ") if ans == 'y': weewx.manager.reconfig(manager_dict['database_dict'], new_database_dict, new_unit_system=target_unit_system, new_schema=manager_dict['schema']) print "Done." elif ans == 'n': print "Nothing done."
def setUp(self): """Drop the old databases, in preparation for running a test.""" try: import MySQLdb except ImportError as e: raise unittest.case.SkipTest(e.message) try: weedb.drop(mysql1_dict) except weedb.NoDatabase: pass try: weedb.drop(sqdb1_dict) except weedb.NoDatabase: pass
def setup_database(db_dict): """Set up a database by using addRecord()""" try: # Drop the old database weedb.drop(db_dict) except weedb.NoDatabaseError: pass # Get a new database by initializing with the schema db_manager = weewx.manager.DaySummaryManager.open_with_create(db_dict, schema=schema) # Populate the database. By passing in a generator, it is all done as one transaction. db_manager.addRecord(gen_fake_data.genFakeRecords(start_ts, stop_ts, interval=interval_secs)) return db_manager
def configDatabases(archive_db_dict, stats_db_dict): """Configures the main and stats databases.""" # Check to see if it already exists and is configured correctly. try: with weewx.archive.Archive.open(archive_db_dict) as archive: if archive.firstGoodStamp() == start_ts and archive.lastGoodStamp() == stop_ts: # Database already exists. We're done. return except: pass # Delete anything that might already be there. try: weedb.drop(archive_db_dict) except: pass # Now build a new one: with weewx.archive.Archive.open_with_create(archive_db_dict, user.schemas.defaultArchiveSchema) as archive: # Because this can generate voluminous log information, # suppress all but the essentials: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_ERR)) # Now generate and add the fake records to populate the database: t1= time.time() archive.addRecord(genFakeRecords()) t2 = time.time() print "Time to create synthetic archive database = %6.2fs" % (t2-t1,) # Now go back to regular logging: syslog.setlogmask(syslog.LOG_UPTO(syslog.LOG_DEBUG)) # Delete any old stats database: try: weedb.drop(stats_db_dict) except weedb.NoDatabase: pass # Now create and configure a new one: with weewx.stats.StatsDb.open_with_create(stats_db_dict, user.schemas.defaultStatsSchema) as stats: t1 = time.time() # Now backfill the stats database from the main archive database. nrecs = stats.backfillFrom(archive) t2 = time.time() print "Time to backfill stats database with %d records: %6.2fs" % (nrecs, t2-t1)
def test_drop_nonexistent_database(self): with self.assertRaises(weedb.NoDatabase): weedb.drop(mysql1_dict) with self.assertRaises(weedb.NoDatabase): weedb.drop(sqdb1_dict)
def tearDown(self): try: weedb.drop(self.archive_db_dict) except: pass
def setUp(self): try: weedb.drop(self.archive_db_dict) except: pass
def setUp(self): try: weedb.drop(self.db_dict) except: pass
def update_to_v3(config_dict, db_binding): import weedb import sqlite3 from weewx.manager import Manager # update an old schema to v3-compatible. this means adding the # interval column and populating it. mgr_dict = weewx.manager.get_manager_dict(config_dict['DataBindings'], config_dict['Databases'], db_binding) # see if update is actually required schema_name = mgr_dict.get('schema') if schema_name is None: s = schema elif isinstance(schema_name, str): s = weeutil.weeutil._get_object(schema_name) else: s = mgr_dict['schema'] if 'interval' in s: print "Column 'interval' already exists, update not necessary" return # make a copy of the database dict new_db_dict = dict(mgr_dict['database_dict']) # modify the database name new_db_dict['database_name'] = mgr_dict['database_dict'][ 'database_name'] + '_new' # see if the new db exists. if so, see if we can delete it try: weedb.create(new_db_dict) except weedb.DatabaseExists: ans = None while ans not in ['y', 'n']: ans = raw_input( "New database '%s' already exists. Delete it (y/n)? " % (new_db_dict['database_name'], )) if ans == 'y': weedb.drop(new_db_dict) elif ans == 'n': print "update aborted" return except sqlite3.OperationalError: pass # see if we really should do this ans = None while ans not in ['y', 'n']: ans = raw_input("Create new database %s (y/n)? " % new_db_dict['database_name']) if ans == 'y': break elif ans == 'n': print "update aborted" return # copy the data over cnt = 0 with Manager.open(mgr_dict['database_dict']) as old_mgr: with Manager.open_with_create(new_db_dict, schema=s) as new_mgr: last_ts = None for r in old_mgr.genBatchRecords(): cnt += 1 print "record %d\r" % cnt, ival = r['dateTime'] - last_ts if last_ts else 0 r['interval'] = ival new_mgr.addRecord(r) last_ts = r['dateTime'] print "copied %d records\n" % cnt
def tearDown(self): try: weedb.drop(self.db_dict) except: pass