本文整理汇总了Python中airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook.update_database方法的典型用法代码示例。如果您正苦于以下问题:Python CloudSpannerHook.update_database方法的具体用法?Python CloudSpannerHook.update_database怎么用?Python CloudSpannerHook.update_database使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook
的用法示例。
在下文中一共展示了CloudSpannerHook.update_database方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestGcpSpannerHookNoDefaultProjectID
# 需要导入模块: from airflow.contrib.hooks.gcp_spanner_hook import CloudSpannerHook [as 别名]
# 或者: from airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook import update_database [as 别名]
#.........这里部分代码省略.........
database_method = instance_method.return_value.database
database_create_method = database_method.return_value.create
with self.assertRaises(AirflowException) as cm:
self.spanner_hook_no_default_project_id.create_database(
instance_id=SPANNER_INSTANCE,
database_id=SPANNER_DATABASE,
ddl_statements=[])
get_client.assert_not_called()
instance_method.assert_not_called()
database_method.assert_not_called()
database_create_method.assert_not_called()
err = cm.exception
self.assertIn("The project id must be passed", str(err))
@mock.patch('airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook._get_client')
def test_create_database_overridden_project_id(self, get_client):
instance_method = get_client.return_value.instance
instance_exists_method = instance_method.return_value.exists
instance_exists_method.return_value = True
database_method = instance_method.return_value.database
database_create_method = database_method.return_value.create
res = self.spanner_hook_no_default_project_id.create_database(
project_id=GCP_PROJECT_ID_HOOK_UNIT_TEST,
instance_id=SPANNER_INSTANCE,
database_id=SPANNER_DATABASE,
ddl_statements=[])
get_client.assert_called_once_with(project_id='example-project')
instance_method.assert_called_once_with(instance_id='instance')
database_method.assert_called_with(database_id='database-name', ddl_statements=[])
database_create_method.assert_called_with()
self.assertIsNone(res)
@mock.patch('airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook._get_client')
def test_update_database_missing_project_id(self, get_client):
instance_method = get_client.return_value.instance
instance_exists_method = instance_method.return_value.exists
instance_exists_method.return_value = True
database_method = instance_method.return_value.database
database_update_method = database_method.return_value.update
with self.assertRaises(AirflowException) as cm:
self.spanner_hook_no_default_project_id.update_database(
instance_id=SPANNER_INSTANCE,
database_id=SPANNER_DATABASE,
ddl_statements=[])
get_client.assert_not_called()
instance_method.assert_not_called()
database_method.assert_not_called()
database_update_method.assert_not_called()
err = cm.exception
self.assertIn("The project id must be passed", str(err))
@mock.patch('airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook._get_client')
def test_update_database_overridden_project_id(self, get_client):
instance_method = get_client.return_value.instance
instance_exists_method = instance_method.return_value.exists
instance_exists_method.return_value = True
database_method = instance_method.return_value.database
database_update_ddl_method = database_method.return_value.update_ddl
res = self.spanner_hook_no_default_project_id.update_database(
project_id=GCP_PROJECT_ID_HOOK_UNIT_TEST,
instance_id=SPANNER_INSTANCE,
database_id=SPANNER_DATABASE,
ddl_statements=[])
get_client.assert_called_once_with(project_id='example-project')
instance_method.assert_called_once_with(instance_id='instance')
database_method.assert_called_with(database_id='database-name')
示例2: CloudSpannerInstanceDatabaseUpdateOperator
# 需要导入模块: from airflow.contrib.hooks.gcp_spanner_hook import CloudSpannerHook [as 别名]
# 或者: from airflow.contrib.hooks.gcp_spanner_hook.CloudSpannerHook import update_database [as 别名]
class CloudSpannerInstanceDatabaseUpdateOperator(BaseOperator):
"""
Updates a Cloud Spanner database with the specified DDL statement.
.. seealso::
For more information on how to use this operator, take a look at the guide:
:ref:`howto/operator:CloudSpannerInstanceDatabaseUpdateOperator`
: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: list[str]
:param project_id: Optional, the ID of the project that owns the the Cloud Spanner
Database. If set to None or missing, the default project_id from the GCP connection is used.
:type project_id: 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,
instance_id,
database_id,
ddl_statements,
project_id=None,
operation_id=None,
gcp_conn_id='google_cloud_default',
*args, **kwargs):
self.instance_id = instance_id
self.project_id = project_id
self.database_id = database_id
self.ddl_statements = ddl_statements
self.operation_id = operation_id
self.gcp_conn_id = gcp_conn_id
self._validate_inputs()
self._hook = CloudSpannerHook(gcp_conn_id=gcp_conn_id)
super(CloudSpannerInstanceDatabaseUpdateOperator, self).__init__(*args, **kwargs)
def _validate_inputs(self):
if 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"
" or None")
if not self.database_id:
raise AirflowException("The required parameter 'database_id' is empty"
" or None")
if not self.ddl_statements:
raise AirflowException("The required parameter 'ddl_statements' is empty"
" or None")
def execute(self, context):
if not self._hook.get_database(project_id=self.project_id,
instance_id=self.instance_id,
database_id=self.database_id):
raise AirflowException("The Cloud Spanner database '{}' in project '{}' and "
"instance '{}' is missing. Create the database first "
"before you can update it.".format(self.database_id,
self.project_id,
self.instance_id))
else:
return self._hook.update_database(project_id=self.project_id,
instance_id=self.instance_id,
database_id=self.database_id,
ddl_statements=self.ddl_statements,
operation_id=self.operation_id)