1
0
mirror of https://github.com/lensapp/lens.git synced 2025-05-20 05:10:56 +00:00

store migrations refactoring

Signed-off-by: Roman <ixrock@gmail.com>
This commit is contained in:
Roman 2020-07-03 14:13:08 +03:00
parent c15aa7972c
commit d7cffeb7a8
13 changed files with 164 additions and 145 deletions

View File

@ -1,32 +1,16 @@
import ElectronStore from "electron-store"
import migrations from "../migrations/cluster-store"
import { Cluster, ClusterBaseInfo } from "../main/cluster";
import * as version200Beta2 from "../migrations/cluster-store/2.0.0-beta.2"
import * as version241 from "../migrations/cluster-store/2.4.1"
import * as version260Beta2 from "../migrations/cluster-store/2.6.0-beta.2"
import * as version260Beta3 from "../migrations/cluster-store/2.6.0-beta.3"
import * as version270Beta0 from "../migrations/cluster-store/2.7.0-beta.0"
import * as version270Beta1 from "../migrations/cluster-store/2.7.0-beta.1"
import { getAppVersion } from "./utils/app-version";
export class ClusterStore {
private static instance: ClusterStore;
public store: ElectronStore;
private store: ElectronStore;
private constructor() {
this.store = new ElectronStore({
// @ts-ignore
// fixme: tests are failed without "projectVersion"
projectVersion: getAppVersion(),
name: "lens-cluster-store",
accessPropertiesByDotNotation: false, // To make dots safe in cluster context names
migrations: {
"2.0.0-beta.2": version200Beta2.migration,
"2.4.1": version241.migration,
"2.6.0-beta.2": version260Beta2.migration,
"2.6.0-beta.3": version260Beta3.migration,
"2.7.0-beta.0": version270Beta0.migration,
"2.7.0-beta.1": version270Beta1.migration
}
migrations: migrations,
})
}
@ -78,8 +62,7 @@ export class ClusterStore {
}
if (index === -1) {
clusters.push(storable)
}
else {
} else {
clusters[index] = storable
}
this.store.set("clusters", clusters)

View File

@ -1,10 +1,5 @@
import ElectronStore from "electron-store"
import * as version210Beta4 from "../migrations/user-store/2.1.0-beta.4"
import { getAppVersion } from "./utils/app-version";
export interface User {
id?: string;
}
import migrations from "../migrations/user-store"
export interface UserPreferences {
httpsProxy?: string;
@ -20,12 +15,7 @@ export class UserStore {
private constructor() {
this.store = new ElectronStore({
// @ts-ignore
// fixme: tests are failed without "projectVersion"
projectVersion: getAppVersion(),
migrations: {
"2.1.0-beta.4": version210Beta4.migration,
}
migrations: migrations,
});
}
@ -81,6 +71,4 @@ export class UserStore {
}
}
const userStore: UserStore = UserStore.getInstance();
export { userStore };
export const userStore: UserStore = UserStore.getInstance();

View File

@ -20,7 +20,7 @@ export class Workspace implements WorkspaceData {
export class WorkspaceStore {
public static defaultId = "default"
private static instance: WorkspaceStore;
public store: ElectronStore;
private store: ElectronStore;
private constructor() {
this.store = new ElectronStore({

View File

@ -1,17 +1,16 @@
/* Early store format had the kubeconfig directly under context name, this moves
it under the kubeConfig key */
import { isTestEnv } from "../../common/vars";
import { migration } from "../migration-wrapper";
export function migration(store: any) {
if(!isTestEnv) {
console.log("CLUSTER STORE, MIGRATION: 2.0.0-beta.2");
export default migration({
version: "2.0.0-beta.2",
run(store, log) {
for (const value of store) {
const contextName = value[0];
// Looping all the keys gives out the store internal stuff too...
if (contextName === "__internal__" || value[1].hasOwnProperty('kubeConfig')) continue;
store.set(contextName, { kubeConfig: value[1] });
}
}
for (const value of store) {
const contextName = value[0];
// Looping all the keys gives out the store internal stuff too...
if(contextName === "__internal__" || value[1].hasOwnProperty('kubeConfig')) continue;
store.set(contextName, { kubeConfig: value[1] });
}
}
})

View File

@ -1,15 +1,14 @@
// Cleans up a store that had the state related data stored
import { isTestEnv } from "../../common/vars";
import { migration } from "../migration-wrapper";
export function migration(store: any) {
if (!isTestEnv) {
console.log("CLUSTER STORE, MIGRATION: 2.4.1");
export default migration({
version: "2.4.1",
run(store, log) {
for (const value of store) {
const contextName = value[0];
if (contextName === "__internal__") continue;
const cluster = value[1];
store.set(contextName, { kubeConfig: cluster.kubeConfig, icon: cluster.icon || null, preferences: cluster.preferences || {} });
}
}
for (const value of store) {
const contextName = value[0];
if (contextName === "__internal__") continue;
const cluster = value[1];
store.set(contextName, { kubeConfig: cluster.kubeConfig, icon: cluster.icon || null, preferences: cluster.preferences || {} });
}
}
})

View File

@ -1,19 +1,19 @@
// Move cluster icon from root to preferences
import { isTestEnv } from "../../common/vars";
import { migration } from "../migration-wrapper";
export function migration(store: any) {
if(!isTestEnv) {
console.log("CLUSTER STORE, MIGRATION: 2.6.0-beta.2");
}
for (const value of store) {
const clusterKey = value[0];
if(clusterKey === "__internal__") continue
const cluster = value[1];
if(!cluster.preferences) cluster.preferences = {};
if(cluster.icon) {
cluster.preferences.icon = cluster.icon;
delete(cluster["icon"]);
export default migration({
version: "2.6.0-beta.2",
run(store, log) {
for (const value of store) {
const clusterKey = value[0];
if (clusterKey === "__internal__") continue
const cluster = value[1];
if (!cluster.preferences) cluster.preferences = {};
if (cluster.icon) {
cluster.preferences.icon = cluster.icon;
delete (cluster["icon"]);
}
store.set(clusterKey, { contextName: clusterKey, kubeConfig: value[1].kubeConfig, preferences: value[1].preferences });
}
store.set(clusterKey, { contextName: clusterKey, kubeConfig: value[1].kubeConfig, preferences: value[1].preferences });
}
}
})

View File

@ -1,38 +1,38 @@
import * as yaml from "js-yaml"
import { isTestEnv } from "../../common/vars";
import { migration } from "../migration-wrapper";
import yaml from "js-yaml"
// Convert access token and expiry from arrays into strings
export function migration(store: any) {
if(!isTestEnv) {
console.log("CLUSTER STORE, MIGRATION: 2.6.0-beta.3");
}
for (const value of store) {
const clusterKey = value[0];
if(clusterKey === "__internal__") continue
const cluster = value[1];
if(!cluster.kubeConfig) continue
const kubeConfig = yaml.safeLoad(cluster.kubeConfig)
if(!kubeConfig.hasOwnProperty('users')) continue
const userObj = kubeConfig.users[0]
if (userObj) {
const user = userObj.user
if (user["auth-provider"] && user["auth-provider"].config) {
const authConfig = user["auth-provider"].config
if (authConfig["access-token"]) {
authConfig["access-token"] = `${authConfig["access-token"]}`
export default migration({
version: "2.6.0-beta.3",
run(store, log) {
for (const value of store) {
const clusterKey = value[0];
if (clusterKey === "__internal__") continue
const cluster = value[1];
if (!cluster.kubeConfig) continue
const kubeConfig = yaml.safeLoad(cluster.kubeConfig)
if (!kubeConfig.hasOwnProperty('users')) continue
const userObj = kubeConfig.users[0]
if (userObj) {
const user = userObj.user
if (user["auth-provider"] && user["auth-provider"].config) {
const authConfig = user["auth-provider"].config
if (authConfig["access-token"]) {
authConfig["access-token"] = `${authConfig["access-token"]}`
}
if (authConfig.expiry) {
authConfig.expiry = `${authConfig.expiry}`
}
log(authConfig)
user["auth-provider"].config = authConfig
kubeConfig.users = [{
name: userObj.name,
user: user
}]
cluster.kubeConfig = yaml.safeDump(kubeConfig)
store.set(clusterKey, cluster)
}
if (authConfig.expiry) {
authConfig.expiry = `${authConfig.expiry}`
}
console.log(authConfig)
user["auth-provider"].config = authConfig
kubeConfig.users = [{
name: userObj.name,
user: user
}]
cluster.kubeConfig = yaml.safeDump(kubeConfig)
store.set(clusterKey, cluster)
}
}
}
}
})

View File

@ -1,15 +1,15 @@
// Add existing clusters to "default" workspace
import { isTestEnv } from "../../common/vars";
import { migration } from "../migration-wrapper";
export function migration(store: any) {
if(!isTestEnv) {
console.log("CLUSTER STORE, MIGRATION: 2.7.0-beta.0");
export default migration({
version: "2.7.0-beta.0",
run(store, log) {
for (const value of store) {
const clusterKey = value[0];
if(clusterKey === "__internal__") continue
const cluster = value[1];
cluster.workspace = "default"
store.set(clusterKey, cluster)
}
}
for (const value of store) {
const clusterKey = value[0];
if(clusterKey === "__internal__") continue
const cluster = value[1];
cluster.workspace = "default"
store.set(clusterKey, cluster)
}
}
})

View File

@ -1,25 +1,25 @@
// add id for clusters and store them to array
// Add id for clusters and store them to array
import { migration } from "../migration-wrapper";
import { v4 as uuid } from "uuid"
import { isTestEnv } from "../../common/vars";
export function migration(store: any) {
if(!isTestEnv) {
console.log("CLUSTER STORE, MIGRATION: 2.7.0-beta.1");
}
const clusters: any[] = []
for (const value of store) {
const clusterKey = value[0];
if(clusterKey === "__internal__") continue
if(clusterKey === "clusters") continue
const cluster = value[1];
cluster.id = uuid()
if (!cluster.preferences.clusterName) {
cluster.preferences.clusterName = clusterKey
export default migration({
version: "2.7.0-beta.1",
run(store, log) {
const clusters: any[] = []
for (const value of store) {
const clusterKey = value[0];
if (clusterKey === "__internal__") continue
if (clusterKey === "clusters") continue
const cluster = value[1];
cluster.id = uuid()
if (!cluster.preferences.clusterName) {
cluster.preferences.clusterName = clusterKey
}
clusters.push(cluster)
store.delete(clusterKey)
}
if (clusters.length > 0) {
store.set("clusters", clusters)
}
clusters.push(cluster)
store.delete(clusterKey)
}
if (clusters.length > 0) {
store.set("clusters", clusters)
}
}
})

View File

@ -0,0 +1,17 @@
// Cluster store migrations
import version200Beta2 from "./2.0.0-beta.2"
import version241 from "./2.4.1"
import version260Beta2 from "./2.6.0-beta.2"
import version260Beta3 from "./2.6.0-beta.3"
import version270Beta0 from "./2.7.0-beta.0"
import version270Beta1 from "./2.7.0-beta.1"
export default {
...version200Beta2,
...version241,
...version260Beta2,
...version260Beta3,
...version270Beta0,
...version270Beta1,
}

View File

@ -0,0 +1,21 @@
import ElectronStore from "electron-store";
import { isTestEnv } from "../common/vars";
export interface MigrationOpts {
version: string;
run(store: ElectronStore, log: (...args: any[]) => void): void;
}
function infoLog(...args: any[]) {
if (isTestEnv) return;
console.log(...args);
}
export function migration({ version, run }: MigrationOpts) {
return {
[version]: (store: ElectronStore) => {
infoLog(`CLUSTER STORE, MIGRATION: ${version}`);
run(store, infoLog);
}
};
}

View File

@ -1,4 +1,9 @@
// Add / reset "lastSeenAppVersion"
export function migration(store: any) {
store.set("lastSeenAppVersion", "0.0.0");
}
import { migration } from "../migration-wrapper";
export default migration({
version: "2.1.0-beta.4",
run(store) {
store.set("lastSeenAppVersion", "0.0.0");
}
})

View File

@ -0,0 +1,7 @@
// User store migrations
import version210Beta4 from "./2.1.0-beta.4"
export default {
...version210Beta4,
}