From ea8bb0b42b8079b00c3a87e58770ca358ad7b3e2 Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Tue, 18 Feb 2014 01:02:48 +0400 Subject: [PATCH] #354:added specs for autostart build_lists --- Gemfile | 1 + Gemfile.lock | 6 +++ app/models/project.rb | 28 ++++++------ app/models/project_to_repository.rb | 9 ++++ app/models/repository.rb | 3 +- ...203553_add_autostart_status_to_projects.rb | 6 --- db/migrate/20140217192639_setup_hstore.rb | 9 ++++ ...192640_add_autostart_status_to_projects.rb | 6 +++ db/schema.rb | 4 +- spec/factories/platform_arch_settings.rb | 8 ++++ spec/models/project_spec.rb | 43 +++++++++++++++++++ 11 files changed, 101 insertions(+), 22 deletions(-) delete mode 100644 db/migrate/20140216203553_add_autostart_status_to_projects.rb create mode 100644 db/migrate/20140217192639_setup_hstore.rb create mode 100644 db/migrate/20140217192640_add_autostart_status_to_projects.rb create mode 100644 spec/factories/platform_arch_settings.rb diff --git a/Gemfile b/Gemfile index 85a0bd471..aea9b4bce 100644 --- a/Gemfile +++ b/Gemfile @@ -4,6 +4,7 @@ gem 'rails', '3.2.16' #, git: 'git://github.com/rails/rails.git' gem 'redhillonrails_core', git: 'git://github.com/rosa-abf/redhillonrails_core.git', branch: 'rails31' # '~> 2.0.0.pre' # deprecated gem 'pg', '~> 0.14.0' +gem 'activerecord-postgres-hstore' gem 'devise', '~> 2.2.3' gem 'omniauth' diff --git a/Gemfile.lock b/Gemfile.lock index b8e154100..894830a7c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -56,6 +56,10 @@ GEM activesupport (= 3.2.16) arel (~> 3.0.2) tzinfo (~> 0.3.29) + activerecord-postgres-hstore (0.7.7) + activerecord (>= 3.1) + pg-hstore (>= 1.1.5) + rake activeresource (3.2.16) activemodel (= 3.2.16) activesupport (= 3.2.16) @@ -272,6 +276,7 @@ GEM cocaine (~> 0.5.3) mime-types pg (0.14.1) + pg-hstore (1.2.0) polyglot (0.3.3) posix-spawn (0.3.8) puma (2.7.1) @@ -459,6 +464,7 @@ PLATFORMS DEPENDENCIES RedCloth + activerecord-postgres-hstore airbrake (~> 3.1.2) ancestry (~> 1.3.0) angular-i18n (= 0.1.2) diff --git a/app/models/project.rb b/app/models/project.rb index edfb3391e..cecee6ed6 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -49,11 +49,10 @@ class Project < ActiveRecord::Base attr_accessible :name, :description, :visibility, :srpm, :is_package, :default_branch, :has_issues, :has_wiki, :maintainer_id, :publish_i686_into_x86_64, - :url, :srpms_list, :mass_import, :add_to_repository_id, :architecture_dependent + :url, :srpms_list, :mass_import, :add_to_repository_id, :architecture_dependent, + :autostart_status attr_readonly :owner_id, :owner_type - serialize :default_platforms, Array - scope :recent, order("lower(#{table_name}.name) ASC") scope :search_order, order("CHAR_LENGTH(#{table_name}.name) ASC") scope :search, lambda {|q| @@ -172,7 +171,7 @@ class Project < ActiveRecord::Base # Select main and project platform repository(contrib, non-free and etc) # If main does not exist, will connect only project platform repository # If project platform repository is main, only main will be connect - main_rep_id = build_for_platform.repositories.find_by_name(%w(main base)).try(:id) + main_rep_id = build_for_platform.repositories.main.first.try(:id) include_repos = ([main_rep_id] << (save_to_platform.main? ? repository_id : nil)).compact.uniq project_version = project_version_for save_to_platform, build_for_platform @@ -301,26 +300,29 @@ class Project < ActiveRecord::Base class << self Modules::Models::Autostart::HUMAN_AUTOSTART_STATUSES.each do |autostart_status, human_autostart_status| define_method "autostart_build_lists_#{human_autostart_status}" do - autostart_iso_builds autostart_status + autostart_build_lists autostart_status end end end def self.autostart_build_lists(autostart_status) Project.where(autostart_status: autostart_status).find_each do |p| - Platform.where(id: p.default_platforms).each do |platform| - platform.platform_arch_settings.pluck(:arch_id).each do |arch_id| - build_list = build_lists.build do |bl| + p.project_to_repositories.autostart_enabled.includes(repository: :platform).each do |p_to_r| + repository = p_to_r.repository + platform = repository.platform + platform.platform_arch_settings.by_default.pluck(:arch_id).each do |arch_id| + build_list = p.build_lists.build do |bl| bl.save_to_platform = platform bl.build_for_platform = platform bl.update_type = 'newpackage' bl.arch_id = arch_id - bl.project_version = project_version_for(platform, platform) - bl.user = p.owner.is_a?(Group) ? p.owner.owner : p.owner - bl.auto_publish = true # TODO: ??? - bl.save_to_repository_id = p.repositories.where(platform_id: platform).first + bl.project_version = p.project_version_for(platform, platform) + bl.user = User.find(p_to_r.user_id) + bl.auto_publish = p_to_r.auto_publish == 'true' + bl.save_to_repository = repository + bl.include_repos = [repository.id, platform.repositories.main.first.try(:id)].uniq.compact end - build_list.save + build_list.save! end end end diff --git a/app/models/project_to_repository.rb b/app/models/project_to_repository.rb index 595a23592..206e79e99 100644 --- a/app/models/project_to_repository.rb +++ b/app/models/project_to_repository.rb @@ -1,13 +1,22 @@ class ProjectToRepository < ActiveRecord::Base + AUTOSTART_OPTIONS = %w(auto_publish user_id enabled) + belongs_to :project belongs_to :repository delegate :path, to: :project + scope :autostart_enabled, lambda { where("autostart_options -> 'enabled' = 'true'") } + after_destroy lambda { project.destroy_project_from_repository(repository) }, unless: lambda {Thread.current[:skip]} validate :one_project_in_platform_repositories, on: :create + serialize :autostart_options, ActiveRecord::Coders::Hstore + AUTOSTART_OPTIONS.each do |field| + store_accessor :autostart_options, field + end + protected def one_project_in_platform_repositories diff --git a/app/models/repository.rb b/app/models/repository.rb index 46f87f820..26ef77c96 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -21,7 +21,8 @@ class Repository < ActiveRecord::Base validates :description, presence: true validates :name, uniqueness: {scope: :platform_id, case_sensitive: false}, presence: true, format: {with: /\A[a-z0-9_\-]+\z/} - scope :recent, order("#{table_name}.name ASC") + scope :recent, order("#{table_name}.name ASC") + scope :main, lambda { where(name: %w(main base)) } before_destroy :detele_directory diff --git a/db/migrate/20140216203553_add_autostart_status_to_projects.rb b/db/migrate/20140216203553_add_autostart_status_to_projects.rb deleted file mode 100644 index 0176b97ba..000000000 --- a/db/migrate/20140216203553_add_autostart_status_to_projects.rb +++ /dev/null @@ -1,6 +0,0 @@ -class AddAutostartStatusToProjects < ActiveRecord::Migration - def change - add_column :projects, :autostart_status, :integer - add_column :projects, :default_platforms, :text - end -end diff --git a/db/migrate/20140217192639_setup_hstore.rb b/db/migrate/20140217192639_setup_hstore.rb new file mode 100644 index 000000000..cb286025c --- /dev/null +++ b/db/migrate/20140217192639_setup_hstore.rb @@ -0,0 +1,9 @@ +class SetupHstore < ActiveRecord::Migration + def self.up + execute "CREATE EXTENSION IF NOT EXISTS hstore" + end + + def self.down + execute "DROP EXTENSION IF EXISTS hstore" + end +end diff --git a/db/migrate/20140217192640_add_autostart_status_to_projects.rb b/db/migrate/20140217192640_add_autostart_status_to_projects.rb new file mode 100644 index 000000000..156b22ad4 --- /dev/null +++ b/db/migrate/20140217192640_add_autostart_status_to_projects.rb @@ -0,0 +1,6 @@ +class AddAutostartStatusToProjects < ActiveRecord::Migration + def change + add_column :projects, :autostart_status, :integer + add_column :project_to_repositories, :autostart_options, :hstore#, default: '', null: false + end +end diff --git a/db/schema.rb b/db/schema.rb index 793c9bffb..7f6db9edc 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended to check this file into your version control system. -ActiveRecord::Schema.define(:version => 20140216203553) do +ActiveRecord::Schema.define(:version => 20140217192640) do create_table "activity_feeds", :force => true do |t| t.integer "user_id", :null => false @@ -416,6 +416,7 @@ ActiveRecord::Schema.define(:version => 20140216203553) do t.integer "repository_id" t.datetime "created_at" t.datetime "updated_at" + t.hstore "autostart_options" end add_index "project_to_repositories", ["repository_id", "project_id"], :name => "index_project_to_repositories_on_repository_id_and_project_id", :unique => true @@ -442,7 +443,6 @@ ActiveRecord::Schema.define(:version => 20140216203553) do t.string "owner_uname", :null => false t.boolean "architecture_dependent", :default => false, :null => false t.integer "autostart_status" - t.text "default_platforms" end add_index "projects", ["owner_id", "name", "owner_type"], :name => "index_projects_on_name_and_owner_id_and_owner_type", :unique => true, :case_sensitive => false diff --git a/spec/factories/platform_arch_settings.rb b/spec/factories/platform_arch_settings.rb new file mode 100644 index 000000000..1f4fdb19d --- /dev/null +++ b/spec/factories/platform_arch_settings.rb @@ -0,0 +1,8 @@ +FactoryGirl.define do + factory :platform_arch_setting do + association :platform, factory: :platform + association :arch, factory: :arch + default true + time_living 777 + end +end diff --git a/spec/models/project_spec.rb b/spec/models/project_spec.rb index df98bb337..dedeb11b0 100644 --- a/spec/models/project_spec.rb +++ b/spec/models/project_spec.rb @@ -181,4 +181,47 @@ describe Project do owner.projects.should have(2).items end + context '#autostart_build_lists_once_a_*' do + let(:project) { FactoryGirl.create(:project_with_commit) } + let(:repository) { FactoryGirl.create(:repository) } + let(:user) { FactoryGirl.create(:user) } + + before do + repository.add_member user + repository.projects << project + p_to_r = project.project_to_repositories.where(repository_id: repository).first + p_to_r.enabled = true + p_to_r.user_id = user.id + p_to_r.save + + FactoryGirl.create(:platform_arch_setting, platform: repository.platform) + FactoryGirl.create(:platform_arch_setting, platform: repository.platform, default: false) + end + + it { ProjectToRepository.autostart_enabled.should have(1).item } + it { repository.platform.platform_arch_settings.should have(2).item } + + shared_examples_for 'autostart build_lists' do |once_a_12_hours, once_a_day, once_a_week| + it { lambda { Project.autostart_build_lists_once_a_12_hours }.should change{ BuildList.count }.by(once_a_12_hours) } + it { lambda { Project.autostart_build_lists_once_a_day }.should change{ BuildList.count }.by(once_a_day) } + it { lambda { Project.autostart_build_lists_once_a_week }.should change{ BuildList.count }.by(once_a_week) } + end + + context 'once_a_12_hours' do + before { project.update_attributes(autostart_status: Modules::Models::Autostart::ONCE_A_12_HOURS) } + it_should_behave_like 'autostart build_lists', 1, 0, 0 + end + + context 'once_a_day' do + before { project.update_attributes(autostart_status: Modules::Models::Autostart::ONCE_A_DAY) } + it_should_behave_like 'autostart build_lists', 0, 1, 0 + end + + context 'once_a_day' do + before { project.update_attributes(autostart_status: Modules::Models::Autostart::ONCE_A_WEEK) } + it_should_behave_like 'autostart build_lists', 0, 0, 1 + end + + end + end