{"version":3,"file":"useMigrateDatabaseFields.mjs","sources":["../../../../src/components/configuration/useMigrateDatabaseFields.ts"],"sourcesContent":["import { useEffect } from 'react';\n\nimport { type DataSourcePluginOptionsEditorProps } from '@grafana/data';\nimport { config } from '@grafana/runtime';\n\nimport { type SQLOptions } from '../../types';\nimport { sqlPluginLogger } from '../../utils/logging';\n\n/**\n * 1. Moves the database field from the options object to jsonData.database and empties the database field.\n * 2. If max open connections, max idle connections, and auto idle are all undefined set these to default values.\n */\nexport function useMigrateDatabaseFields<T extends SQLOptions, S = {}>({\n  onOptionsChange,\n  options,\n}: DataSourcePluginOptionsEditorProps<T, S>) {\n  useEffect(() => {\n    const jsonData = options.jsonData;\n    let newOptions = { ...options };\n    let optionsUpdated = false;\n\n    // Migrate the database field from the column into the jsonData object\n    if (options.database) {\n      sqlPluginLogger.logDebug(`Migrating from options.database with value ${options.database} for ${options.name}`);\n      newOptions.database = '';\n      newOptions.jsonData = { ...jsonData, database: options.database };\n      optionsUpdated = true;\n    }\n\n    // Set default values for max open connections, max idle connection,\n    // and auto idle if they're all undefined\n    if (\n      jsonData.maxOpenConns === undefined &&\n      jsonData.maxIdleConns === undefined &&\n      jsonData.maxIdleConnsAuto === undefined\n    ) {\n      const { maxOpenConns, maxIdleConns } = config.sqlConnectionLimits;\n\n      sqlPluginLogger.logDebug(\n        `Setting default max open connections to ${maxOpenConns} and setting max idle connection to ${maxIdleConns}`\n      );\n\n      // Spread from the jsonData in new options in case\n      // the database field was migrated as well\n      newOptions.jsonData = {\n        ...newOptions.jsonData,\n        maxOpenConns: maxOpenConns,\n        maxIdleConns: maxIdleConns,\n        maxIdleConnsAuto: true,\n      };\n\n      // Make sure we issue an update if options changed\n      optionsUpdated = true;\n    }\n\n    // If the maximum connection lifetime hasn't been\n    // otherwise set fill in with the default from configuration\n    if (jsonData.connMaxLifetime === undefined) {\n      const { connMaxLifetime } = config.sqlConnectionLimits;\n\n      // Spread new options and add our value\n      newOptions.jsonData = {\n        ...newOptions.jsonData,\n        connMaxLifetime: connMaxLifetime,\n      };\n\n      // Note that we've updated the options\n      optionsUpdated = true;\n    }\n\n    // Only issue an update if we changed options\n    if (optionsUpdated) {\n      onOptionsChange(newOptions);\n    }\n  }, [onOptionsChange, options]);\n}\n"],"names":[],"mappings":";;;;;AAYO,SAAS,wBAAA,CAAuD;AAAA,EACrE,eAAA;AAAA,EACA;AACF,CAAA,EAA6C;AAC3C,EAAA,SAAA,CAAU,MAAM;AACd,IAAA,MAAM,WAAW,OAAA,CAAQ,QAAA;AACzB,IAAA,IAAI,UAAA,GAAa,EAAE,GAAG,OAAA,EAAQ;AAC9B,IAAA,IAAI,cAAA,GAAiB,KAAA;AAGrB,IAAA,IAAI,QAAQ,QAAA,EAAU;AACpB,MAAA,eAAA,CAAgB,SAAS,CAAA,2CAAA,EAA8C,OAAA,CAAQ,QAAQ,CAAA,KAAA,EAAQ,OAAA,CAAQ,IAAI,CAAA,CAAE,CAAA;AAC7G,MAAA,UAAA,CAAW,QAAA,GAAW,EAAA;AACtB,MAAA,UAAA,CAAW,WAAW,EAAE,GAAG,QAAA,EAAU,QAAA,EAAU,QAAQ,QAAA,EAAS;AAChE,MAAA,cAAA,GAAiB,IAAA;AAAA,IACnB;AAIA,IAAA,IACE,QAAA,CAAS,iBAAiB,KAAA,CAAA,IAC1B,QAAA,CAAS,iBAAiB,KAAA,CAAA,IAC1B,QAAA,CAAS,qBAAqB,KAAA,CAAA,EAC9B;AACA,MAAA,MAAM,EAAE,YAAA,EAAc,YAAA,EAAa,GAAI,MAAA,CAAO,mBAAA;AAE9C,MAAA,eAAA,CAAgB,QAAA;AAAA,QACd,CAAA,wCAAA,EAA2C,YAAY,CAAA,oCAAA,EAAuC,YAAY,CAAA;AAAA,OAC5G;AAIA,MAAA,UAAA,CAAW,QAAA,GAAW;AAAA,QACpB,GAAG,UAAA,CAAW,QAAA;AAAA,QACd,YAAA;AAAA,QACA,YAAA;AAAA,QACA,gBAAA,EAAkB;AAAA,OACpB;AA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