def setup_test_db(): cursor = database_f.get_test_cursor() # Strip it down tables = [] query = """SELECT tablename FROM pg_tables WHERE schemaname = 'public'""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for row in cursor: tables.append(row['tablename']) for t in tables: query = """DROP TABLE {} CASCADE""".format(t) try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) query = """SELECT tablename FROM pg_tables WHERE schemaname = 'public'""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for row in cursor: raise Exception("Not all tables deleted, found table %s" % row['tablename']) # Build structure sync_module.main(fix=True, print_output=False) # Populate it with the test data path = "{}/test_lib/test_setup.sql".format(sys.path[0]) with open(path) as f: query = dummy_data + f.read() if query != "": try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) # Test to ensure we list the right number of tables tables = 0 query = """SELECT tablename FROM pg_tables WHERE schemaname = 'public'""" try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) for row in cursor: tables += 1 if tables != len(sync_module.table_list): raise Exception("Error: Tables listed ({}) does not match the length of sync.table_list: {}".format(tables, len(sync_module.table_list)))
def install(options): from profiteer import sync as sync_module """Installs the system""" # Setup database connection if config.get('db_username') == "username": if config.get('db_password') == "password": print(cli_f.shell_text("[y]No database login[/y]")) print(""" Access to the database has not yet been setup. Open config.json and fill in values for db_host, db_username, db_password and db_name. You can optionally also create values for test and mock databases too. These will allow you to run unit tests involving the database and to trial code against mock data. When ready, run this program again.""") return False # Test database connection try: cursor = database_f.get_cursor() except Exception: print(cli_f.shell_text("[r]Database not accessible[/r]")) print(""" The login details for the database are incorrect. Double check the db_host, db_username, db_password and db_name fields in config.json When ready, run this program again to retry the connection.""") return False print(cli_f.shell_text("[g]Connected to database[/g]")) # Install database o = sync_module.main(fix=True, show_fixes=False, print_output=False) # Insert admin query = """UPDATE users SET password = '******' WHERE id = 1;""".format(user.encode_password('password', 'K*WJgU&j8M) ZT?=J_T-TUfH9*lY#!>@')) try: cursor.execute(query) except Exception as e: raise Exception("Database error: %s\nQuery: %s" % (str(e.args[0]).replace("\n",""), query)) print(cli_f.shell_text("[g]Database installed[/g]")) return True
def sync(options, fix=False): if options.verbose: print(sync_module.main(fix=fix, show_fixes=True)) else: print(sync_module.main(fix=fix))