@@ -470,8 +470,7 @@ def record_change_script(self, script, script_content, change_history_table, exe
470
470
query = self ._q_ch_log .format (** frmt_args )
471
471
self .execute_snowflake_query (query )
472
472
473
-
474
- def deploy_command (config ):
473
+ def setup_session (config ):
475
474
req_args = set (['snowflake_account' ,'snowflake_user' ,'snowflake_role' ,'snowflake_warehouse' ])
476
475
validate_auth_config (config , req_args )
477
476
@@ -481,11 +480,9 @@ def deploy_command(config):
481
480
print (_log_config_details .format (** config ))
482
481
483
482
#connect to snowflake and maintain connection
484
- session = SnowflakeSchemachangeSession (config )
485
-
486
- scripts_skipped = 0
487
- scripts_applied = 0
483
+ SnowflakeSchemachangeSession (config )
488
484
485
+ def calculate_repeatable_migration_checksum (config , session ):
489
486
# Deal with the change history table (create if specified)
490
487
change_history_table = get_change_history_table_details (config ['change_history_table' ])
491
488
change_history_metadata = session .fetch_change_history_metadata (change_history_table )
@@ -515,15 +512,20 @@ def deploy_command(config):
515
512
max_published_version_display = 'None'
516
513
print (_log_ch_max_version .format (max_published_version_display = max_published_version_display ))
517
514
518
- # Find all scripts in the root folder (recursively) and sort them correctly
519
- all_scripts = get_all_scripts_recursively (config ['root_folder' ], config ['verbose' ])
520
- all_script_names = list (all_scripts .keys ())
521
- # Sort scripts such that versioned scripts get applied first and then the repeatable ones.
522
- all_script_names_sorted = sorted_alphanumeric ([script for script in all_script_names if script [0 ] == 'V' ]) \
523
- + sorted_alphanumeric ([script for script in all_script_names if script [0 ] == 'R' ]) \
524
- + sorted_alphanumeric ([script for script in all_script_names if script [0 ] == 'A' ])
515
+ [change_history_table , r_scripts_checksum , max_published_version ]
516
+
517
+ def apply_scripts (config , all_scripts , all_script_names_sorted , apply = True ):
518
+ session = setup_session (config )
519
+
520
+ scripts_skipped = 0
521
+ scripts_applied = 0
522
+
523
+ [
524
+ change_history_table ,
525
+ r_scripts_checksum ,
526
+ max_published_version
527
+ ] = calculate_repeatable_migration_checksum (config , session )
525
528
526
- # Loop through each script in order and apply any required changes
527
529
for script_name in all_script_names_sorted :
528
530
script = all_scripts [script_name ]
529
531
@@ -560,23 +562,30 @@ def deploy_command(config):
560
562
print (_log_apply .format (** script ))
561
563
562
564
if not config ['dry_run' ]:
563
- execution_time = session .apply_change_script (script , content , change_history_table )
565
+ execution_time = 0
566
+ if apply :
567
+ execution_time = session .apply_change_script (script , content , change_history_table )
564
568
session .record_change_script (script , content , change_history_table , execution_time )
565
569
scripts_applied += 1
566
570
567
- print ( _log_apply_set_complete . format ( scripts_applied = scripts_applied , scripts_skipped = scripts_skipped ))
571
+ [ scripts_skipped , scripts_applied ]
568
572
569
- def undo_command (config ):
570
- req_args = set (['snowflake_account' ,'snowflake_user' ,'snowflake_role' ,'snowflake_warehouse' , 'step' ])
571
- validate_auth_config (config , req_args )
573
+ def deploy_command (config ):
574
+ # Find all scripts in the root folder (recursively) and sort them correctly
575
+ all_scripts = get_all_scripts_recursively (config ['root_folder' ], config ['verbose' ])
576
+ all_script_names = list (all_scripts .keys ())
577
+ # Sort scripts such that versioned scripts get applied first and then the repeatable ones.
578
+ all_script_names_sorted = sorted_alphanumeric ([script for script in all_script_names if script [0 ] == 'V' ]) \
579
+ + sorted_alphanumeric ([script for script in all_script_names if script [0 ] == 'R' ]) \
580
+ + sorted_alphanumeric ([script for script in all_script_names if script [0 ] == 'A' ])
572
581
573
- # Log some additional details
574
- if config ['dry_run' ]:
575
- print ("Running in dry-run mode" )
576
- print (_log_config_details .format (** config ))
582
+ # Loop through each script in order and apply any required changes
583
+ [scripts_skipped , scripts_applied ] = apply_scripts (config , all_scripts , all_script_names_sorted , True )
577
584
578
- #connect to snowflake and maintain connection
579
- session = SnowflakeSchemachangeSession (config )
585
+ print (_log_apply_set_complete .format (scripts_applied = scripts_applied , scripts_skipped = scripts_skipped ))
586
+
587
+ def undo_command (config ):
588
+ session = setup_session (config )
580
589
581
590
# Deal with the change history table (raise if not provided)
582
591
change_history_table = get_change_history_table_details (config ['change_history_table' ])
@@ -615,86 +624,13 @@ def undo_command(config):
615
624
print (_log_undo_set_complete .format (scripts_applied = scripts_applied ))
616
625
617
626
def recalculate_checksum_command (config ):
618
- req_args = set (['snowflake_account' ,'snowflake_user' ,'snowflake_role' ,'snowflake_warehouse' ])
619
- validate_auth_config (config , req_args )
620
-
621
- # Log some additional details
622
- if config ['dry_run' ]:
623
- print ("Running in dry-run mode" )
624
- print (_log_config_details .format (** config ))
625
-
626
- #connect to snowflake and maintain connection
627
- session = SnowflakeSchemachangeSession (config )
628
-
629
- scripts_skipped = 0
630
- scripts_applied = 0
631
-
632
- # Deal with the change history table (create if specified)
633
- change_history_table = get_change_history_table_details (config ['change_history_table' ])
634
- change_history_metadata = session .fetch_change_history_metadata (change_history_table )
635
- if change_history_metadata :
636
- print (_log_ch_use .format (last_altered = change_history_metadata ['last_altered' ], ** change_history_table ))
637
- elif config ['create_change_history_table' ]:
638
- # Create the change history table (and containing objects) if it don't exist.
639
- if not config ['dry_run' ]:
640
- session .create_change_history_table_if_missing (change_history_table )
641
- print (_log_ch_create .format (** change_history_table ))
642
- else :
643
- raise ValueError (_err_ch_missing .format (** change_history_table ))
644
-
645
- # Find the max published version
646
- max_published_version = ''
647
-
648
- change_history = None
649
- r_scripts_checksum = None
650
- if (config ['dry_run' ] and change_history_metadata ) or not config ['dry_run' ]:
651
- change_history = session .fetch_change_history (change_history_table )
652
- r_scripts_checksum = session .fetch_r_scripts_checksum (change_history_table )
653
-
654
- if change_history :
655
- max_published_version = change_history [0 ]
656
- max_published_version_display = max_published_version
657
- if max_published_version_display == '' :
658
- max_published_version_display = 'None'
659
- print (_log_ch_max_version .format (max_published_version_display = max_published_version_display ))
660
-
661
627
# Find all scripts in the root folder (recursively) and sort them correctly
662
628
all_scripts = get_all_scripts_recursively (config ['root_folder' ], config ['verbose' ])
663
629
all_script_names = list (all_scripts .keys ())
664
630
# Sort scripts such that versioned scripts get applied first and then the repeatable ones.
665
631
all_script_names_sorted = sorted_alphanumeric ([script for script in all_script_names if script [0 ] == 'R' ])
666
632
667
- # Loop through each script in order and apply any required changes
668
- for script_name in all_script_names_sorted :
669
- script = all_scripts [script_name ]
670
-
671
- # Always process with jinja engine
672
- jinja_processor = JinjaTemplateProcessor (project_root = config ['root_folder' ], modules_folder = config ['modules_folder' ])
673
- content = jinja_processor .render (jinja_processor .relpath (script ['script_full_path' ]), config ['vars' ], config ['verbose' ])
674
-
675
- # Apply only R scripts where the checksum changed compared to the last execution of snowchange
676
- if script_name [0 ] == 'R' :
677
- # Compute the checksum for the script
678
- checksum_current = hashlib .sha224 (content .encode ('utf-8' )).hexdigest ()
679
-
680
- # check if R file was already executed
681
- if (r_scripts_checksum is not None ) and script_name in list (r_scripts_checksum ['script_name' ]):
682
- checksum_last = list (r_scripts_checksum .loc [r_scripts_checksum ['script_name' ] == script_name , 'checksum' ])[0 ]
683
- else :
684
- checksum_last = ''
685
-
686
- # check if there is a change of the checksum in the script
687
- if checksum_current == checksum_last :
688
- if config ['verbose' ]:
689
- print (_log_skip_r .format (** script ))
690
- scripts_skipped += 1
691
- continue
692
-
693
- print (_log_recalculate .format (** script ))
694
-
695
- if not config ['dry_run' ]:
696
- session .record_change_script (script , content , change_history_table , 0 )
697
- scripts_applied += 1
633
+ [scripts_applied , scripts_skipped ] = apply_scripts (config , all_scripts , all_script_names_sorted , False )
698
634
699
635
print (_log_apply_set_complete .format (scripts_applied = scripts_applied , scripts_skipped = scripts_skipped ))
700
636
0 commit comments