如何自动执行SQL文件?
从长时间的应用程序开始,我们就拥有CICD。现在,我们也计划自动执行DB脚本执行。 PFB要求。
例如,我们有create_tables.sql和alter_tables.sql文件。
create_tables.sql包含:
CREATE TABLE EMPLOYEE_DETAILS (
EMP_ID VARCHAR2(128 CHAR) NOT NULL,
FIRSTNAME VARCHAR2(128 CHAR),
LASTNAME VARCHAR2(128 CHAR),
CONSTRAINT REQUESTSUBMITTERS_PK PRIMARY KEY ( EMP_ID )
);
alter_tables.sql包含:
--v0.1
ALTER TABLE EMPLOYEE_DETAILS MODIFY FIRSTNAME VARCHAR2(256);
COMMIT;
--v0.2
ALTER TABLE EMPLOYEE_DETAILS MODIFY FIRSTNAME VARCHAR2(256) NOT NULL;
ALTER TABLE EMPLOYEE_DETAILS MODIFY LASTNAME VARCHAR2(256) NOT NULL;
COMMIT;
--v0.3
ALTER TABLE EMPLOYEE_DETAILS ADD EMAIL VARCHAR2(256) NOT NULL;
COMMIT;
假设我们有一个环境ABC。此环境具有create_tables.sql文件中可用的所有更改,但仅-v0.1从alter_tables.sql文件更改。 现在,我们只想促进 Just -v0.2从Alter_tables.sql文件更改为此环境。请注意,我们不想从Alter_tables.sql文件促进更改-v0.1和-v0.3。 因此,通常,每个SQL文件中都会有-v0.xxx(一个唯一的增量指示器),以指定要执行的SQL语句。
您能否让我知道可以自动使用这种方法的方法。 另外,请随时提出您认为比以上更好的其他解决方案。
We have CICD in place from long time for our application. Now, we are planning to automate DB script execution as well.
PFB the requirement.
For example, we have create_tables.sql and alter_tables.sql files.
create_tables.sql contains:
CREATE TABLE EMPLOYEE_DETAILS (
EMP_ID VARCHAR2(128 CHAR) NOT NULL,
FIRSTNAME VARCHAR2(128 CHAR),
LASTNAME VARCHAR2(128 CHAR),
CONSTRAINT REQUESTSUBMITTERS_PK PRIMARY KEY ( EMP_ID )
);
alter_tables.sql contains:
--v0.1
ALTER TABLE EMPLOYEE_DETAILS MODIFY FIRSTNAME VARCHAR2(256);
COMMIT;
--v0.2
ALTER TABLE EMPLOYEE_DETAILS MODIFY FIRSTNAME VARCHAR2(256) NOT NULL;
ALTER TABLE EMPLOYEE_DETAILS MODIFY LASTNAME VARCHAR2(256) NOT NULL;
COMMIT;
--v0.3
ALTER TABLE EMPLOYEE_DETAILS ADD EMAIL VARCHAR2(256) NOT NULL;
COMMIT;
Assume that we have an environments ABC. This environment has all changes available in create_tables.sql file but only --v0.1 change from alter_tables.sql file.
Now, we just want to promote JUST --v0.2 change from alter_tables.sql file to this environment. Please note that we don't want to promote changes --v0.1 and --v0.3 from alter_tables.sql file.
So, in general there will be --v0.xxx (a unique incremental indicator) in each sql file to specify which sql statements to execute.
Can you please let me know what could be the way to automate with this approach.
Also, please feel free to suggest any other solution which you think is better than above one.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,
alter
是DDL,它在执行该命令之前和之后隐含地提交,因此 - 明确commit
ting几乎是没有用的。从我的角度来看,您的方法太难应用了,因为您将所有的更改存储在同一文件中,因此您必须对其进行解析,并仅提取一些(所需的部分)要执行的部分。
如何创建一组表包含什么应用?以下是一个示例:
一些示例数据:
请注意,ID = 1和4包含重复命令:
这是将在此操场上使用的表:
完成工作的过程;在两个嵌套循环中,它读取计划运行的 master 表(
cb_apply = 1
),但尚未运行(applied_date为null <<) /代码>)。内部循环只是读取该怎么做,执行它并记录是否成功。
基本上,您使用 schedule 使用
dbms_scheduler
(或dbms_job
,取决于您的数据库版本):好的,让我们尝试一下。设置日期格式,只是要知道什么是:
现在,让我们运行
change = 2
:right;这是一种作品。
当然,应该/应该/应该改进那件代码(我在几分钟之内写),但这只是一般的想法。
另一方面,为什么不使用版本控制系统(例如git或subversion)这样做?好人开发了这些工具,这些工具强大得多,以至于我(或您)在这么短的时间内都可以“发明”任何工具。我想检查这些产品是值得的。
First of all,
ALTER
is a DDL and it implicitly commits before and after executing that command, so - explicitlyCOMMIT
ting is pretty much useless.From my point of view, your approach is too difficult to apply as you have all the changes stored in the same file so you'd have to parse it and extract only some - desired - parts of it to be executed.
How about creating a set of tables that contains what to apply? Here's an example:
Some sample data:
Note that ID = 1 and 4 contain duplicate commands:
This is table that will be used in this playground:
Procedure that does the job; in two nested loops, it reads rows from the master table which are scheduled to run (
cb_apply = 1
) but have not been ran yet (applied_date is null
). Inner loop just reads what to do, executes it and logs whether it was successful or not.Basically, you'd schedule its execution using
DBMS_SCHEDULER
(orDBMS_JOB
, depending on your database version):OK, let's try it. Setting date format, just to know what is what:
Let's now run
change = 2
:Right; it kind of works.
Certainly, that piece of code (I wrote in a matter of minutes) could/should be improved, but that's just the general idea.
On the other hand, why wouldn't you do that using version control system, such as Git or Subversion? Good people developed these tools which are much, much more powerful that anything me (or you) could "invent" in such a short time. I guess it's worth to check these products.