简体   繁体   English

在文件夹中运行SQL脚本

[英]Running SQL scripts in folder

I have a scripts folder and I want to run SQL scripts against my database. 我有一个scripts文件夹,我想对我的数据库运行SQL脚本。 Is there a way I can iterate through all the SQL files and check if the SQL file is not executed by checking a table in my database. 有没有一种方法可以遍历所有SQL文件并通过检查数据库中的表来检查SQL文件是否未执行。

Check if the script was executed, if no then it execute else just skips it. 检查脚本是否已执行,如果否,则执行,否则跳过该脚本。 After running the SQL script, it should insert a entry in table just for keeping track that script was executed. 运行SQL脚本后,它应该在表中插入一个条目,以跟踪该脚本的执行情况。

I am thinking of writing a ruby or python script to achieve this. 我正在考虑编写一个ruby或python脚本来实现这一目标。 Any thoughts ? 有什么想法吗 ?

Before the answer starts: Your problem looks like a migration, eg the migrations from Sequel or Active Record 在回答开始之前:您的问题看起来像是迁移,例如从SequelActive Record 迁移

Please take first a look for this migrations before you try my answer. 在尝试我的答案之前,请先查看此迁移。


With Sequel you can try something like this: 使用Sequel,您可以尝试以下操作:

def call_sql_scripts(mask)
  DB.create_table(:executed){
    add_column :filename
  } unless DB.table_exists?(:executed)

  Dir[mask].each{|filename|
    if DB[:executed].filter(:filename => filename).first
      puts "Script %s was already executed" % filename
    else
      puts "Start %s" % filename
      begin 
        DB.run(File.read(filename))
        DB[:executed].insert(:filename => filename) #executed if run was successful
      rescue Sequel::DatabaseError
        puts "%s failed" % filename
      end
    end

  }
end

call_sql_scripts('*.sql')

The table :executed stores the already executed script files. :executed表存储已执行的脚本文件。

Attention: 注意:

  • If you rename or move the files, they are not detected as changes. 如果重命名或移动文件,则不会将它们检测为更改。
  • There is no defined sequence (if you need this, you must take a look for migrations.) 没有定义的顺序(如果需要此顺序,则必须查看迁移情况。)
  • I would prefer to use Sequel-commands to build the database and not loading SQL-scripts in Files. 我宁愿使用Sequel命令来构建数据库,也不在文件中加载SQL脚本。

A full minimal working example: 完整的最小工作示例:

File.open('1.sql', 'w'){|f| f << 'create table tab1 (`field` add_column)'}
File.open('2.sql', 'w'){|f| f << 'create table tab2  (`field` add_column)'}
File.open('3.sql', 'w'){|f| f << 'create table tab3  (`field` add_column)'}

require 'sequel'
DB = Sequel.sqlite('test2.db')

def call_sql_scripts(mask)
  DB.create_table(:executed){
    add_column :filename
  } unless DB.table_exists?(:executed)

  Dir[mask].each{|filename|
    if DB[:executed].filter(:filename => filename).first
      puts "Script %s was already executed" % filename
    else
      puts "Start %s" % filename
      begin 
        DB.run(File.read(filename))
        DB[:executed].insert(:filename => filename) #executed if run was successful
      rescue Sequel::DatabaseError
        puts "%s failed" % filename
      end
    end

  }
end

call_sql_scripts('*.sql')

If you call it the 2nd time you see the scripts are not called again. 如果您第二次调用它,则会看到脚本不再被调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM