I am trying to create a scheduled query using the bq command-line tool storing the query to be executed on a sql file named scheduled_query.sql, but I am not able to have it working. I am checking this documentation.
I want to schedule the following query:
INSERT INTO github_actions_test_dataset.timestamp_to_unix
SELECT
CURRENT_DATETIME("Europe/Madrid") AS timestamp,
UNIX_MILLIS(CURRENT_TIMESTAMP()) AS unix_miliseconds
For this, I have executed the following command with success:
bq mk --transfer_config --display_name='Example Scheduled Query' --target_dataset=github_actions_test_dataset --service_account_name=my-sa@my-project.iam.gserviceaccount.com --data_source=scheduled_query --params='{"query":"INSERT INTO github_actions_test_dataset.timestamp_to_unix SELECT CURRENT_DATETIME(\"Europe/Madrid\") AS timestamp, UNIX_MILLIS(CURRENT_TIMESTAMP()) AS unix_miliseconds"}'
However, instead of having to write the query in the command I want to retrieve it from a .sql file, and feed it to the command. I tried using sed 's/"/\\"/g' scheduled_query.sql in order to escape de " character in my query like this:
bq mk --transfer_config --display_name='Example Scheduled Query' --target_dataset=$DATASET --service_account_name=github-actions-sa@my-project.iam.gserviceaccount.com --data_source=scheduled_query --params='{"query":"'$(sed 's/"/\\"/g' scheduled_query.sql)'"}'
But again received
Too many positional args, still have ['SELECT', 'CURRENT_DATETIME(\\"Europe/Madrid\\")', 'AS', 'timestamp,', 'UNIX_MILLIS(CURRENT_TIMESTAMP())', 'AS', 'unix_miliseconds"}']
The solution may be more related on how to concatenate quoted strings on a bash command. What am I doing wrong? Note that I want to use the bq mk command, not the bk query one.
