def migrate_tables_and_views(apps, schema_editor): # This shouldn't run during tests. If it does, list_all_users # will return actual users, and then crash when it can't find them # in django's test_db if 'test' in sys.argv: return DataHubManager.execute_sql all_users = DataHubManager.list_all_users() # filter out the anonymous user, which doesn't have a db all_users = [username for username in all_users if ( username != settings.ANONYMOUS_ROLE) ] # give users select/update/insert access to their rows in the policy table for username in all_users: try: with DataHubManager(username) as m: res = m.execute_sql( "SELECT table_name FROM information_schema.tables " "WHERE table_schema = 'public'") tables_and_views = [table[0] for table in res['tuples']] move_tables_to_new_schema(username, tables_and_views) except (User.DoesNotExist, OperationalError): pass
def migrate_tables_and_views(apps, schema_editor): # This shouldn't run during tests. If it does, list_all_users # will return actual users, and then crash when it can't find them # in django's test_db if 'test' in sys.argv: return DataHubManager.execute_sql all_users = DataHubManager.list_all_users() # filter out the anonymous user, which doesn't have a db all_users = [ username for username in all_users if (username != settings.ANONYMOUS_ROLE) ] # give users select/update/insert access to their rows in the policy table for username in all_users: try: with DataHubManager(username) as m: res = m.execute_sql( "SELECT table_name FROM information_schema.tables " "WHERE table_schema = 'public'") tables_and_views = [table[0] for table in res['tuples']] move_tables_to_new_schema(username, tables_and_views) except (User.DoesNotExist, OperationalError): pass
def delete_all_test_users(cls): # When building tests, it's possible to delete some combination of the # django user/postgres user/postgres user database # This tries to catch the edge cases. all_users = DataHubManager.list_all_users() test_users = filter(lambda x: x.startswith('delete_me_'), all_users) for user in test_users: try: DataHubManager.remove_user(user, remove_db=True, ignore_missing_user=True) except: print('UNABLE TO DELETE USER ' + user) # Delete all django users whose name starts with 'delete_me_'. all_users = User.objects.all() test_users = all_users.filter(username__startswith='delete_me_') for user in test_users: user.delete()
def delete_all_test_users(self): # When building tests, it's possible to delete some combination of the # django user/postgres user/postgres user database # This tries to catch the edge cases. all_users = DataHubManager.list_all_users() test_users = filter(lambda x: x.startswith('delete_me_'), all_users) for user in test_users: try: DataHubManager.remove_user(user, remove_db=True) except: print('UNABLE TO DELETE USER ' + user) # Delete all django users whose name starts with 'delete_me_'. all_users = User.objects.all() test_users = all_users.filter(username__startswith='delete_me_') for user in test_users: user.delete()
def migrate_tables_and_views(apps, schema_editor): # This shouldn't run during tests. If it does, list_all_users # will return actual users, and then crash when it can't find them # in django's test_db if 'test' in sys.argv: return DataHubManager.execute_sql all_users = DataHubManager.list_all_users() # filter out the anonymous user, which doesn't have a db all_users = [ username for username in all_users if (username != settings.ANONYMOUS_ROLE) ] # give users select/update/insert access to their rows in the policy table for username in all_users: try: with DataHubManager(username) as m: # Disable query rewriting during migrations to avoid a race # condition. The final Collaborator model has a license_id # attribute, but that won't be added to the database until # a later migration. The query rewriter uses Collaborators, # so the migration will fail if it is enabled. m.user_con.backend.row_level_security = False res = m.execute_sql( "SELECT table_name FROM information_schema.tables " "WHERE table_schema = 'public'") tables_and_views = [table[0] for table in res['tuples']] move_tables_to_new_schema(username, tables_and_views) except (User.DoesNotExist, OperationalError): pass