-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmssql.ts
297 lines (290 loc) · 7.48 KB
/
mssql.ts
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import * as Joi from "joi";
// defintions based on sql-server 1 datatypes
// https://docs.microsoft.com/en-us/sql/t-sql/data-types/data-types-transact-sql?view=sql-server-ver15
export const joiBase = (type: string) => {
switch (type) {
// Exact numerics
case "bigint":
case "numeric":
return Joi.string();
case "bit":
case "smallint":
return Joi.number().integer();
case "decimal":
case "smallmoney":
return Joi.string();
case "int":
case "tinyint":
return Joi.number().integer();
case "money":
return Joi.string();
// Approximate numerics
case "float":
return Joi.string();
case "real":
return Joi.number();
// Date and time
case "date":
case "datetimeoffset":
case "datetime2":
case "smalldatetime":
case "datetime":
case "time":
return Joi.string();
// Character strings
case "char":
case "varchar":
case "text":
return Joi.string();
// Unicode character strings
case "nchar":
case "nvarchar":
case "ntext":
case "Binary strings":
case "binary":
case "varbinary":
case "image":
return Joi.string();
// Other data types
case "cursor":
case "rowversion":
case "hierarchyid":
case "uniqueidentifier":
case "sql_variant":
case "xml":
case "table":
return Joi.string();
default:
return Joi.string();
}
};
export const toSchemaScalar = (type: string) => {
switch (type) {
// Exact numerics
case "bigint":
case "numeric":
return "String";
case "bit":
case "smallint":
return "Float";
case "decimal":
case "smallmoney":
return "String";
case "int":
case "tinyint":
return "Float";
case "money":
return "String";
// Approximate numerics
case "float":
return "String";
case "real":
return "Float";
// Date and time
case "date":
case "datetimeoffset":
case "datetime2":
case "smalldatetime":
case "datetime":
case "time":
return "String";
// Character strings
case "char":
case "varchar":
case "text":
return "String";
// Unicode character strings
case "nchar":
case "nvarchar":
case "ntext":
case "Binary strings":
case "binary":
case "varbinary":
case "image":
return "String";
// Other data types
case "cursor":
case "rowversion":
case "hierarchyid":
case "uniqueidentifier":
case "sql_variant":
case "xml":
case "table":
return "String";
default:
return "String";
}
};
export const toProtoScalar = (type: string) => {
switch (type) {
// Exact numerics
case "bigint":
case "numeric":
return "string";
case "bit":
case "smallint":
return "uint32";
case "decimal":
case "smallmoney":
return "string";
case "int":
case "tinyint":
return "uint32";
case "money":
return "string";
// Approximate numerics
case "float":
return "string";
case "real":
return "string";
// Date and time
case "date":
case "datetimeoffset":
case "datetime2":
case "smalldatetime":
case "datetime":
case "time":
return "string";
// Character strings
case "char":
case "varchar":
case "text":
return "string";
// Unicode character strings
case "nchar":
case "nvarchar":
case "ntext":
case "Binary strings":
case "binary":
case "varbinary":
case "image":
return "string";
// Other data types
case "cursor":
case "rowversion":
case "hierarchyid":
case "uniqueidentifier":
case "sql_variant":
case "xml":
case "table":
return "string";
default:
return "string";
}
};
export const dialect = ({ migrationTable }) => {
const dbSurveyQuery = `
with keys as (
SELECT
main.TABLE_CATALOG
,main.TABLE_SCHEMA
,main.TABLE_NAME
,main.COLUMN_NAME
,detail.CONSTRAINT_TYPE
FROM
INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE main
JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS detail
on
main.TABLE_CATALOG = detail.TABLE_CATALOG
and
main.TABLE_SCHEMA = detail.TABLE_SCHEMA
and
main.TABLE_NAME = detail.TABLE_NAME
and
main.CONSTRAINT_NAME = detail.CONSTRAINT_NAME
where
detail.CONSTRAINT_TYPE in ('PRIMARY KEY', 'UNIQUE')
),
survey as (
SELECT
detail.TABLE_CATALOG,
detail.TABLE_SCHEMA resource_schema,
case
when main.TABLE_TYPE = 'VIEW' then 'view'
else 'table'
end resource_type,
detail.TABLE_NAME resource_name,
detail.ordinal_position resource_column_id,
detail.column_name resource_column_name,
case
when detail.is_nullable = 'NO' then 1
else 0
end notnull,
detail.data_type "type"
FROM
information_schema.tables main
JOIN (
select
col.TABLE_CATALOG,
col.TABLE_SCHEMA,
col.TABLE_NAME,
col.ordinal_position,
col.column_name,
col.data_type,
case
when col.character_maximum_length is not null then col.character_maximum_length
else col.numeric_precision
end as max_length,
col.is_nullable
from
information_schema.columns col
where
col.table_schema not in (
'sys',
'information_schema'
)
) detail
on
main.TABLE_SCHEMA = detail.TABLE_SCHEMA
and
main.TABLE_NAME = detail.TABLE_NAME
WHERE
main.table_schema not in (
'sys',
'information_schema'
)
)
select
survey.resource_schema
,survey.resource_type
,survey.resource_name
,survey.resource_column_id
,survey.resource_column_name
,survey.notnull
,survey.type
,case
when keys.CONSTRAINT_TYPE = 'PRIMARY KEY' then 1
else 0
end primarykey
,case
when keys.CONSTRAINT_TYPE = 'UNIQUE' then 1
else 0
end uniquekey
from
survey
left join keys
on
keys.TABLE_CATALOG = survey.TABLE_CATALOG
and
keys.TABLE_SCHEMA = survey.resource_schema
and
keys.TABLE_NAME = survey.resource_name
and
keys.COLUMN_NAME = survey.resource_column_name
order by
survey.resource_schema
,survey.resource_type
,survey.resource_name
,survey.resource_column_id
;
`;
const versionQuery = `select @@version as db_version;`;
return {
dbSurveyQuery,
versionQuery,
joiBase,
toSchemaScalar,
toProtoScalar,
};
};