app/models/table.rb
# adds the column if not exists or cast it to timestamp field
adds the column if not exists or cast it to timestamp field
adds the column if not exists or cast it to timestamp field
def normalize_timestamp(database, column)
    schema = self.schema(reload: true)
    if schema.nil? || !schema.flatten.include?(column)
      database.run(%Q{
        ALTER TABLE "#{name}"
        ADD COLUMN #{column} timestamptz
        DEFAULT NOW()
      })
    end
    if schema.present?
      column_type = Hash[schema][column]
      if column_type == 'string' && schema.flatten.include?(column)
        success = ms_to_timestamp(database, name, column)
        success = string_to_timestamp(database, name, column) unless success
        database.run(%Q{
          ALTER TABLE "#{name}"
          ALTER COLUMN #{column}
          SET DEFAULT now()
        })
      elsif column_type == 'date' || column_type == 'timestamptz'
        database.run(%Q{
          ALTER TABLE "#{name}"
          ALTER COLUMN #{column}
          SET DEFAULT now()
        })
      end
    end
  end
def normalize_timestamp ( database , column ) schema = self . schema ( reload : true ) if schema . nil? || ! schema . flatten . include? ( column ) database . run ( %Q{
        ALTER TABLE "#{name}"
        ADD COLUMN #{column} timestamptz
        DEFAULT NOW()
      } ) end if schema . present? column_type = Hash [ schema ] [ column ] if column_type == 'string' && schema . flatten . include? ( column ) success = ms_to_timestamp ( database , name , column ) success = string_to_timestamp ( database , name , column ) unless success database . run ( %Q{
          ALTER TABLE "#{name}"
          ALTER COLUMN #{column}
          SET DEFAULT now()
        } ) elsif column_type == 'date' || column_type == 'timestamptz' database . run ( %Q{
          ALTER TABLE "#{name}"
          ALTER COLUMN #{column}
          SET DEFAULT now()
        } ) end end end
adds the column if not exists or cast it to timestamp field
adds the column if not exists or cast it to timestamp field
# if column already exists, cast to timestamp value and set default
database
<not_specific>
column
<not_specific>