fixed time and datetime column types for SQLite driver

This commit is contained in:
cfowlerdev 2017-06-08 22:26:14 +08:00
parent d99ecb439d
commit 82792087ff
3 changed files with 101 additions and 10 deletions

View File

@ -772,17 +772,9 @@ export class SqliteQueryRunner implements QueryRunner {
case "date":
return "date";
case "time":
if (typeOptions.timezone) {
return "time with time zone";
} else {
return "time without time zone";
}
return "text";
case "datetime":
if (typeOptions.timezone) {
return "timestamp with time zone";
} else {
return "timestamp without time zone";
}
return "datetime";
case "json":
return "json";
case "simple_array":

View File

@ -0,0 +1,17 @@
import {Entity} from "../../../../src/decorator/entity/Entity";
import {PrimaryColumn} from "../../../../src/decorator/columns/PrimaryColumn";
import {Column} from "../../../../src/decorator/columns/Column";
@Entity()
export class Post {
@PrimaryColumn("int")
id: number;
@Column({type: "datetime", nullable: true})
dateTimeColumn: Date;
@Column({type: "time", nullable: true})
timeColumn: Date;
}

View File

@ -0,0 +1,82 @@
import "reflect-metadata";
import {createTestingConnections, closeTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {ObjectLiteral} from "../../../src/common/ObjectLiteral";
import {expect} from "chai";
import {Post} from "./entity/Post";
describe("github issues > #513 Incorrect time/datetime types for SQLite", () => {
let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
schemaCreate: true,
dropSchemaOnConnection: true,
enabledDrivers: ["sqlite"]
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));
it("should create datetime column type for datetime in sqlite", () => Promise.all(connections.map(async connection => {
const dbColumns: ObjectLiteral[] = await connection.entityManager.query("PRAGMA table_info(Post)");
expect(dbColumns).not.to.be.null;
expect(dbColumns).not.to.be.empty;
let columnType: string = "";
dbColumns.map((dbColumn) => {
if (dbColumn["name"] === "dateTimeColumn") {
columnType = dbColumn["type"];
}
});
// Expect "datetime" type to translate to SQLite affinity type "DATETIME"
columnType.should.equal("datetime");
})));
it("should persist correct type in datetime column in sqlite", () => Promise.all(connections.map(async connection => {
const now: Date = new Date();
const post: Post = new Post();
post.id = 1;
post.dateTimeColumn = now;
await connection.entityManager.persist(post);
const storedPost = await connection.entityManager.findOneById(Post, post.id);
expect(storedPost).to.not.be.null;
storedPost!.dateTimeColumn.toDateString().should.equal(now.toDateString());
})));
it("should create datetime column type for time in sqlite", () => Promise.all(connections.map(async connection => {
const dbColumns: ObjectLiteral[] = await connection.entityManager.query("PRAGMA table_info(Post)");
expect(dbColumns).not.to.be.null;
expect(dbColumns).not.to.be.empty;
let columnType: string = "";
dbColumns.map((dbColumn) => {
if (dbColumn["name"] === "timeColumn") {
columnType = dbColumn["type"];
}
});
// Expect "time" type to translate to SQLite type "TEXT"
columnType.should.equal("text");
})));
it("should persist correct type in datetime column in sqlite", () => Promise.all(connections.map(async connection => {
const now: Date = new Date();
const post: Post = new Post();
post.id = 2;
post.timeColumn = now; // Should maybe use Date type?
await connection.entityManager.persist(post);
const storedPost = await connection.entityManager.findOneById(Post, post.id);
expect(storedPost).to.not.be.null;
const expectedTimeString = now.getHours() + ":" + now.getMinutes() + ":" + now.getSeconds();
storedPost!.timeColumn.toString().should.equal(expectedTimeString);
})));
});