如何运行架构:加载我的rails应用程序的初始capistrano 3部署

时间:2022-03-07 20:38:03

I would like to run db:schema:load in place of db:migrate on the initial deploy of my rails app.

我想在我的rails应用程序的初始部署中运行db:schema:load代替db:migrate。

This used to be fairly trivial, as seen in this stack overflow question, but in Capistrano 3, they have deprecated the deploy:cold task. The initial deploy isn't any different than all subsequent deploys.

这曾经相当简单,如此堆栈溢出问题所示,但在Capistrano 3中,他们已经弃用了deploy:cold任务。初始部署与所有后续部署没有任何不同。

Any suggestions? Thanks!

有什么建议么?谢谢!

2 个解决方案

#1


2  

You'll have to define deploy:cold as basically a duplicate of the normal deploy task but with deploy:db_load_schema instead of deploy:migrations. For example:

您必须定义deploy:cold,因为它基本上是正常部署任务的副本,但使用deploy:db_load_schema而不是deploy:migrations。例如:

desc 'Deploy app for first time'
task :cold do
  invoke 'deploy:starting'
  invoke 'deploy:started'
  invoke 'deploy:updating'
  invoke 'bundler:install'
  invoke 'deploy:db_load_schema' # This replaces deploy:migrations
  invoke 'deploy:compile_assets'
  invoke 'deploy:normalize_assets'
  invoke 'deploy:publishing'
  invoke 'deploy:published'
  invoke 'deploy:finishing'
  invoke 'deploy:finished'
end

desc 'Setup database'
task :db_load_schema do
  on roles(:db) do
    within release_path do
      with rails_env: (fetch(:rails_env) || fetch(:stage)) do
        execute :rake, 'db:schema:load'
      end
    end
  end
end

It might even be better to run the deploy:db_schema_load task independently, as the tasks included in the default deploy might change over time.

由于默认部署中包含的任务可能会随时间发生变化,因此独立运行deploy:db_schema_load任务可能更好。

I actually using db:setup for fresh deploys because it seeds the database after creating tables:

我实际上使用db:setup进行新的部署,因为它在创建表后为数据库播种:

desc 'Setup database'
task :db_setup do
  ...
        execute :rake, 'db:setup'
  ...
end

#2


2  

I, too, am new to Capistrano, and trying to use it for the first time to deploy a Rails app to production servers I configured with Puppet.

我也是Capistrano的新手,并且第一次尝试使用它将Rails应用程序部署到我使用Puppet配置的生产服务器上。

I finally had to dig into the Capistrano source (and capistrano/bundler, and capistrano/rails, and even sshkit and net-ssh to debug auth problems) to determine exactly how everything works before I felt confidant deciding for myself what changes I wanted to make. I just finished making those changes, and I'm pleased with the results:

我终于不得不深入了解Capistrano源代码(和capistrano / bundler,以及capistrano / rails,甚至是sshkit和net-ssh来调试auth问题)以确定一切是如何工作的,然后才能让我知道我要为自己做出什么样的改变?使。我刚刚完成了这些更改,我对结果感到满意:

# lib/capistrano/tasks/cold.rake
namespace :deploy do

  desc "deploy app for the first time (expects pre-created but empty DB)"
  task :cold do
    before 'deploy:migrate', 'deploy:initdb'
    invoke 'deploy'
  end

  desc "initialize a brand-new database (db:schema:load, db:seed)"
  task :initdb do
    on primary :web do |host|
      within release_path do
        if test(:psql, 'portal_production -c "SELECT table_name FROM information_schema.tables WHERE table_schema=\'public\' AND table_type=\'BASE TABLE\';"|grep schema_migrations')
          puts '*** THE PRODUCTION DATABASE IS ALREADY INITIALIZED, YOU IDIOT! ***'
        else
          execute :rake, 'db:schema:load'
          execute :rake, 'db:seed'
        end
      end
    end
  end

end

The deploy:cold task merely hooks my custom deploy:inidb task to run before deploy:migrate. That way the schema and seeds get loaded, and the deploy:migrate step that follows does nothing (safely) because there are no new migrations to run. As a safety, I test to see if the schema_migrations table already exists before loading the schema in case you run deploy:cold again.

部署:冷任务仅挂钩我的自定义部署:inidb任务在部署之前运行:迁移。这样,架构和种子就会被加载,并且随后的deploy:migrate步骤不会(安全地)执行任何操作,因为没有新的迁移要运行。为安全起见,我在加载架构之前测试schema_migrations表是否已经存在,以防再次运行deploy:cold。

Note: I choose to create the DB using Puppet so I can avoid having to grant the CREATEDB privilege to my production postgresql user, but if you want Capistrano to do it, just add "execute :rake, 'db:create'" before the db:schema:load, or replace all three lines with 'db:setup'.

