Run all sql files with Chef

后端 未结 1 796
礼貌的吻别
礼貌的吻别 2021-01-14 21:21

Hello i need help i made a command that should read all the sql files inside of the sql_dumps folder but it isnt working.. here is what i got.

execute \"Run_         


        
相关标签:
1条回答
  • 2021-01-14 21:52

    There is a misconception here about how Chef compiles resources. You are expecting Chef to execute the command 15 times, but that's not how Chef operates. Chef runs in two phases - the execution phase, and the compilation phase. During the compilation phase (which runs first), the Ruby is evaluated and resources are added to the resource collection. With some exceptions, this phase does not alter the state of the system. So given your recipe:

    execute "Run_SQL_Files" do
      dirResults = Dir.glob("/tmp/sql_dumps/*.sql") 
      var = 0
      while var < 15 do
        var = var + 1 
        command "mysql --user=root --password=toomba source" + dirResults[var]
        # Already tried this also
        # command "mysql --user=root --password=toomba < "  dirResults[var]
        puts dirResults[var]
      end
    end
    

    That is functionally equivalent to a recipe that was written like this (after the compilation phase completes)"

    execute "Run_SQL_Files" do
      command "mysql --user=root --password=toomba source /tmp/sql_dumps/15.sql"
    end
    

    Notice that Chef is only going to utilize the last value for the command attribute. That is because Chef executes in two phases (as already mentioned).

    Using conditional logic and loops inside of a resource definition is almost always going to cause problems. In this example, you need to compile the command outside of the execute resource. Each SQL command you want to execute needs to have it's own execute block. Here's a simple, refactored example:

    Dir["/tmp/sql_dumps/*.sql"].each do |path|
      execute "run_sql_#{path}" do
        command "mysql --user=root --password=toomba < #{path}"
      end
    end
    

    This will put 15 (assumption from OP) execute resources in the resource collection, and execute them in order.

    0 讨论(0)
提交回复
热议问题