Skip to content

Commit

Permalink
fix: .synchronize() drops json column on mariadb (#5391)
Browse files Browse the repository at this point in the history
* #3636 synchronize drops json column on mariadb

* fixes failing test - QueryFailedError: ER_BLOB_CANT_HAVE_DEFAULT: BLOB/TEXT column 'data' can't have a default value

* simple commit to trigger ci jobs
  • Loading branch information
creduo committed Feb 14, 2020
1 parent ceff897 commit e3c78c1
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/driver/mysql/MysqlDriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,15 @@ export class MysqlDriver implements Driver {
} else if (column.type === "uuid") {
return "varchar";

} else if (column.type === "json" && this.options.type === "mariadb") {
/*
* MariaDB implements this as a LONGTEXT rather, as the JSON data type contradicts the SQL standard,
* and MariaDB's benchmarks indicate that performance is at least equivalent.
*
* @see https://mariadb.com/kb/en/json-data-type/
*/
return "longtext";

} else if (column.type === "simple-array" || column.type === "simple-json") {
return "text";

Expand Down
16 changes: 16 additions & 0 deletions test/github-issues/3636/entity/Post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import {Column} from "../../../../src/decorator/columns/Column";
import {PrimaryColumn} from "../../../../src/decorator/columns/PrimaryColumn";
import {Entity} from "../../../../src/decorator/entity/Entity";

@Entity()
export class Post {

@PrimaryColumn()
id: number;

@Column({
type: "json",
})
data: any;

}
33 changes: 33 additions & 0 deletions test/github-issues/3636/issue-3636.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import "reflect-metadata";
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../utils/test-utils";
import {Connection} from "../../../src/connection/Connection";
import {Post} from "./entity/Post";
import {expect} from "chai";

describe("github issues > #3636 synchronize drops (and then re-adds) json column in mariadb", () => {

let connections: Connection[];
before(async () => connections = await createTestingConnections({
entities: [__dirname + "/entity/*{.js,.ts}"],
subscribers: [__dirname + "/subscriber/*{.js,.ts}"],
enabledDrivers: ["mariadb"],
}));
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("should not drop json column", () => Promise.all(connections.map(async function(connection) {

const post = new Post();
post.id = 1;
post.data = {hello: "world"};
await connection.manager.save(post);

await connection.synchronize();

const loadedPost = await connection.manager.findOne(Post, 1);

expect(loadedPost).to.be.not.empty;
expect(loadedPost!.data.hello).to.be.eq("world");
})));

});

0 comments on commit e3c78c1

Please sign in to comment.