Skip to content

Commit 6cfed75

Browse files
author
Giovanni Garufi
committed
Updated according to review
1 parent 017eab2 commit 6cfed75

File tree

2 files changed

+41
-52
lines changed

2 files changed

+41
-52
lines changed

AbstractSQLCompiler.ts

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ export interface SqlModel {
5959
dropSchema: string[]
6060
}
6161

62-
export interface hasDependants {
62+
export interface HasDependants {
6363
[dependant: string]: true
6464
}
6565

66-
export interface schemaDependencyMap {
66+
export interface SchemaDependencyMap {
6767
[tableName: string]: {
6868
resourceName: string
6969
primitive: AbstractSqlTable['primitive']
@@ -82,7 +82,7 @@ export interface ModifiedFields {
8282
fields?: {}[]
8383
}
8484

85-
type ReduceFn<T> = (accumulator: any, element: T) => any
85+
type ReduceFn<T> = (element: T) => string | undefined
8686
type MatchFn<T> = (haystack:Array<T>, needle:T) => (T | undefined)
8787
interface Pair<T> { src: T, dst: T }
8888
interface Split<T> { ins: Array<T>, del: Array<T>, mod: Array<Pair<T>> }
@@ -247,8 +247,8 @@ const mkSchemaDependencyMap = (tables: ResourceMap<AbstractSqlTable>, engine: En
247247
} else {
248248
ifNotExistsStr = ''
249249
}
250-
const hasDependants: hasDependants = {}
251-
const schemaDependencyMap: schemaDependencyMap = {}
250+
const hasDependants: HasDependants = {}
251+
const schemaDependencyMap: SchemaDependencyMap = {}
252252

253253
_.forOwn(tables, (table, resourceName) => {
254254
if (_.isString(table)) {
@@ -289,7 +289,7 @@ const mkSchemaDependencyMap = (tables: ResourceMap<AbstractSqlTable>, engine: En
289289
}
290290

291291
const compileSchema = (abstractSqlModel: AbstractSqlModel, engine: Engines, ifNotExists: boolean): SqlModel => {
292-
let { hasDependants, schemaDependencyMap } = mkSchemaDependencyMap(abstractSqlModel.tables, engine, ifNotExists)
292+
const { hasDependants, schemaDependencyMap } = mkSchemaDependencyMap(abstractSqlModel.tables, engine, ifNotExists)
293293

294294
const createSchemaStatements = []
295295
let dropSchemaStatements = []
@@ -394,10 +394,13 @@ const genSplit = <T>(src:Array<T>, dst: Array<T>, matchFn: MatchFn<T>): Split<T>
394394
// The result of the match function will be either the element that should be matched to
395395
// the argument or undefined if no such element is found.
396396
const genDiff = <T>(insFn: ReduceFn<T>, delFn: ReduceFn<T>, modFn: ReduceFn<Pair<T>>, matchFn: MatchFn<T>, src: Array<T>, dst: Array<T>) => {
397-
let split = genSplit(src, dst, matchFn)
398-
return _.flatten(_.reduce(split.ins, insFn,
399-
_.reduce(split.del, delFn,
400-
_.reduce(split.mod, modFn, []))))
397+
const split = genSplit(src, dst, matchFn)
398+
399+
const diff = _.map(split.mod, modFn)
400+
.concat(_.map(split.del, delFn))
401+
.concat(_.map(split.ins, insFn))
402+
403+
return _.reject(diff, _.isUndefined)
401404
}
402405

403406
const diffFields = (src: AbstractSqlField[], dst: AbstractSqlField[], mappings: ResourceMap<string>, engine: Engines, ifNotExists: boolean) => {
@@ -411,12 +414,8 @@ const diffFields = (src: AbstractSqlField[], dst: AbstractSqlField[], mappings:
411414
ifNotExistsStr = ''
412415
ifExistsStr = ''
413416
}
414-
// defaultTypes =
415-
// 'Short Text': '\'\''
416-
// 'Text': '\'\''
417-
// 'Integer': 0
418417

419-
let matchFn: MatchFn<AbstractSqlField> = (fieldArray, field) => {
418+
const matchFn: MatchFn<AbstractSqlField> = (fieldArray, field) => {
420419
let match = _.find(fieldArray, { fieldName: field.fieldName })
421420
if (_.isUndefined(match)) {
422421
if (_.isString(mappings[field.fieldName])) {
@@ -427,26 +426,24 @@ const diffFields = (src: AbstractSqlField[], dst: AbstractSqlField[], mappings:
427426
}
428427
}
429428

430-
const insFn: ReduceFn<AbstractSqlField> = (acc, field) => {
429+
const insFn: ReduceFn<AbstractSqlField> = (field) => {
431430
// Columns are inserted as NULL for the time being, this is because we have no sensible way to automatically assign a default value
432-
acc.push('ADD COLUMN ' + ifNotExistsStr + '"' + field.fieldName + '" ' + dataTypeGen(engine, field) + ';\n\t')
433-
return acc
431+
return 'ADD COLUMN ' + ifNotExistsStr + '"' + field.fieldName + '" ' + dataTypeGen(engine, field) + ';\n\t'
432+
434433
}
435434

436-
const delFn: ReduceFn<AbstractSqlField> = (acc, field) => {
437-
acc.push('DROP COLUMN ' + ifExistsStr + '"' + field.fieldName + '";\n\t')
438-
return acc
435+
const delFn: ReduceFn<AbstractSqlField> = (field) => {
436+
return 'DROP COLUMN ' + ifExistsStr + '"' + field.fieldName + '";\n\t'
439437
}
440438

441-
const modFn: ReduceFn<Pair<AbstractSqlField>> = (acc, {src, dst}) => {
439+
const modFn: ReduceFn<Pair<AbstractSqlField>> = ({src, dst}) => {
442440
if (_.isEqual(src, dst)) {
443-
return acc
441+
return
444442
}
445443
if (_.isEqual(_.omit(src, ['fieldName', 'references']), _.omit(dst, ['fieldName', 'references']))) {
446-
acc.push('RENAME COLUMN "' + src.fieldName + '" TO "' + dst.fieldName + '";\n\t')
447-
return acc
444+
return 'RENAME COLUMN "' + src.fieldName + '" TO "' + dst.fieldName + '";\n\t'
448445
}
449-
throw Error('Can not migrate pre-existing field ' + src.fieldName + ' to ' + dst.fieldName)
446+
throw Error(`Can not migrate pre-existing field ${src.fieldName} to ${dst.fieldName}`)
450447
}
451448

452449
return genDiff(insFn, delFn, modFn, matchFn, src, dst)
@@ -471,27 +468,24 @@ const diffSchemas = (src: AbstractSqlModel, dst: AbstractSqlModel, engine: Engin
471468
}
472469
}
473470

474-
const insFn: ReduceFn<AbstractSqlTable> = (acc, table ) => {
471+
const insFn: ReduceFn<AbstractSqlTable> = (table) => {
475472
if (!_.isString(table) && !table.primitive) {
476-
acc.push(dstSDM[table.name].createSQL)
473+
return dstSDM[table.name].createSQL
477474
}
478-
return acc
479475
}
480476

481-
const delFn: ReduceFn<AbstractSqlTable> = (acc, table) => {
477+
const delFn: ReduceFn<AbstractSqlTable> = (table) => {
482478
if (!_.isString(table) && !table.primitive) {
483-
acc.push(srcSDM[table.name].dropSQL)
479+
return srcSDM[table.name].dropSQL
484480
}
485-
return acc
486481
}
487482

488-
const modFn: ReduceFn<Pair<AbstractSqlTable>> = (acc, { src: srcTbl, dst: dstTbl }) => {
483+
const modFn: ReduceFn<Pair<AbstractSqlTable>> = ({ src: srcTbl, dst: dstTbl }) => {
489484
if (_.isEqual(srcTbl, dstTbl)) {
490-
return acc
485+
return
491486
} else if (_.isEqual(_.omit(srcTbl, 'fields'), _.omit(dstTbl, 'fields'))) {
492487
const fields = diffFields(srcTbl.fields, dstTbl.fields, _.invert(src.synonyms), engine, ifNotExists)
493-
acc.push('ALTER TABLE "' + srcTbl.name + '"\n\t' + fields.join(''))
494-
return acc
488+
return 'ALTER TABLE "' + srcTbl.name + '"\n\t' + fields.join('')
495489
} else {
496490
const [srcResource, srcRest] = extractMappings(srcTbl.name)
497491
const [dstResource, dstRest] = extractMappings(dstTbl.name)
@@ -501,8 +495,7 @@ const diffSchemas = (src: AbstractSqlModel, dst: AbstractSqlModel, engine: Engin
501495
[srcRest]: dstResource
502496
}
503497
const fields = diffFields(srcTbl.fields, dstTbl.fields, mappings, engine, ifNotExists)
504-
acc.push('ALTER TABLE "' + srcTbl.name + '"\n\t' + 'RENAME TO "' + dstTbl.name + '";\n\t' + fields.join(''))
505-
return acc
498+
return 'ALTER TABLE "' + srcTbl.name + '"\n\t' + 'RENAME TO "' + dstTbl.name + '";\n\t' + fields.join('')
506499
}
507500
}
508501

@@ -527,4 +520,4 @@ const generateExport = (engine: Engines, ifNotExists: boolean) => {
527520
}
528521
export const postgres = generateExport(Engines.postgres, true)
529522
export const mysql = generateExport(Engines.mysql, true)
530-
export const websql = generateExport(Engines.websql, false)
523+
export const websql = generateExport(Engines.websql, false)

test/sbvr/test.coffee

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,21 +30,17 @@ module.exports = exports = (builtInVocab = false) ->
3030

3131
runMigration = (describe, src, dst, expectation) ->
3232
it 'Migration: \n\nVocabulary: src\n' + src + '\n\nVocabulary: dst\n' + dst, ->
33-
try
34-
SBVRParser.reset()
35-
srcLf = SBVRParser.matchAll(src, 'Process')
36-
srcSchema = LF2AbstractSQLTranslator(srcLf, 'Process')
33+
SBVRParser.reset()
34+
srcLf = SBVRParser.matchAll(src, 'Process')
35+
srcSchema = LF2AbstractSQLTranslator(srcLf, 'Process')
3736

38-
SBVRParser.reset()
39-
dstLf = SBVRParser.matchAll(dst, 'Process')
40-
dstSchema = LF2AbstractSQLTranslator(dstLf, 'Process')
37+
SBVRParser.reset()
38+
dstLf = SBVRParser.matchAll(dst, 'Process')
39+
dstSchema = LF2AbstractSQLTranslator(dstLf, 'Process')
4140

42-
migration = AbstractSQLCompiler.postgres.diffSchemas(srcSchema, dstSchema)
43-
console.log('GOT MIGRATION', migration)
44-
catch e
45-
throw e
46-
for i in [0...Math.max(migration.length, expectation.length)]
47-
expect(migration[i]).to.equal(expectation[i])
41+
migration = AbstractSQLCompiler.postgres.diffSchemas(srcSchema, dstSchema)
42+
43+
expect(migration).to.deep.equal(expectation)
4844

4945
runSchema = (describe, input, expectation) ->
5046
runExpectation describe, input, (result) ->

0 commit comments

Comments
 (0)