Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add support multiple alter in alter table statement #60

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,11 @@ package-lock.json
coverage
dist
lib

# Vim swap files
[._]*.s[a-v][a-z]
[._]*.sw[a-p]
[._]s[a-rt-v][a-z]
[._]ss[a-gi-z]
[._]sw[a-p]

77 changes: 40 additions & 37 deletions src/ast-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface IAstPartialMapper {
transaction?: (val: a.CommitStatement | a.RollbackStatement | a.StartTransactionStatement) => a.Statement | nil
createIndex?: (val: a.CreateIndexStatement) => a.Statement | nil
alterTable?: (st: a.AlterTableStatement) => a.Statement | nil
tableAlteration?: (change: a.TableAlteration, table: a.QNameAliased) => a.TableAlteration | nil
dropColumn?: (change: a.TableAlterationDropColumn, table: a.QNameAliased) => a.TableAlteration | nil
renameConstraint?: (change: a.TableAlterationRenameConstraint, table: a.QNameAliased) => a.TableAlteration | nil
setTableOwner?: (change: a.TableAlterationOwner, table: a.QNameAliased) => a.TableAlteration | nil
Expand Down Expand Up @@ -642,55 +643,57 @@ export class AstDefaultMapper implements IAstMapper {
if (!table) {
return null; // no table
}
let change: a.TableAlteration | nil;
switch (st.change.type) {
case 'add column': {
change = this.addColumn(st.change, st.table);
break;
}
case 'add constraint': {
change = this.addConstraint(st.change, st.table);
break;
}
case 'alter column': {
change = this.alterColumn(st.change, st.table);
break;
}
case 'rename': {
change = this.renameTable(st.change, st.table);
break;
}
case 'rename column': {
change = this.renameColumn(st.change, st.table);
break;
}
case 'rename constraint': {
change = this.renameConstraint(st.change, st.table);
break;
}
case 'drop column': {
change = this.dropColumn(st.change, st.table);
break;
}
case 'owner': {
change = this.setTableOwner(st.change, st.table);
break;
let changes: a.TableAlteration[] = [];
let hasChanged: boolean = false;
for (let i = 0; i < (st.changes?.length || 0); i++) {
const currentChange: a.TableAlteration = st.changes[i];
const change: a.TableAlteration | nil = this.tableAlteration(currentChange, st.table);

hasChanged = hasChanged || (change != currentChange);

if (!!change) {
changes.push(change);
}
default:
throw NotSupported.never(st.change);
}

if (!change) {
if (!changes.length) {
return null; // no change left
}

if (!hasChanged) {
return st;
}

return assignChanged(st, {
table,
change,
changes,
});

}

tableAlteration(change: a.TableAlteration, table: a.QNameAliased): a.TableAlteration | nil {
switch (change.type) {
case 'add column':
return this.addColumn(change, table);
case 'add constraint':
return this.addConstraint(change, table);
case 'alter column':
return this.alterColumn(change, table);
case 'rename':
return this.renameTable(change, table);
case 'rename column':
return this.renameColumn(change, table);
case 'rename constraint':
return this.renameConstraint(change, table);
case 'drop column':
return this.dropColumn(change, table);
case 'owner':
return this.setTableOwner(change, table);
default:
throw NotSupported.never(change);
}
}

dropColumn(change: a.TableAlterationDropColumn, table: a.QNameAliased): a.TableAlteration | nil {
return change;
}
Expand Down
9 changes: 7 additions & 2 deletions src/syntax/alter-table.ne
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
# https://www.postgresql.org/docs/12/sql-altertable.html

altertable_statement -> kw_alter %kw_table kw_ifexists:? %kw_only:? table_ref
altertable_action {% x => track(x, {
altertable_actions {% x => track(x, {
type: 'alter table',
... x[2] ? {ifExists: true} : {},
... x[3] ? {only: true} : {},
table: unwrap(x[4]),
change: unwrap(x[5]),
changes: unbox(x[5]).map(unwrap),
}) %}


altertable_actions -> altertable_action (comma altertable_action {% last %}):* {% ([head, tail]) => {
return [head, ...(tail || [])];
} %}

altertable_action
-> altertable_rename_table
| altertable_rename_column
Expand Down Expand Up @@ -104,3 +108,4 @@ altercol_generated_seq
setSeqOpts(ret, x[1]);
return track(x, ret);
}%}

Loading