Skip to content

Commit e856a46

Browse files
committed
Partitions: fixed FE of partitions
1 parent a33f6e8 commit e856a46

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

forward_engineering/configs/templates.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ module.exports = {
1212

1313
createTablePartitionOf:
1414
'CREATE${temporary} TABLE${ifNotExist} ${name}\n' +
15-
'${partitionOf} (${keyConstraints}${checkConstraints}${foreignKeyConstraints}\n' +
16-
')${options};\n\n${comment}${columnDescriptions}',
15+
'${partitionOf} ${openParenthesis}${keyConstraints}${checkConstraints}${foreignKeyConstraints}\n' +
16+
'${closeParenthesis}${options};\n\n${comment}${columnDescriptions}',
1717

1818
columnDefinition: '${name} ${type}${collation}${primaryKey}${uniqueKey}${defaultValue}${notNull}',
1919

forward_engineering/ddlProvider.js

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ module.exports = (baseProvider, options, app) => {
147147
unlogged,
148148
selectStatement,
149149
partitionOf,
150+
partitionBounds,
150151
},
151152
isActivated
152153
) {
@@ -163,24 +164,30 @@ module.exports = (baseProvider, options, app) => {
163164
key => key.statement
164165
);
165166
const keyConstraintsString = generateConstraintsString(dividedKeysConstraints, isActivated);
167+
const keyConstraintsValue = partitionOf ? keyConstraintsString?.slice(1) : keyConstraintsString
166168

167169
const dividedForeignKeys = divideIntoActivatedAndDeactivated(foreignKeyConstraints, key => key.statement);
168170
const foreignKeyConstraintsString = generateConstraintsString(dividedForeignKeys, isActivated);
169171

170172
const columnDescriptions = '\n' + getColumnComments(tableName, columnDefinitions);
171173
const template = partitionOf ? templates.createTablePartitionOf : templates.createTable;
172174

173-
const checkConstraintPrefix = partitionOf && !keyConstraintsString ? '\n\t' : ',\n\t'
175+
const checkConstraintPrefix = partitionOf && !keyConstraintsString ? '\n\t' : ',\n\t';
176+
const checkConstraintsValue = !_.isEmpty(checkConstraints)
177+
? wrap(_.join(checkConstraints, ',\n\t'), checkConstraintPrefix, '')
178+
: '';
179+
180+
const isEmptyPartitionBody = partitionOf && !keyConstraintsValue && !checkConstraintsValue && !foreignKeyConstraintsString
181+
const openParenthesis = isEmptyPartitionBody ? '': '('
182+
const closeParenthesis = isEmptyPartitionBody ? '' : ')'
174183

175184
const tableStatement = assignTemplates(template, {
176185
temporary: getTableTemporaryValue(temporary, unlogged),
177186
ifNotExist: ifNotExistStr,
178187
name: tableName,
179188
columnDefinitions: !partitionOf ? '\t' + _.join(columns, ',\n\t') : '',
180-
keyConstraints: partitionOf ? keyConstraintsString?.slice(1) : keyConstraintsString,
181-
checkConstraints: !_.isEmpty(checkConstraints)
182-
? wrap(_.join(checkConstraints, ',\n\t'), checkConstraintPrefix, '')
183-
: '',
189+
keyConstraints: keyConstraintsValue,
190+
checkConstraints: checkConstraintsValue,
184191
foreignKeyConstraints: foreignKeyConstraintsString,
185192
options: getTableOptions({
186193
inherits,
@@ -190,10 +197,13 @@ module.exports = (baseProvider, options, app) => {
190197
storage_parameter,
191198
table_tablespace_name,
192199
selectStatement,
200+
partitionBounds,
193201
}),
194202
comment: description ? comment : '',
195-
columnDescriptions,
196203
partitionOf: partitionOf ? ` PARTITION OF ${partitionOf} ` : '',
204+
columnDescriptions,
205+
openParenthesis,
206+
closeParenthesis,
197207
});
198208

199209
return tableStatement;
@@ -547,6 +557,7 @@ module.exports = (baseProvider, options, app) => {
547557
'on_commit',
548558
'storage_parameter',
549559
'table_tablespace_name',
560+
'partitionBounds'
550561
),
551562
};
552563
},

0 commit comments

Comments
 (0)