You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: 20191011175857_add_year_to_existing_complementary_exam_settings.rb
    path of file: ./repos/i-diario/db/migrate
    
    the code of the file until where you have to start completion: class AddYearToExistingComplementaryExamSettings < ActiveRecord::Migration[4.2]
  def change
    execute <<-SQL
      DO $$
        DECLARE _complementary_exam_setting RECORD;
        DECLARE _year INTEGER;
      BEGIN
        FOR _complementary_exam_setting IN (
          SELECT id
  
    
  def change
    execute <<-SQL
      DO $$
        DECLARE _complementary_exam_setting RECORD;
        DECLARE _year INTEGER;
      BEGIN
        FOR _complementary_exam_setting IN (
          SELECT id
            FROM complementary_exam_settings
        ) LOOP
          IF EXISTS (
            SELECT 1
              FROM complementary_exams
            WHERE complementary_exam_setting_id = _complementary_exam_setting.id
          ) THEN
            SELECT CAST (
              EXTRACT (
                YEAR FROM (
                  SELECT recorded_at FROM complementary_exams
                   WHERE complementary_exam_setting_id = _complementary_exam_setting.id
                   LIMIT 1
                )
              )
              AS INTEGER
            ) INTO _year;
          ELSE
            SELECT CAST (EXTRACT (YEAR FROM NOW()) AS INTEGER) INTO _year;
          END IF;

          UPDATE complementary_exam_settings
             SET year = _year
           WHERE id = _complementary_exam_setting.id;
        END LOOP;
      END$$;
    SQL
  end