def _sql_script(client, sql, database, job_name, credential_id): job_name = maybe_get_random_name(job_name) db_id = client.get_database_id(database) cred_id = credential_id or client.default_credential export_job = client.scripts.post_sql(job_name, remote_host_id=db_id, credential_id=cred_id, sql=sql) run_job = client.scripts.post_sql_runs(export_job.id) return export_job.id, run_job.id
def _sql_script(client, sql, database, job_name, credential_id, hidden=False, csv_settings=None): job_name = maybe_get_random_name(job_name) db_id = client.get_database_id(database) cred_id = credential_id or client.default_credential csv_settings = csv_settings or {} export_job = client.scripts.post_sql(job_name, remote_host_id=db_id, credential_id=cred_id, sql=sql, hidden=hidden, csv_settings=csv_settings) run_job = client.scripts.post_sql_runs(export_job.id) return export_job.id, run_job.id
def test_maybe_random_name_not_random(): given_name = '22222' assert maybe_get_random_name(given_name) == given_name
def test_maybe_random_name_random(mock_uuid): random_name = '11111' mock_uuid.uuid4.return_value = mock.Mock(hex=random_name) assert maybe_get_random_name(None) == random_name
def transfer_table(source_db, dest_db, source_table, dest_table, job_name=None, api_key=None, source_credential_id=None, dest_credential_id=None, polling_interval=_DEFAULT_POLLING_INTERVAL, **advanced_options): """Transfer a table from one location to another. Parameters ---------- source_db : str or int The name of the database where the source table is located. Optionally, could be the database ID. dest_db : str or int The name of the database where the table will be transfered. Optionally, could be the database ID. source_table : str Full name of the table to transfer, e.g., ``'schema.table'``. dest_table : str Full name of the table in the destination database, e.g., ``'schema.table'``. job_name : str, optional A name to give the job. If omitted, a random job name will be used. api_key : str, optional Your Civis API key. If not given, the :envvar:`CIVIS_API_KEY` environment variable will be used. source_credential_id : str or int, optional Optional credential ID for the source database. If ``None``, the default credential will be used. dest_credential_id : str or int, optional Optional credential ID for the destination database. If ``None``, the default credential will be used. polling_interval : int or float, optional Number of seconds to wait between checks for job completion. **advanced_options : kwargs Extra keyword arguments will be passed to the import sync job. See :func:`~civis.resources._resources.Imports.post_syncs`. Returns ------- results : :class:`~civis.polling.PollableResult` A `PollableResult` object. Examples -------- >>> transfer_table(source_db='Cluster A', dest_db='Cluster B', ... source_table='schma.tbl', dest_table='schma.tbl') """ client = APIClient(api_key=api_key) source_cred_id = source_credential_id or client.default_credential dest_cred_id = dest_credential_id or client.default_credential job_name = maybe_get_random_name(job_name) source = { 'remote_host_id': client.get_database_id(source_db), 'credential_id': source_cred_id } destination = { 'remote_host_id': client.get_database_id(dest_db), 'credential_id': dest_cred_id } job_id = client.imports.post(job_name, "Dbsync", True, source=source, destination=destination).id client.imports.post_syncs(id=job_id, source={'path': source_table}, destination={'path': dest_table}, advanced_options=advanced_options) run_id = client.imports.post_runs(id=job_id).run_id poll = PollableResult(client.imports.get_files_runs, (job_id, run_id), polling_interval) return poll