Exemple #1
0
    def create(self, request, *args, **kwargs):

        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)

        schema = serializer.save()

        if request.data.get('discover'):
            adapter = DatabaseAdapter()

            for table_metadata in adapter.fetch_tables(schema.name):
                table_metadata['schema'] = schema.id
                table_metadata['groups'] = [
                    group.id for group in schema.groups.all()
                ]
                for key in [
                        'license', 'access_level', 'metadata_access_level'
                ]:
                    table_metadata[key] = getattr(schema, key)

                table_serializer = TableSerializer(data=table_metadata)
                if table_serializer.is_valid():
                    table = table_serializer.save()

                    for column_metadata in adapter.fetch_columns(
                            schema.name, table.name):
                        column_metadata['table'] = table.id
                        column_metadata['groups'] = [
                            group.id for group in table.groups.all()
                        ]
                        for key in ['access_level', 'metadata_access_level']:
                            column_metadata[key] = getattr(table, key)

                        column_serializer = ColumnSerializer(
                            data=column_metadata)
                        if column_serializer.is_valid():
                            column_serializer.save()

        headers = self.get_success_headers(serializer.data)
        return Response(serializer.data,
                        status=status.HTTP_201_CREATED,
                        headers=headers)
Exemple #2
0
    def handle(self, *args, **options):
        if options['user']:
            usernames = [options['user']]
        else:
            usernames = ['anonymous'] + list(User.objects.values_list('username', flat=True))

        adapter = DatabaseAdapter()

        stale_tables = []
        for username in usernames:
            schema_name = settings.QUERY_USER_SCHEMA_PREFIX + username

            tables = adapter.fetch_tables(schema_name)
            jobs = QueryJob.objects.filter(schema_name=schema_name)

            for table in tables:
                job = QueryJob.objects.filter(
                    schema_name=schema_name,
                    table_name=table['name']
                ).first()

                if job and job.phase not in [QueryJob.PHASE_EXECUTING, QueryJob.PHASE_COMPLETED]:
                    stale_tables.append((schema_name, table['name'], job.phase if job else None))

        if stale_tables:
            print('The following database tables have no associated QueryJob:')

            for stale_table in stale_tables:
                print('%s.%s -> %s' % stale_table)

            if options['delete']:
                for schema_name, table_name, phase in stale_tables:
                    adapter.drop_table(schema_name, table_name)

                print('The tables have been deleted.')
        else:
            print('No tables without associated QueryJob have been found.')