注意:我选择使用Puppet创建数据库,因此我可以避免将CREATEDB权限授予我的生产postgresql用户,但是如果你想要Capistrano这样做,只需在执行前添加“execute:rake,'db:create'”。 db:schema:使用'db:setup'加载或替换所有三行。

#1


2  

You'll have to define deploy:cold as basically a duplicate of the normal deploy task but with deploy:db_load_schema instead of deploy:migrations. For example:

您必须定义deploy:cold,因为它基本上是正常部署任务的副本,但使用deploy:db_load_schema而不是deploy:migrations。例如:

desc 'Deploy app for first time'
task :cold do
  invoke 'deploy:starting'
  invoke 'deploy:started'
  invoke 'deploy:updating'
  invoke 'bundler:install'
  invoke 'deploy:db_load_schema' # This replaces deploy:migrations
  invoke 'deploy:compile_assets'
  invoke 'deploy:normalize_assets'
  invoke 'deploy:publishing'
  invoke 'deploy:published'
  invoke 'deploy:finishing'
  invoke 'deploy:finished'
end

desc 'Setup database'
task :db_load_schema do
  on roles(:db) do
    within release_path do
      with rails_env: (fetch(:rails_env) || fetch(:stage)) do
        execute :rake, 'db:schema:load'
      end
    end
  end
end

It might even be better to run the deploy:db_schema_load task independently, as the tasks included in the default deploy might change over time.

由于默认部署中包含的任务可能会随时间发生变化,因此独立运行deploy:db_schema_load任务可能更好。

I actually using db:setup for fresh deploys because it seeds the database after creating tables:

我实际上使用db:setup进行新的部署,因为它在创建表后为数据库播种:

desc 'Setup database'
task :db_setup do
  ...
        execute :rake, 'db:setup'
  ...
end

#2


2  

I, too, am new to Capistrano, and trying to use it for the first time to deploy a Rails app to production servers I configured with Puppet.

我也是Capistrano的新手,并且第一次尝试使用它将Rails应用程序部署到我使用Puppet配置的生产服务器上。

I finally had to dig into the Capistrano source (and capistrano/bundler, and capistrano/rails, and even sshkit and net-ssh to debug auth problems) to determine exactly how everything works before I felt confidant deciding for myself what changes I wanted to make. I just finished making those changes, and I'm pleased with the results:

我终于不得不深入了解Capistrano源代码(和capistrano / bundler,以及capistrano / rails,甚至是sshkit和net-ssh来调试auth问题)以确定一切是如何工作的,然后才能让我知道我要为自己做出什么样的改变?使。我刚刚完成了这些更改,我对结果感到满意:

# lib/capistrano/tasks/cold.rake
namespace :deploy do

  desc "deploy app for the first time (expects pre-created but empty DB)"
  task :cold do
    before 'deploy:migrate', 'deploy:initdb'
    invoke 'deploy'
  end

  desc "initialize a brand-new database (db:schema:load, db:seed)"
  task :initdb do
    on primary :web do |host|
      within release_path do
        if test(:psql, 'portal_production -c "SELECT table_name FROM information_schema.tables WHERE table_schema=\'public\' AND table_type=\'BASE TABLE\';"|grep schema_migrations')
          puts '*** THE PRODUCTION DATABASE IS ALREADY INITIALIZED, YOU IDIOT! ***'
        else
          execute :rake, 'db:schema:load'
          execute :rake, 'db:seed'
        end
      end
    end
  end

end

The deploy:cold task merely hooks my custom deploy:inidb task to run before deploy:migrate. That way the schema and seeds get loaded, and the deploy:migrate step that follows does nothing (safely) because there are no new migrations to run. As a safety, I test to see if the schema_migrations table already exists before loading the schema in case you run deploy:cold again.

部署:冷任务仅挂钩我的自定义部署:inidb任务在部署之前运行:迁移。这样,架构和种子就会被加载,并且随后的deploy:migrate步骤不会(安全地)执行任何操作,因为没有新的迁移要运行。为安全起见,我在加载架构之前测试schema_migrations表是否已经存在,以防再次运行deploy:cold。

Note: I choose to create the DB using Puppet so I can avoid having to grant the CREATEDB privilege to my production postgresql user, but if you want Capistrano to do it, just add "execute :rake, 'db:create'" before the db:schema:load, or replace all three lines with 'db:setup'.

注意:我选择使用Puppet创建数据库,因此我可以避免将CREATEDB权限授予我的生产postgresql用户,但是如果你想要Capistrano这样做,只需在执行前添加“execute:rake,'db:create'”。 db:schema:使用'db:setup'加载或替换所有三行。