MCPcopy Create free account
hub / github.com/zenstackhq/zenstack / getDefaultValue

Function getDefaultValue

packages/cli/src/actions/pull/provider/mysql.ts:219–285  ·  view source on GitHub ↗
({ defaultValue, fieldType, datatype, datatype_name, services, enums })

Source from the content-addressed store, hash-verified

217 }
218 },
219 getDefaultValue({ defaultValue, fieldType, datatype, datatype_name, services, enums }) {
220 const val = defaultValue.trim();
221
222 // Handle NULL early
223 if (val.toUpperCase() === 'NULL') {
224 return null;
225 }
226
227 // Handle enum defaults
228 if (datatype === 'enum' && datatype_name) {
229 const enumDef = enums.find((e) => getDbName(e) === datatype_name);
230 if (enumDef) {
231 // Strip quotes from the value (MySQL returns 'value')
232 const enumValue = val.startsWith("'") && val.endsWith("'") ? val.slice(1, -1) : val;
233 const enumField = enumDef.fields.find((f) => getDbName(f) === enumValue);
234 if (enumField) {
235 return (ab) => ab.ReferenceExpr.setTarget(enumField);
236 }
237 }
238 }
239
240 switch (fieldType) {
241 case 'DateTime':
242 if (/^CURRENT_TIMESTAMP(\(\d*\))?$/i.test(val) || val.toLowerCase() === 'current_timestamp()' || val.toLowerCase() === 'now()') {
243 return (ab) => ab.InvocationExpr.setFunction(getFunctionRef('now', services));
244 }
245 // Fallback to string literal for other DateTime defaults
246 return (ab) => ab.StringLiteral.setValue(val);
247
248 case 'Int':
249 case 'BigInt':
250 if (val.toLowerCase() === 'auto_increment') {
251 return (ab) => ab.InvocationExpr.setFunction(getFunctionRef('autoincrement', services));
252 }
253 return (ab) => ab.NumberLiteral.setValue(val);
254
255 case 'Float':
256 return normalizeFloatDefault(val);
257
258 case 'Decimal':
259 return normalizeDecimalDefault(val);
260
261 case 'Boolean':
262 return (ab) => ab.BooleanLiteral.setValue(val.toLowerCase() === 'true' || val === '1' || val === "b'1'");
263
264 case 'String':
265 if (val.toLowerCase() === 'uuid()') {
266 return (ab) => ab.InvocationExpr.setFunction(getFunctionRef('uuid', services));
267 }
268 return (ab) => ab.StringLiteral.setValue(val);
269 case 'Json':
270 return (ab) => ab.StringLiteral.setValue(val);
271 case 'Bytes':
272 return (ab) => ab.StringLiteral.setValue(val);
273 }
274
275 // Handle function calls (e.g., uuid(), now())
276 if (val.includes('(') && val.includes(')')) {

Callers

nothing calls this directly

Calls 9

getDbNameFunction · 0.90
getFunctionRefFunction · 0.90
normalizeFloatDefaultFunction · 0.90
normalizeDecimalDefaultFunction · 0.90
findMethod · 0.80
setTargetMethod · 0.80
setFunctionMethod · 0.80
setValueMethod · 0.45
addArgMethod · 0.45

Tested by

no test coverage detected