Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Support for schema syncing via cli #196

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 12 additions & 10 deletions schemachange/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,18 +202,19 @@ class SnowflakeSchemachangeSession:
+ " EXECUTION_TIME NUMBER, STATUS VARCHAR, INSTALLED_BY VARCHAR, INSTALLED_ON TIMESTAMP_LTZ)"
_q_ch_r_checksum = "SELECT DISTINCT SCRIPT, FIRST_VALUE(CHECKSUM) OVER (PARTITION BY SCRIPT " \
+ "ORDER BY INSTALLED_ON DESC) FROM {database_name}.{schema_name}.{table_name} WHERE SCRIPT_TYPE = 'R' AND " \
+ "STATUS = 'Success'"
_q_ch_fetch ="SELECT VERSION FROM {database_name}.{schema_name}.{table_name} WHERE SCRIPT_TYPE = 'V' ORDER" \
+ "STATUS = 'Success' AND SCHEMA = '{database_schema}'"
_q_ch_fetch ="SELECT VERSION FROM {database_name}.{schema_name}.{table_name} WHERE SCRIPT_TYPE = 'V' AND SCHEMA = '{database_schema}' ORDER" \
+ " BY INSTALLED_ON DESC LIMIT 1"
_q_sess_tag = "ALTER SESSION SET QUERY_TAG = '{query_tag}'"
_q_ch_log = "INSERT INTO {database_name}.{schema_name}.{table_name} (VERSION, DESCRIPTION, SCRIPT, SCRIPT_TYPE, " \
+ "CHECKSUM, EXECUTION_TIME, STATUS, INSTALLED_BY, INSTALLED_ON) values ('{script_version}'," \
_q_ch_log = "INSERT INTO {database_name}.{schema_name}.{table_name} (VERSION, SCHEMA, DESCRIPTION, SCRIPT, SCRIPT_TYPE, " \
+ "CHECKSUM, EXECUTION_TIME, STATUS, INSTALLED_BY, INSTALLED_ON) values ('{script_version}','{database_schema}'," \
+ "'{script_description}','{script_name}','{script_type}','{checksum}',{execution_time}," \
+ "'{status}','{user}',CURRENT_TIMESTAMP);"
_q_set_sess_role = 'USE ROLE {role};'
_q_set_sess_database = 'USE DATABASE {database};'
_q_set_sess_schema = 'USE SCHEMA {schema};'
_q_set_sess_warehouse = 'USE WAREHOUSE {warehouse};'
_q_set_sess_schema = 'USE SCHEMA {schema};'
#endregion Query Templates


Expand All @@ -224,7 +225,7 @@ def __init__(self, config):
if config['query_tag']:
session_parameters["QUERY_TAG"] += ";%s" % config['query_tag']

# Retreive Connection info from config dictionary
# Retrieve Connection info from config dictionary
self.conArgs = {"user": config['snowflake_user'],"account": config['snowflake_account'] \
,"role": config['snowflake_role'],"warehouse": config['snowflake_warehouse'] \
,"database": config['snowflake_database'],"schema": config['snowflake_schema'], "application": _snowflake_application_name \
Expand Down Expand Up @@ -503,6 +504,9 @@ def deploy_command(config):

# Deal with the change history table (create if specified)
change_history_table = get_change_history_table_details(config['change_history_table'])
change_history_table["database_schema"] = (
config.get("snowflake_schema", "NONE")
)
change_history_metadata = session.fetch_change_history_metadata(change_history_table)
if change_history_metadata:
print(_log_ch_use.format(last_altered=change_history_metadata['last_altered'], **change_history_table))
Expand Down Expand Up @@ -644,8 +648,7 @@ def get_schemachange_config(config_file_path, root_folder, modules_folder, snowf
"modules_folder":modules_folder, "snowflake_account":snowflake_account, \
"snowflake_user":snowflake_user, "snowflake_role":snowflake_role, \
"snowflake_warehouse":snowflake_warehouse, "snowflake_database":snowflake_database, \
"snowflake_schema":snowflake_schema, \
"change_history_table":change_history_table, "vars":vars, \
"snowflake_schema": snowflake_schema, "change_history_table":change_history_table, "vars":vars, \
"create_change_history_table":create_change_history_table, \
"autocommit":autocommit, "verbose":verbose, "dry_run":dry_run,\
"query_tag":query_tag, "oauth_config":oauth_config}
Expand Down Expand Up @@ -861,9 +864,8 @@ def main(argv=sys.argv):
#nullify expected null values for render.
if args.subcommand == 'render':
renderoveride = {"snowflake_account":None,"snowflake_user":None,"snowflake_role":None, \
"snowflake_warehouse":None,"snowflake_database":None,"change_history_table":None, \
"snowflake_schema":None,"create_change_history_table":None,"autocommit":None, \
"dry_run":None,"query_tag":None,"oauth_config":None }
"snowflake_warehouse":None,"snowflake_database":None,"snowflake_schema": None,"change_history_table":None, \
"create_change_history_table":None,"autocommit":None,"dry_run":None,"query_tag":None,"oauth_config":None }
schemachange_args.update(renderoveride)
config = get_schemachange_config(**schemachange_args)

Expand Down