forked from GoogleCloudPlatform/spanner-migration-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdirect-connection.component.ts
More file actions
176 lines (162 loc) · 6.71 KB
/
direct-connection.component.ts
File metadata and controls
176 lines (162 loc) · 6.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import { Component, OnInit } from '@angular/core'
import { FormControl, FormGroup, Validators } from '@angular/forms'
import { Router } from '@angular/router'
import IDbConfig from 'src/app/model/db-config'
import { FetchService } from 'src/app/services/fetch/fetch.service'
import { DataService } from 'src/app/services/data/data.service'
import { LoaderService } from '../../services/loader/loader.service'
import { DialectList, InputType, PersistedFormValues, StorageKeys } from 'src/app/app.constants'
import { SnackbarService } from 'src/app/services/snackbar/snackbar.service'
import { extractSourceDbName } from 'src/app/utils/utils'
import { ClickEventService } from 'src/app/services/click-event/click-event.service'
import { MatDialog } from '@angular/material/dialog'
import { InfodialogComponent } from '../infodialog/infodialog.component'
@Component({
selector: 'app-direct-connection',
templateUrl: './direct-connection.component.html',
styleUrls: ['./direct-connection.component.scss'],
})
export class DirectConnectionComponent implements OnInit {
connectForm = new FormGroup({
dbEngine: new FormControl('', [Validators.required]),
isSharded: new FormControl(false),
hostName: new FormControl('', [Validators.required]),
port: new FormControl('', [Validators.required, Validators.pattern('^[0-9]+$')]),
userName: new FormControl('', [Validators.required]),
password: new FormControl(''),
dbName: new FormControl('', [Validators.required]),
dialect: new FormControl('', [Validators.required]),
})
dbEngineList = [
{ value: 'mysql', displayName: 'MySQL' },
{ value: 'sqlserver', displayName: 'SQL Server' },
{ value: 'oracle', displayName: 'Oracle' },
{ value: 'postgres', displayName: 'PostgreSQL' },
]
isTestConnectionSuccessful = false
connectRequest: any = null
getSchemaRequest: any = null
isConfigSet: boolean = false
shardedResponseList = [
{ value: false, displayName: 'No'},
{ value: true, displayName: 'Yes'},
]
dialect = DialectList
constructor(
private router: Router,
private fetch: FetchService,
private data: DataService,
private loader: LoaderService,
private snackbarService: SnackbarService,
private clickEvent: ClickEventService,
private dialog: MatDialog,
) {}
ngOnInit(): void {
//initialise component with the previously persisted values if present.
if (localStorage.getItem(PersistedFormValues.DirectConnectForm) != null) {
this.connectForm.setValue(JSON.parse(localStorage.getItem(PersistedFormValues.DirectConnectForm) as string))
}
if (localStorage.getItem(PersistedFormValues.IsConnectionSuccessful) != null) {
this.isTestConnectionSuccessful = localStorage.getItem(PersistedFormValues.IsConnectionSuccessful) === 'true'
}
this.clickEvent.cancelDbLoad.subscribe({
next: (res: boolean) => {
if (res && this.connectRequest) {
this.connectRequest.unsubscribe()
if (this.getSchemaRequest) {
this.getSchemaRequest.unsubscribe()
}
}
},
})
}
testConn() {
this.clickEvent.openDatabaseLoader('test-connection', this.connectForm.value.dbName!)
const { dbEngine, isSharded, hostName, port, userName, password, dbName, dialect } = this.connectForm.value
localStorage.setItem(PersistedFormValues.DirectConnectForm, JSON.stringify(this.connectForm.value))
let config: IDbConfig = {
dbEngine: dbEngine!,
isSharded: isSharded!,
hostName: hostName!,
port: port!,
userName: userName!,
password: password!,
dbName: dbName!,
}
this.connectRequest =this.fetch.connectTodb(config, dialect!).subscribe({
next: () => {
this.snackbarService.openSnackBar('SUCCESS! Spanner migration tool was able to successfully ping source database', 'Close', 3)
//Datbase loader causes the direct connection form to get refreshed hence this value needs to be persisted to local storage.
localStorage.setItem(PersistedFormValues.IsConnectionSuccessful, "true")
this.clickEvent.closeDatabaseLoader()
},
error: (e) => {
this.isTestConnectionSuccessful = false
this.snackbarService.openSnackBar(e.error, 'Close')
localStorage.setItem(PersistedFormValues.IsConnectionSuccessful, "false")
this.clickEvent.closeDatabaseLoader()
}
})
}
async connectToDb() {
this.data.updateIsConfigSet();
this.fetch.getIsConfigSet().subscribe({
next: (res: boolean) => {
this.isConfigSet = res;
if (!this.isConfigSet) {
this.dialog.open(InfodialogComponent, {
data: {
message: "Please configure spanner project id and instance id to proceed",
type: 'error',
title: 'Configure Spanner',
},
maxWidth: '500px',
});
return;
}
this.clickEvent.openDatabaseLoader('direct', this.connectForm.value.dbName!);
window.scroll(0, 0);
this.data.resetStore();
localStorage.clear();
const { dbEngine, isSharded, hostName, port, userName, password, dbName, dialect } = this.connectForm.value;
localStorage.setItem(PersistedFormValues.DirectConnectForm, JSON.stringify(this.connectForm.value));
let config: IDbConfig = {
dbEngine: dbEngine!,
isSharded: isSharded!,
hostName: hostName!,
port: port!,
userName: userName!,
password: password!,
dbName: dbName!,
};
this.connectRequest = this.fetch.connectTodb(config, dialect!).subscribe({
next: () => {
this.getSchemaRequest = this.data.getSchemaConversionFromDb();
this.data.conv.subscribe((res) => {
localStorage.setItem(
StorageKeys.Config,
JSON.stringify({ dbEngine, hostName, port, userName, password, dbName })
);
localStorage.setItem(StorageKeys.Type, InputType.DirectConnect);
localStorage.setItem(StorageKeys.SourceDbName, extractSourceDbName(dbEngine!));
this.clickEvent.closeDatabaseLoader();
// After a successful load, remove the persisted values.
localStorage.removeItem(PersistedFormValues.DirectConnectForm);
this.router.navigate(['/workspace']);
});
},
error: (e) => {
this.snackbarService.openSnackBar(e.error, 'Close');
this.clickEvent.closeDatabaseLoader();
},
});
},
error: (err) => {
console.error('Error fetching configuration:', err);
},
});
}
refreshDbSpecifcConnectionOptions() {
this.connectForm.value.isSharded = false
}
}