Skip to content

Commit 0397e44

Browse files
fix: Migration issues with scale & precision in sqlite/sql.js (#6638)
* fix: Migration issues with scale & precision in sqlite/sql.js Specifying precision or scale properties on columns with SQLite/sql.js would result in migrations being generated even on an unchanged schema. This was due to the precision and scale arguments not correctly being inferred when reading the table. This change handles scale and precision in the same way that "length" was already being correctly handled. Fixes #6636 * awaited the test * fix missing async Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
1 parent 490ad0d commit 0397e44

File tree

4 files changed

+78
-5
lines changed

4 files changed

+78
-5
lines changed

src/driver/sqlite-abstract/AbstractSqliteDriver.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,12 +130,31 @@ export abstract class AbstractSqliteDriver implements Driver {
130130
/**
131131
* Gets list of column data types that support precision by a driver.
132132
*/
133-
withPrecisionColumnTypes: ColumnType[] = [];
133+
withPrecisionColumnTypes: ColumnType[] = [
134+
"real",
135+
"double",
136+
"double precision",
137+
"float",
138+
"real",
139+
"numeric",
140+
"decimal",
141+
"date",
142+
"time",
143+
"datetime"
144+
];
134145

135146
/**
136147
* Gets list of column data types that support scale by a driver.
137148
*/
138-
withScaleColumnTypes: ColumnType[] = [];
149+
withScaleColumnTypes: ColumnType[] = [
150+
"real",
151+
"double",
152+
"double precision",
153+
"float",
154+
"real",
155+
"numeric",
156+
"decimal",
157+
];
139158

140159
/**
141160
* Orm has special columns and we need to know what database column types should be for those types.

src/driver/sqlite-abstract/AbstractSqliteQueryRunner.ts

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -825,17 +825,31 @@ export abstract class AbstractSqliteQueryRunner extends BaseQueryRunner implemen
825825
}
826826
}
827827

828-
// parse datatype and attempt to retrieve length
828+
// parse datatype and attempt to retrieve length, precision and scale
829829
let pos = tableColumn.type.indexOf("(");
830830
if (pos !== -1) {
831-
let dataType = tableColumn.type.substr(0, pos);
831+
const fullType = tableColumn.type;
832+
let dataType = fullType.substr(0, pos);
832833
if (!!this.driver.withLengthColumnTypes.find(col => col === dataType)) {
833-
let len = parseInt(tableColumn.type.substring(pos + 1, tableColumn.type.length - 1));
834+
let len = parseInt(fullType.substring(pos + 1, fullType.length - 1));
834835
if (len) {
835836
tableColumn.length = len.toString();
836837
tableColumn.type = dataType; // remove the length part from the datatype
837838
}
838839
}
840+
if (!!this.driver.withPrecisionColumnTypes.find(col => col === dataType)) {
841+
const re = new RegExp(`^${dataType}\\((\\d+),?\\s?(\\d+)?\\)`);
842+
const matches = fullType.match(re);
843+
if (matches && matches[1]) {
844+
tableColumn.precision = +matches[1];
845+
}
846+
if (!!this.driver.withScaleColumnTypes.find(col => col === dataType)) {
847+
if (matches && matches[2]) {
848+
tableColumn.scale = +matches[2];
849+
}
850+
}
851+
tableColumn.type = dataType; // remove the precision/scale part from the datatype
852+
}
839853
}
840854

841855
return tableColumn;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Column, Entity, PrimaryColumn } from "../../../../src";
2+
3+
@Entity()
4+
export class Test {
5+
6+
@PrimaryColumn()
7+
id: number;
8+
9+
@Column({ nullable: true, precision: 6 })
10+
startedAt?: Date;
11+
12+
@Column({ type: 'decimal', precision: 5, scale: 2 })
13+
value: number;
14+
15+
}

test/github-issues/6636/issue-6636.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import { Connection } from "../../../src";
2+
import { closeTestingConnections, createTestingConnections, reloadTestingDatabases } from "../../utils/test-utils";
3+
import { Test } from "./entity/Test";
4+
import { expect } from "chai";
5+
6+
describe("github issues > #6636 migration issues with scale & precision", () => {
7+
8+
let connections: Connection[];
9+
before(async () => connections = await createTestingConnections({
10+
entities: [Test],
11+
enabledDrivers: ["sqljs", "sqlite", "better-sqlite3"],
12+
}));
13+
beforeEach(() => reloadTestingDatabases(connections));
14+
after(() => closeTestingConnections(connections));
15+
16+
it("should not create migrations columns with precision", async () => {
17+
await Promise.all(connections.map(async (connection) => {
18+
const sqlInMemory = await connection.driver.createSchemaBuilder().log();
19+
expect(sqlInMemory.upQueries).to.eql([]);
20+
expect(sqlInMemory.downQueries).to.eql([]);
21+
}
22+
))
23+
});
24+
25+
});

0 commit comments

Comments
 (0)