# Copyright 2018 Google LLC. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
- release_tracks: [ALPHA]
  help_text:
    brief: Update a new BigQuery table.
    description: |-
      Create a new BigQuery table.
      Updates one or more attributes of a table or view.
    examples: |
      The following command updates the description on a table with ID  `my-table` in `my-dataset`:

        $ {command}  /projects/myproject/datasets/my-dataset/tables/my-table --description 'My New Table'

      The following command changes the schema mode from `REQUIRED` to `NULLABLE` on the `value`
      and `tags` columns in  a table with ID `my-other-table` in dataset `my-other-dataset`:

        $ {command}  my-other-table --dataset my-other-dataset \
          --relax-columns name,tags

  request:
    collection: bigquery.tables
    method: patch
    modify_request_hooks:
    - googlecloudsdk.command_lib.util.hooks.request_modifiers:SetFieldFromName:api_field=table.tableReference.tableId
    - googlecloudsdk.command_lib.bq.hooks:ProcessSchemaUpdate
  arguments:
    resource:
      help_text: The BigQuery table you want to update.
      spec: !REF googlecloudsdk.command_lib.bq.resources:table

    params:
    - _REF_: googlecloudsdk.command_lib.bq.flags:table.description
    - _REF_: googlecloudsdk.command_lib.bq.flags:table.expiration
    - arg_name: relax-columns
      required: false
      metavar: FIELD_NAME
      type: 'googlecloudsdk.calliope.arg_parsers:ArgList:'
      help_text: |
        A comma-separated list of field names in the current schema that should have their mode
        changed from REQUIRED to NULLABLE.

        For more details on updating and managing BigQuery schemas see:
        https://cloud.google.com/bigquery/docs/managing-table-schemas
    - group:
        mutex: true
        required: false
        help_text: Specify changes to the table schema.
        params:
        - arg_name: add-columns
          metavar: FIELD_NAME=FIELD_TYPE
          api_field: table.schema.fields
          type:
            arg_dict:
              flatten: true
              spec:
              - api_field: name
              - api_field: type
          help_text: |-
            A comma-separated list of entries of the form FIELD_NAME[=FIELD_TYPE] specifying field names
            and types for the columns being  added to the table. FIELD_TYPE defaults to string if not present.
            Possible FIELD_TYPES are `string`, `integer`, `float`, `boolean`, `record`, and `timestamp`.

            For more details on BigQuery schemas see: https://cloud.google.com/bigquery/docs/schemas.
        - arg_name: add-columns-file
          api_field: table.schema
          type: "googlecloudsdk.calliope.arg_parsers:FileContents:"
          processor: googlecloudsdk.command_lib.bq.hooks:BqTableSchemaFileProcessor
          help_text: |-
            The name of a JSON file containing a single object containing an array each element
            of which is an object with properties name, type, and, optionally a
            mode (one of: `NULLABLE` or `REPEATED`), specifying the columns to be added
            to the table. If mode is omitted the default is 'NULLABLE'.

            For example:
            {
              'schema':
                [
                  {
                    'name': 'field1',
                    'type': 'string',
                    'mode': 'REQUIRED'
                  },
                  {
                    'name': 'field2',
                    'type': 'integer',
                    'mode': 'REPEATED'
                  },
                  [
                    {
                      'name': 'fieldN',
                      'type': TYPE,
                      ['mode': MODE]
                    }
                    ...
                  ]
                ]
            }

            For more details on BigQuery schemas see: https://cloud.google.com/bigquery/docs/schemas.
