生成具有目标数据库数据类型的XML/SQL输出

编程入门 行业动态 更新时间:2024-10-25 16:21:22
本文介绍了生成具有目标数据库数据类型的XML/SQL输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要将PostgreSQL数据库的结构转换为Oracle.在PostgreSQL中,我有一个包含数据的postgres数据库.

I need to convert the structure of PostgreSQL databases to Oracle. In PostgreSQL, I have a postgres database with data.

在Oracle中,我有一个空白数据库,我想在其中写入PostgreSQL中的postgres数据库.

In Oracle I have a blank database in which I want to write postgres database which in PostgreSQL.

实际上,我不需要数据,只需要结构(关系).

In fact, I do not need the data, only the structure (relationships).

为此,我使用 Liquibase .我使用以下命令从PostgreSQL获取变更日志:

For this I use Liquibase. I get the changelog from PostgreSQL with the command:

liquibase \ --driver=org.postgresql.Driver \ --classpath="C:\db_drivers\postgresql-9.3-1102.jdbc3.jar" \ --changeLogFile="./postgresql_changelog.xml" \ --url="jdbc:postgresql://localhost:5432/postgres" \ --username=schema_name_here \ --password=************** \ --logLevel=debug \ --defaultSchemaName=sep \ generateChangeLog

此后,我尝试在Oracle数据库中创建对象:

After this I try to create objects in the Oracle database:

liquibase \ --driver=oracle.jdbc.OracleDriver \ --classpath="C:\db_drivers\ojdbc14.jar" \ --changeLogFile="./postgresql_changelog.xml" \ --url="jdbc:oracle:thin:@ip_here:orabeta" \ --username=*** \ --password=*** \ update

不起作用: ORA-00902

这是postgresql_changelog.xml的一部分:

Here is a fragment of postgresql_changelog.xml:

... <changeSet author="Alexey (generated)" id="1409146335011-53"> <createTable tableName="TABLE1A"> <column name="total_pk" type="INT8"> <constraints nullable="false"/> </column> <column name="form_fk" type="INT8"> <constraints nullable="false"/> </column> ...

我还生成一个纯SQL-文件:

I also generate a pure SQL- file:

liquibase \ --driver=oracle.jdbc.OracleDriver \ --classpath="C:\db_drivers\ojdbc14.jar" \ --changeLogFile="./postgresql_changelog.xml" \ --url="jdbc:oracle:thin:@ip_here:orabeta" \ --username=*** \ --password=*** \ updateSQL > update.sql

这是update.sql的一部分:

Here is a fragment of update.sql:

... CREATE TABLE SCHEMA_HERE.TABLE1A (total_pk INT8 NOT NULL, form_fk INT8, .....etc ); INSERT INTO SCHEMA_HERE.TABLE1A (ID, FORM_ID, ...etc) ...

我想生成一个文件,所有数据类型都对应于目标数据库,即我要创建的文件.我可以编写一个简单的解析器来替换数据类型,但这不是正确的解决方案-可以有很多数据库.

I would like to generate the file, in which all data types correspond to the target database, ie that I want to create. I can write a simple parser that replace data types, but it is not the right solution - can be many database.

是否可以使用目标数据库的数据类型生成XML/SQL输出?

It possible to generate XML/ SQL output with data types of target database?

或者也许有一个选项可以生成具有抽象"数据类型的输出?也就是说,例如使用不在真实数据库中的数据类型,而不是INT8-抽象整数数据类型,等等.

Or maybe there is an option that allow to generate output with "abstract" data types? Ie with the data types that are not in the real databases, for example, instead of INT8 - abstract integer data type, etc.

推荐答案

实际上,有必要手动更正生成文件中的数据类型(不仅是数据类型,还包括所有数据类型).数据库的详细信息(减少约束名称,索引等的长度.)数据类型不会自动转换.

Actually, it is necessary to manually correct the data types in the generated file (and not only the data types, but also all specifics of the database(reduce the length of the constraint names, indexes, etc.). Data types are not converted automatically.

例如:

... <changeSet author="Alexey (generated)" id="1409146335011-53"> <createTable tableName="TABLE1A"> <!-- replace INT8 to NUMBER(16) for Oracle (for example) --> <column name="total_pk" type="INT8"> <constraints nullable="false"/> </column> <!-- replace INT4 to NUMBER(10) for Oracle (for example) --> <column name="form_fk" type="INT4"> <constraints nullable="false"/> </column> ...

在那之后执行命令:

liquibase \ --driver=oracle.jdbc.OracleDriver \ --classpath="C:\db_drivers\ojdbc14.jar" \ --changeLogFile="./postgresql_changelog.xml" \ --url="jdbc:oracle:thin:@ip_here:orabeta" \ --username=*** \ --password=*** \ update

该结构将在目标数据库上生成.

The structure will be generated on the target database.

但是,实际上,可以使用抽象"数据类型,如文档中所述: Liquibase ,列标记

But, really, can be used "abstract" data types, as write in the documentation: Liquibase, Column tag

为帮助使脚本独立于数据库,以下通用" 数据类型将转换为正确的数据库实现: BOOLEAN CURRENCY UUID CLOB BLOB DATE DATETIME TIME BIGINT

To help make scripts database-independent, the following "generic" data types will be converted to the correct database implementation: BOOLEAN CURRENCY UUID CLOB BLOB DATE DATETIME TIME BIGINT

此外,指定java.sql.Types.*类型将转换为 以及正确的类型.如果需要,可以包括精度.这是 一些示例:java.sql.Types.TIMESTAMP java.sql.Types.VARCHAR(255)

Also, specifying a java.sql.Types.* type will be converted to the correct type as well. If needed, precision can be included. Here are some examples: java.sql.Types.TIMESTAMP java.sql.Types.VARCHAR(255)

并且有可能在没有DDL的情况下复制结构(以及没有DML的数据).

And possible to make the copy of structure without DDL (and data without DML).

同样有可能迁移数据(选项--diffTypes = "data"):

Is similarly possible migrate the data (option --diffTypes = "data"):

liquibase \ --driver=org.postgresql.Driver \ --classpath="C:\db_drivers\postgresql-9.3-1102.jdbc3.jar" \ --changeLogFile="./data.xml" \ --url="jdbc:postgresql://localhost:5432/postgres" \ --username=*** \ --password=*** \ --logLevel=debug \ --defaultSchemaName=schema_name_here \ --diffTypes="data" \ generateChangeLog

并且暂时(手动)禁用所有约束后:

liquibase \ --driver=oracle.jdbc.OracleDriver \ --classpath="C:\db_drivers\ojdbc14.jar" \ --changeLogFile="./data.xml" \ --url="jdbc:oracle:thin:@ip_here:orabeta" \ --username=*** \ --password=*** \ --logLevel=debug \ update

更多推荐

生成具有目标数据库数据类型的XML/SQL输出

本文发布于:2023-10-27 15:26:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533785.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据类型   目标   数据库   SQL   XML

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!