@@ -10,15 +10,64 @@ import {
1010import { Configuration , ConfigurationCreateAnswers } from '../types'
1111import { appendOrReplaceByKey , deleteByKey , findByKey } from '../util/array'
1212import { randomString } from '../util/string'
13- import { store } from './store'
13+ import { store , CURRENT_VERSION } from './store'
1414
1515const storeKey = 'configurations' as const
1616const searchKey = 'configurationName' as const
1717const excludeProperties = [ 'googleCloudProject' , 'confirmation' ] as const
1818
1919export const configurationPath = store . path
2020
21- export const getConfigurations = ( ) : Configuration [ ] => store . get ( storeKey )
21+ type LegacyConfiguration = Omit < Configuration , 'databaseType' | 'databaseInstance' > & {
22+ googleCloudSqlInstance : {
23+ connectionName : string
24+ port : number
25+ }
26+ }
27+
28+ const isLegacyConfiguration = ( config : Configuration | LegacyConfiguration ) : config is LegacyConfiguration => {
29+ return 'googleCloudSqlInstance' in config && ! ( 'databaseType' in config )
30+ }
31+
32+ const migrateLegacyConfiguration = ( legacy : LegacyConfiguration ) : Configuration => {
33+ return {
34+ configurationName : legacy . configurationName ,
35+ databaseType : 'cloudsql' ,
36+ databaseInstance : {
37+ connectionName : legacy . googleCloudSqlInstance . connectionName ,
38+ port : legacy . googleCloudSqlInstance . port ,
39+ } ,
40+ kubernetesContext : legacy . kubernetesContext ,
41+ kubernetesNamespace : legacy . kubernetesNamespace ,
42+ kubernetesServiceAccount : legacy . kubernetesServiceAccount ,
43+ localPort : legacy . localPort ,
44+ }
45+ }
46+
47+ const migrateConfigurationsIfNeeded = ( ) : void => {
48+ const currentVersion = store . get ( 'version' )
49+ const configurations = store . get ( storeKey ) as ( Configuration | LegacyConfiguration ) [ ]
50+
51+ // Check if migration is needed (no version or configurations in old format)
52+ const needsMigration = ! currentVersion || configurations . some ( isLegacyConfiguration )
53+
54+ if ( needsMigration ) {
55+ const migratedConfigurations = configurations . map ( ( config ) => {
56+ if ( isLegacyConfiguration ( config ) ) {
57+ return migrateLegacyConfiguration ( config )
58+ }
59+ return config
60+ } )
61+
62+ store . set ( storeKey , migratedConfigurations )
63+ store . set ( 'version' , CURRENT_VERSION )
64+ }
65+ }
66+
67+ export const getConfigurations = ( ) : Configuration [ ] => {
68+ migrateConfigurationsIfNeeded ( )
69+ return store . get ( storeKey )
70+ }
2271
2372export const getConfiguration = ( name : string ) : Configuration | undefined => {
2473 const configurations = getConfigurations ( )
@@ -31,6 +80,10 @@ export const saveConfiguration = (answers: ConfigurationCreateAnswers): void =>
3180 const configurations = store . get ( storeKey )
3281 appendOrReplaceByKey ( configurations , configuration , searchKey )
3382 store . set ( storeKey , configurations )
83+
84+ if ( ! store . get ( 'version' ) ) {
85+ store . set ( 'version' , CURRENT_VERSION )
86+ }
3487}
3588
3689export const deleteConfiguration = ( configuratioName : string ) : void => {
0 commit comments