<commit_before>path = require 'path'
fs = require 'fs'
async = require 'async'
log = require('printit')
    prefix: 'init'
    date: true

Konnector = require '../models/konnector'
konnectorModules = require '../lib/konnector_hash'
Bill = require '../models/bill'

# List of konnectors for which the slug has changed
konnectorsToMigrate = [
    { oldSlug: 'virginmobile', newSlug: 'virgin_mobile' },
    { oldSlug: 'sncf', newSlug: 'voyages_sncf' }
]

# List of fields do update
# Format of element :
#     model: an instance of the model for which the field has to be changed
#     vendor: vendor which provided data (string)
#     field: field of model to be updated (string)
#     match: the element to be changed (string or regexp)
#     replace: replacement of match (string)
fieldsToMigrate = [
    { model: Bill, vendor: 'SNCF', field: 'vendor',
    match: /^SNCF$/, replace: 'VOYAGES SNCF' }
]

# Ensure that model fields are properly configured for 0.6.0 version.
# It means that account information are stored in the accounts field and
# that password is properly constructed to handle multiple accounts.
patch060 = (callback) ->
    Konnector.request 'all',  (err, konnectors) ->
        async.eachSeries konnectors, (konnector, done) ->
            if konnector.fieldValues?
                slug = konnector.slug
                log.info "Cleaning fields for konnector #{slug}..."
                konnector.cleanFieldValues()
                data =
                    fieldValues: konnector.fieldValues
                    accounts: konnector.accounts
                    password: <PASSWORD>

                konnector.updateAttributes data, (err) ->
                    if err
                        log.info "An error occured cleaning konnector #{slug}"
                        log.error err
                    else
                        log.info "Fields for konnector #{slug} are cleaned."
                    done()
            else
                done()
        , (err) ->
            callback()

# Delete error messages of already deleted configurations (i.e.
# no more credentials
patch381 = (callback) ->
    Konnector.request 'all', (err, konnectors) ->
        async.eachSeries konnectors, (konnector, done) ->
            if konnector.accounts.length is 0 and konnector.importErrorMessage
                data =
                    importErrorMessage : null

                slug = konnector.slug
                konnector.updateAttributes data, (err) ->
                    if err
                        log.info "An error occured cleaning konnector #{slug}"
                        log.error err
                    else
                        log.info "Fields for konnector #{slug} are cleaned."
                    done()
            else
                done()
        , (err) ->
            callback()

# This patch is to migrate user's account due to a change in the
# konnector interface. The field 'username' is replaced by the
# field 'login' to harmonize with other konnectors.

patchOnlineNet = (callback) ->
    Konnector.request 'bySlug', key: 'online_net', (err, konnectors) ->
        return callback err if err
        if not konnectors.length
            callback()
        konnector = konnectors[0]
        accounts = konnector.accounts
        if accounts[0]? && accounts[0].username
            log.info "Starting migration for online.net konnector"
            newAccounts = []
            for account in accounts
                account.login = account.username
                delete account.username
                newAccounts.push(account)
            
            konnector.updateAttributes accounts: newAccounts, (err) ->
                return callback err if err

                log.info "Successfully updated online.net konnector"
                callback()

        else
            callback()

# Applies all the migrations due to patches
patches = (callback) ->
    patch060 ->
        patch381 ->
            patchOnlineNet ->
                callback()

# Applies all the migrations due to evolution in the konnector name or vendor
migrations = (callback) ->
    migrateKonnectors ->
        migrateFields ->
            callback()


# Ensure that all konnector modules have a proper module created and that their
# isImporting flag is set to false.
module.exports = (callback) ->
<commit_msg>[fix] force callback in a patch when there's no  konnector<commit_after>path = require 'path'
fs = require 'fs'
async = require 'async'
log = require('printit')
    prefix: 'init'
    date: true

Konnector = require '../models/konnector'
konnectorModules = require '../lib/konnector_hash'
Bill = require '../models/bill'

# List of konnectors for which the slug has changed
konnectorsToMigrate = [
    { oldSlug: 'virginmobile', newSlug: 'virgin_mobile' },
    { oldSlug: 'sncf', newSlug: 'voyages_sncf' }
]

# List of fields do update
# Format of element :
#     model: an instance of the model for which the field has to be changed
#     vendor: vendor which provided data (string)
#     field: field of model to be updated (string)
#     match: the element to be changed (string or regexp)
#     replace: replacement of match (string)
fieldsToMigrate = [
    { model: Bill, vendor: 'SNCF', field: 'vendor',
    match: /^SNCF$/, replace: 'VOYAGES SNCF' }
]

# Ensure that model fields are properly configured for 0.6.0 version.
# It means that account information are stored in the accounts field and
# that password is properly constructed to handle multiple accounts.
patch060 = (callback) ->
    Konnector.request 'all',  (err, konnectors) ->
        async.eachSeries konnectors, (konnector, done) ->
            if konnector.fieldValues?
                slug = konnector.slug
                log.info "Cleaning fields for konnector #{slug}..."
                konnector.cleanFieldValues()
                data =
                    fieldValues: konnector.fieldValues
                    accounts: konnector.accounts
                    password: <PASSWORD>

                konnector.updateAttributes data, (err) ->
                    if err
                        log.info "An error occured cleaning konnector #{slug}"
                        log.error err
                    else
                        log.info "Fields for konnector #{slug} are cleaned."
                    done()
            else
                done()
        , (err) ->
            callback()

# Delete error messages of already deleted configurations (i.e.
# no more credentials
patch381 = (callback) ->
    Konnector.request 'all', (err, konnectors) ->
        async.eachSeries konnectors, (konnector, done) ->
            if konnector.accounts.length is 0 and konnector.importErrorMessage
                data =
                    importErrorMessage : null

                slug = konnector.slug
                konnector.updateAttributes data, (err) ->
                    if err
                        log.info "An error occured cleaning konnector #{slug}"
                        log.error err
                    else
                        log.info "Fields for konnector #{slug} are cleaned."
                    done()
            else
                done()
        , (err) ->
            callback()

# This patch is to migrate user's account due to a change in the
# konnector interface. The field 'username' is replaced by the
# field 'login' to harmonize with other konnectors.

patchOnlineNet = (callback) ->
    Konnector.request 'bySlug', key: 'online_net', (err, konnectors) ->
        return callback err if err
        return callback() unless konnectors.length

        konnector = konnectors[0]
        accounts = konnector.accounts
        if accounts[0]? && accounts[0].username
            log.info "Starting migration for online.net konnector"
            newAccounts = []
            for account in accounts
                account.login = account.username
                delete account.username
                newAccounts.push(account)

            konnector.updateAttributes accounts: newAccounts, (err) ->
                return callback err if err

                log.info "Successfully updated online.net konnector"
                callback()

        else
            callback()

# Applies all the migrations due to patches
patches = (callback) ->
    patch060 ->
        patch381 ->
            patchOnlineNet ->
                callback()

# Applies all the migrations due to evolution in the konnector name or vendor
migrations = (callback) ->
    migrateKonnectors ->
        migrateFields ->
            callback()


# Ensure that all konnector modules have a proper module created and that their
# isImporting flag is set to false.
module.exports = (callback) ->

{'fraction_non_alphanumeric': 0.08374271678281732, 'fraction_numerical': 0.004341368673597624, 'mean_word_length': 1.881500987491771, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 47, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5691593', 'n_tokens_mistral': 2339, 'n_tokens_neox': 2256, 'n_words': 783}