From f5c1509281ac8d420505d4207edabe6ce5467d8f Mon Sep 17 00:00:00 2001 From: Vokhmin Alexey V Date: Thu, 22 Aug 2013 20:12:54 +0400 Subject: [PATCH] #276: added RepositoryStatus model, migrations --- app/models/platform.rb | 24 ++++++++++++ app/models/repository.rb | 1 + app/models/repository_status.rb | 37 +++++++++++++++++++ ...130822154741_create_repository_statuses.rb | 14 +++++++ .../20130822160501_add_status_to_platform.rb | 7 ++++ 5 files changed, 83 insertions(+) create mode 100644 app/models/repository_status.rb create mode 100644 db/migrate/20130822154741_create_repository_statuses.rb create mode 100644 db/migrate/20130822160501_add_status_to_platform.rb diff --git a/app/models/platform.rb b/app/models/platform.rb index d365287ad..d6487af96 100644 --- a/app/models/platform.rb +++ b/app/models/platform.rb @@ -3,6 +3,15 @@ class Platform < ActiveRecord::Base VISIBILITIES = %w(open hidden) NAME_PATTERN = /[\w\-\.]+/ + READY = RepositoryStatus::READY + WAITING_FOR_REGENERATION = RepositoryStatus::WAITING_FOR_REGENERATION + REGENERATING = RepositoryStatus::REGENERATING + + HUMAN_STATUSES = { READY => :ready, + WAITING_FOR_REGENERATION => :waiting_for_regeneration, + REGENERATING => :regenerating + }.freeze + belongs_to :parent, :class_name => 'Platform', :foreign_key => 'parent_platform_id' belongs_to :owner, :polymorphic => true @@ -60,6 +69,21 @@ class Platform < ActiveRecord::Base include Modules::Models::Owner + state_machine :status, :initial => :ready do + event :ready do + transition :regenerating => :ready + end + + event :regenerate do + transition :waiting_for_regeneration => :regenerating + transition :ready => :waiting_for_regeneration + end + + HUMAN_STATUSES.each do |code,name| + state name, :value => code + end + end + def clear system("rm -Rf #{ APP_CONFIG['root_path'] }/platforms/#{ self.name }/repository/*") end diff --git a/app/models/repository.rb b/app/models/repository.rb index 3b981ac2f..98d42f131 100644 --- a/app/models/repository.rb +++ b/app/models/repository.rb @@ -12,6 +12,7 @@ class Repository < ActiveRecord::Base has_many :project_to_repositories, :dependent => :destroy, :validate => true has_many :projects, :through => :project_to_repositories has_one :key_pair, :dependent => :destroy + has_one :repository_statuses, :dependent => :destroy has_many :build_lists, :foreign_key => :save_to_repository_id, :dependent => :destroy diff --git a/app/models/repository_status.rb b/app/models/repository_status.rb new file mode 100644 index 000000000..588769bf8 --- /dev/null +++ b/app/models/repository_status.rb @@ -0,0 +1,37 @@ +class RepositoryStatus < ActiveRecord::Base + READY = 0 + WAITING_FOR_REGENERATION = 100 + REGENERATING = 200 + PUBLISH = 300 + + HUMAN_STATUSES = { READY => :ready, + WAITING_FOR_REGENERATION => :waiting_for_regeneration, + REGENERATING => :regenerating, + PUBLISH => :publish + }.freeze + + validates :repository_id, :platform_id, :presence => true + validates :repository_id, :uniqueness => {:scope => :platform_id} + + attr_accessible :last_regenerated_at, :last_regenerated_status, :platform_id, :repository_id, :status + + state_machine :status, :initial => :ready do + event :ready do + transition [:regenerating, :publish] => :ready + end + + event :regenerate do + transition :waiting_for_regeneration => :regenerating + transition :ready => :waiting_for_regeneration + end + + event :publish_packages do + transition :ready => :publish + end + + HUMAN_STATUSES.each do |code,name| + state name, :value => code + end + end + +end diff --git a/db/migrate/20130822154741_create_repository_statuses.rb b/db/migrate/20130822154741_create_repository_statuses.rb new file mode 100644 index 000000000..0614488e4 --- /dev/null +++ b/db/migrate/20130822154741_create_repository_statuses.rb @@ -0,0 +1,14 @@ +class CreateRepositoryStatuses < ActiveRecord::Migration + def change + create_table :repository_statuses do |t| + t.integer :repository_id, :null => false + t.integer :platform_id, :null => false + t.integer :status + t.datetime :last_regenerated_at + t.integer :last_regenerated_status + + t.timestamps + end + add_index :repository_statuses, [:repository_id, :platform_id], :unique => true + end +end diff --git a/db/migrate/20130822160501_add_status_to_platform.rb b/db/migrate/20130822160501_add_status_to_platform.rb new file mode 100644 index 000000000..655dbec52 --- /dev/null +++ b/db/migrate/20130822160501_add_status_to_platform.rb @@ -0,0 +1,7 @@ +class AddStatusToPlatform < ActiveRecord::Migration + def change + add_column :platforms, :status, :integer + add_column :platforms, :last_regenerated_at, :datetime + add_column :platforms, :last_regenerated_status, :integer + end +end