mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
Merge pull request #527 from cfowlerdev/0.1.0
Issue #513 : fixed time and datetime column types for SQLite driver
This commit is contained in:
commit
7c581d6439
@ -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":
|
||||
|
||||
17
test/github-issues/513/entity/Post.ts
Normal file
17
test/github-issues/513/entity/Post.ts
Normal 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;
|
||||
|
||||
}
|
||||
82
test/github-issues/513/issue-513.ts
Normal file
82
test/github-issues/513/issue-513.ts
Normal 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);
|
||||
})));
|
||||
|
||||
});
|
||||
Loading…
x
Reference in New Issue
Block a user