import os
from django.core.management import BaseCommand, CommandError
from gcutils.bigquery import Client


class Command(BaseCommand):
    help = 'Removes any datasets whose tables have all expired'

    def handle(self, *args, **kwargs):
        if os.environ['DJANGO_SETTINGS_MODULE'] != \
                'openprescribing.settings.test':
            raise CommandError('Command must run with test settings')

        gcbq_client = Client().gcbq_client

        for dataset_list_item in gcbq_client.list_datasets():
            dataset_ref = dataset_list_item.reference
            tables = list(gcbq_client.list_tables(dataset_ref))
            if len(tables) == 0:
                gcbq_client.delete_dataset(dataset_ref)

import os
from django.core.management import BaseCommand, CommandError
from gcutils.bigquery import Client


class Command(BaseCommand):
    help = 'Removes any datasets whose tables have all expired'

    def handle(self, *args, **kwargs):
        if os.environ['DJANGO_SETTINGS_MODULE'] != \
                'openprescribing.settings.test':
            raise CommandError('Command must run with test settings')

        gcbq_client = Client().gcbq_client

        datasets = list(gcbq_client.list_datasets())

        for dataset_list_item in datasets:
            dataset_ref = dataset_list_item.reference
            tables = list(gcbq_client.list_tables(dataset_ref))
            if len(tables) == 0:
                gcbq_client.delete_dataset(dataset_ref)

Clean up BQ test data properly
Clean up BQ test data properly

If you delete datasets while iterating over datasets, you eventually get
errors.  This fixes that by building a list of all datasets before we
delete any.

  import os
  from django.core.management import BaseCommand, CommandError
  from gcutils.bigquery import Client
  
  
  class Command(BaseCommand):
      help = 'Removes any datasets whose tables have all expired'
  
      def handle(self, *args, **kwargs):
          if os.environ['DJANGO_SETTINGS_MODULE'] != \
                  'openprescribing.settings.test':
              raise CommandError('Command must run with test settings')
  
          gcbq_client = Client().gcbq_client
  
+         datasets = list(gcbq_client.list_datasets())
+ 
-         for dataset_list_item in gcbq_client.list_datasets():
+         for dataset_list_item in datasets:
              dataset_ref = dataset_list_item.reference
              tables = list(gcbq_client.list_tables(dataset_ref))
              if len(tables) == 0:
                  gcbq_client.delete_dataset(dataset_ref)
  
Clean up BQ test data properly
## Code Before:
import os
from django.core.management import BaseCommand, CommandError
from gcutils.bigquery import Client


class Command(BaseCommand):
    help = 'Removes any datasets whose tables have all expired'

    def handle(self, *args, **kwargs):
        if os.environ['DJANGO_SETTINGS_MODULE'] != \
                'openprescribing.settings.test':
            raise CommandError('Command must run with test settings')

        gcbq_client = Client().gcbq_client

        for dataset_list_item in gcbq_client.list_datasets():
            dataset_ref = dataset_list_item.reference
            tables = list(gcbq_client.list_tables(dataset_ref))
            if len(tables) == 0:
                gcbq_client.delete_dataset(dataset_ref)

## Instruction:
Clean up BQ test data properly
## Code After:
import os
from django.core.management import BaseCommand, CommandError
from gcutils.bigquery import Client


class Command(BaseCommand):
    help = 'Removes any datasets whose tables have all expired'

    def handle(self, *args, **kwargs):
        if os.environ['DJANGO_SETTINGS_MODULE'] != \
                'openprescribing.settings.test':
            raise CommandError('Command must run with test settings')

        gcbq_client = Client().gcbq_client

        datasets = list(gcbq_client.list_datasets())

        for dataset_list_item in datasets:
            dataset_ref = dataset_list_item.reference
            tables = list(gcbq_client.list_tables(dataset_ref))
            if len(tables) == 0:
                gcbq_client.delete_dataset(dataset_ref)
