kaxil commented on a change in pull request #4353: [AIRFLOW-3480] Add Database 
Deploy/Update/Delete operators
URL: https://github.com/apache/incubator-airflow/pull/4353#discussion_r245113466
 
 

 ##########
 File path: airflow/contrib/operators/gcp_spanner_operator.py
 ##########
 @@ -197,3 +200,201 @@ def execute(self, context):
     def sanitize_queries(queries):
         if len(queries) and queries[-1] == '':
             del queries[-1]
+
+
+class CloudSpannerInstanceDatabaseDeployOperator(BaseOperator):
+    """
+    Creates a new Cloud Spanner database, or if database exists,
+    the operator does nothing.
+
+
+    :param project_id: The ID of the project that owns the Cloud Spanner 
Database.
+    :type project_id: str
+    :param instance_id: The Cloud Spanner instance ID.
+    :type instance_id: str
+    :param database_id: The Cloud Spanner database ID.
+    :type database_id: str
+    :param ddl_statements: The string list containing DDL for the new database.
+    :type ddl_statements: [str]
+    :param gcp_conn_id: The connection ID used to connect to Google Cloud 
Platform.
+    :type gcp_conn_id: str
+    """
+    # [START gcp_spanner_database_deploy_template_fields]
+    template_fields = ('project_id', 'instance_id', 'database_id', 
'ddl_statements',
+                       'gcp_conn_id')
+    template_ext = ('.sql', )
+    # [END gcp_spanner_database_deploy_template_fields]
+
+    @apply_defaults
+    def __init__(self,
+                 project_id,
+                 instance_id,
+                 database_id,
+                 ddl_statements,
+                 gcp_conn_id='google_cloud_default',
+                 *args, **kwargs):
+        # type: (str, str, str, [str], str, object, object) -> None
+        self.instance_id = instance_id
+        self.project_id = project_id
+        self.database_id = database_id
+        self.ddl_statements = ddl_statements
+        self.gcp_conn_id = gcp_conn_id
+        self._validate_inputs()
+        self._hook = CloudSpannerHook(gcp_conn_id=gcp_conn_id)
+        super(CloudSpannerInstanceDatabaseDeployOperator, 
self).__init__(*args, **kwargs)
+
+    def _validate_inputs(self):
+        if not self.project_id:
+            raise AirflowException("The required parameter 'project_id' is 
empty")
+        if not self.instance_id:
+            raise AirflowException("The required parameter 'instance_id' is 
empty")
+        if not self.database_id:
+            raise AirflowException("The required parameter 'database_id' is 
empty")
+        if not self.ddl_statements:
+            raise AirflowException("The required parameter 'ddl_statements' is 
empty")
+
+    def execute(self, context):
+        if not self._hook.get_database(self.project_id,
+                                       self.instance_id,
+                                       self.database_id):
+            self.log.info("Creating Cloud Spanner database "
+                          "'{}' in project '{}' and instance '{}'".
+                          format(self.database_id, self.project_id, 
self.instance_id))
+            return self._hook.create_database(project_id=self.project_id,
+                                              instance_id=self.instance_id,
+                                              database_id=self.database_id,
+                                              
ddl_statements=self.ddl_statements)
+        else:
+            self.log.info("The database '{}' in project '{}' and instance '{}'"
+                          " already exists. Nothing to do. Exiting.".
+                          format(self.database_id, self.project_id, 
self.instance_id))
+        return True
+
+
+class CloudSpannerInstanceDatabaseUpdateOperator(BaseOperator):
+    """
+    Updates a Cloud Spanner database with the specified DDL statement.
+
+    :param project_id: The ID of the project that owns the the Cloud Spanner 
Database.
+    :type project_id: str
+    :param instance_id: The Cloud Spanner instance ID.
+    :type instance_id: str
+    :param database_id: The Cloud Spanner database ID.
+    :type database_id: str
+    :param ddl_statements: The string list containing DDL to apply to the 
database.
+    :type ddl_statements: [str]
+    :param operation_id: (Optional) Unique per database operation id that can
+           be specified to implement idempotency check.
+    :type operation_id: [str]
+    :param gcp_conn_id: The connection ID used to connect to Google Cloud 
Platform.
+    :type gcp_conn_id: str
+    """
+    # [START gcp_spanner_database_update_template_fields]
+    template_fields = ('project_id', 'instance_id', 'database_id', 
'ddl_statements',
+                       'gcp_conn_id')
+    template_ext = ('.sql', )
+    # [END gcp_spanner_database_update_template_fields]
+
+    @apply_defaults
+    def __init__(self,
+                 project_id,
 
 Review comment:
   I am happy for you to make this change in the next PR, provided it won't be 
long before we have that PR ready so that we can include it in 1.10.2

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to