|
849 | 849 | expect(history_records.first['input_b'].to_i).to eq(456) |
850 | 850 | end |
851 | 851 | end |
| 852 | + |
| 853 | + # Test for renamed dynamic-model fields keeping history tables and triggers in sync. |
| 854 | + # This protects create/update paths after field names change in app configuration. |
| 855 | + context 'field renames' do |
| 856 | + before :all do |
| 857 | + create_admin |
| 858 | + create_user |
| 859 | + |
| 860 | + @original_allow_migrations = Settings::AllowDynamicMigrations |
| 861 | + change_setting('AllowDynamicMigrations', true) |
| 862 | + |
| 863 | + @schema_name = 'dynamic_test' |
| 864 | + @table_name = 'test_field_renames' |
| 865 | + @history_table_name = 'test_field_rename_history' |
| 866 | + |
| 867 | + DynamicModel.active.where(table_name: @table_name).each { |dm| dm.disable!(@admin) } |
| 868 | + DynamicModel.send(:remove_const, :TestFieldRename) if defined?(DynamicModel::TestFieldRename) |
| 869 | + |
| 870 | + @conn = ActiveRecord::Base.connection |
| 871 | + @conn.execute("DROP TABLE IF EXISTS #{@schema_name}.#{@history_table_name} CASCADE") |
| 872 | + @conn.execute("DROP TABLE IF EXISTS #{@schema_name}.#{@table_name} CASCADE") |
| 873 | + |
| 874 | + @dm = DynamicModel.create!( |
| 875 | + current_admin: @admin, |
| 876 | + name: 'Test Field Renames', |
| 877 | + table_name: @table_name, |
| 878 | + schema_name: @schema_name, |
| 879 | + primary_key_name: :id, |
| 880 | + foreign_key_name: :master_id, |
| 881 | + category: :test, |
| 882 | + field_list: 'select_still_interested select_continue_now notes', |
| 883 | + options: <<~YAML |
| 884 | + _db_columns: |
| 885 | + select_still_interested: |
| 886 | + type: string |
| 887 | + select_continue_now: |
| 888 | + type: string |
| 889 | + notes: |
| 890 | + type: string |
| 891 | + YAML |
| 892 | + ) |
| 893 | + |
| 894 | + @dm.update_tracker_events |
| 895 | + end |
| 896 | + |
| 897 | + after :all do |
| 898 | + @dm&.disable!(@admin) |
| 899 | + change_setting('AllowDynamicMigrations', @original_allow_migrations) |
| 900 | + end |
| 901 | + |
| 902 | + it 'rebuilds the history trigger when fields are renamed' do |
| 903 | + master = Master.create! current_user: @user |
| 904 | + master.current_user = @user |
| 905 | + |
| 906 | + setup_access :dynamic_model__test_field_renames |
| 907 | + |
| 908 | + @dm.instance_variable_set(:@ran_migration, nil) |
| 909 | + @dm.update!( |
| 910 | + current_admin: @admin, |
| 911 | + field_list: 'still_interested continue_now notes', |
| 912 | + options: <<~YAML |
| 913 | + _db_columns: |
| 914 | + still_interested: |
| 915 | + type: string |
| 916 | + continue_now: |
| 917 | + type: string |
| 918 | + notes: |
| 919 | + type: string |
| 920 | + YAML |
| 921 | + ) |
| 922 | + |
| 923 | + @dm.instance_variable_set(:@migration_generator, nil) |
| 924 | + @dm.send(:generate_migration) |
| 925 | + @dm.send(:run_migration) if @dm.instance_variable_get(:@do_migration) |
| 926 | + @dm.update_tracker_events |
| 927 | + |
| 928 | + @conn.schema_cache.clear! |
| 929 | + DynamicModel.reset_active_model_configurations! |
| 930 | + |
| 931 | + history_columns = @conn.columns("#{@schema_name}.#{@history_table_name}").map(&:name) |
| 932 | + expect(history_columns).to include('still_interested', 'continue_now') |
| 933 | + expect(history_columns).not_to include('select_still_interested', 'select_continue_now') |
| 934 | + |
| 935 | + model_class = @dm.implementation_class |
| 936 | + model_class.reset_column_information |
| 937 | + |
| 938 | + record = master.dynamic_model__test_field_renames.create!( |
| 939 | + current_user: @user, |
| 940 | + still_interested: 'yes', |
| 941 | + continue_now: 'no', |
| 942 | + notes: 'renamed fields' |
| 943 | + ) |
| 944 | + |
| 945 | + history_record = @conn.exec_query( |
| 946 | + "SELECT still_interested, continue_now FROM #{@schema_name}.#{@history_table_name} " \ |
| 947 | + "WHERE #{@table_name.singularize}_id = #{record.id} ORDER BY id DESC LIMIT 1" |
| 948 | + ).first |
| 949 | + |
| 950 | + expect(history_record['still_interested']).to eq('yes') |
| 951 | + expect(history_record['continue_now']).to eq('no') |
| 952 | + end |
| 953 | + end |
852 | 954 | end |
0 commit comments