问题描述
简而言之
如何使用longblob
s.t将pgloader
从MysqL迁移到Postgres。如果列被注释为@Lob
和@Basic(fetch= FetchType.LAZY)
,Hibernate很高兴?
完整故事
因此,我正在将MysqL数据库迁移(或至少尝试)到postgres。我现在正尝试正确移动此表:
我当前的pgloader脚本非常简单:
LOAD DATABASE
FROM MysqL://foo:bar@localhost:3306/foobar
INTO postgresql://foo:bar@localhost:5432/foobar
CAST
type int to integer drop typemod,type bigint with extra auto_increment to bigserial drop typemod,type bigint to bigint drop typemod
ALTER TABLE NAMES MATCHING 'User' RENAME TO 'users'
ALTER TABLE NAMES MATCHING ~/./ SET SCHEMA 'public'
;
这足以加载数据并使外键正常工作。
postgres表如下:
File
是Java实体,其内容带有注释@Lob
:
@Entity
@Inheritance(strategy= InheritanceType.JOINED)
public class File extends BaseEntity {
@NotNull
private String name;
@Column
@Size(max = 4096)
private String description;
@NotNull
private String mimeType;
@Lob
@Basic(fetch= FetchType.LAZY)
private transient byte[] content;
...
}
Schema-validation: wrong column type encountered in column [content] in table [File];
found [bytea (Types#BINARY)],but expecting [oid (Types#BLOB)]
如何使此迁移正常工作?
我确实尝试过设置
spring.jpa.properties.hibernate.jdbc.use_streams_for_binary=false
如proper hibernate annotation for byte[]中的建议,但这没有任何作用。
解决方法
嗯……我想我可以根据Migrate PostgreSQL text/bytea column to large object?的建议在该事实之后创建blob。
意味着迁移脚本将获得扩展名:
LOAD DATABASE
FROM mysql://foo:bar@localhost:3306/foobar
INTO postgresql://foo:bar@localhost:5432/foobar
CAST
type int to integer drop typemod,type bigint with extra auto_increment to bigserial drop typemod,type bigint to bigint drop typemod
ALTER TABLE NAMES MATCHING 'User' RENAME TO 'users'
ALTER TABLE NAMES MATCHING ~/./ SET SCHEMA 'public'
AFTER LOAD DO
$$
ALTER TABLE file RENAME COLUMN content TO content_bytes;
$$,$$
ALTER TABLE file ADD COLUMN content OID;
$$,$$
UPDATE file SET
content = lo_from_bytea(0,content_bytes::bytea),content_bytes = NULL
;
$$,$$
ALTER TABLE file DROP COLUMN content_bytes
$$
;