Skip to content

Commit eeea59f

Browse files
committed
fix alter table issue
1 parent 6dc3ee5 commit eeea59f

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "pg-mem",
3-
"version": "1.0.20",
3+
"version": "1.0.21",
44
"description": "A memory version of postgres",
55
"main": "index.js",
66
"scripts": {

src/table.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ type Raw<T> = ImMap<string, T>;
2020
export class MemoryTable<T = any> extends DataSourceBase<T> implements IMemoryTable, _ITable<T> {
2121

2222
private handlers = new Map<TableEvent, Set<() => void>>();
23-
readonly selection: _ISelection<T>;
23+
readonly selection: Alias<T>;
2424
private it = 0;
2525
hasPrimary: boolean;
2626
private readonly: boolean;
@@ -54,7 +54,7 @@ export class MemoryTable<T = any> extends DataSourceBase<T> implements IMemoryTa
5454
constructor(readonly schema: _ISchema, t: _Transaction, _schema: Schema) {
5555
super(schema);
5656
this.name = _schema.name;
57-
this.selection = buildAlias(this, this.name);
57+
this.selection = buildAlias(this, this.name) as Alias<T>;
5858

5959
// fields
6060
for (const s of _schema.fields) {
@@ -157,6 +157,7 @@ export class MemoryTable<T = any> extends DataSourceBase<T> implements IMemoryTa
157157
// once constraints created, reference them. (constraint creation might have thrown)m
158158
this.columns.push(cref.expression);
159159
this.schema.db.onSchemaChange();
160+
this.selection.rebuild();
160161
}
161162

162163

src/tests/insert.queries.spec.ts

+7
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,11 @@ describe('[Queries] Inserts', () => {
132132
.to.deep.equal([{ id: 1 }]);
133133
})
134134

135+
it('[bugfix] allows returning statement', () => {
136+
expect(many(`CREATE TABLE "user" ("id" SERIAL NOT NULL, "name" text NOT NULL, CONSTRAINT "PK_cace4a159ff9f2512dd42373760" PRIMARY KEY ("id"));
137+
ALTER TABLE "user" ADD data jsonb;
138+
INSERT INTO "user"("name", "data") VALUES ('me', '{"tags":["nice"]}') RETURNING "id";`))
139+
.to.deep.equal([{ id: 1 }])
140+
})
141+
135142
});

src/transforms/alias.ts

+6
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,12 @@ export function buildAlias(on: _ISelection, alias?: string): _ISelection<any> {
1414

1515
export class Alias<T> extends TransformBase<T>{
1616

17+
rebuild() {
18+
this._columns = null;
19+
this.oldToThis.clear();
20+
this.thisToOld.clear();
21+
}
22+
1723
private oldToThis = new Map<IValue, IValue>();
1824
private thisToOld = new Map<IValue, IValue>();
1925
get debugId() {

0 commit comments

Comments
 (0)