Commit 7f67108a authored by Douwe Maan's avatar Douwe Maan

Add project_mirror_data columns at the appropriate times

parent c3a8d277
......@@ -4,17 +4,25 @@ class CreateProjectMirrorData < ActiveRecord::Migration
DOWNTIME = false
def up
return if table_exists?(:project_mirror_data)
create_table :project_mirror_data do |t|
t.references :project, index: true, foreign_key: { on_delete: :cascade }
t.string :status
t.string :jid
t.text :last_error
if table_exists?(:project_mirror_data)
add_column :project_mirror_data, :status, :string
add_column :project_mirror_data, :jid, :string
add_column :project_mirror_data, :last_error, :text
else
create_table :project_mirror_data do |t|
t.references :project, index: true, foreign_key: { on_delete: :cascade }
t.string :status
t.string :jid
t.text :last_error
end
end
end
def down
drop_table(:project_mirror_data) if table_exists?(:project_mirror_data)
remove_column :project_mirror_data, :status
remove_column :project_mirror_data, :jid
remove_column :project_mirror_data, :last_error
# ee/db/migrate/20170509153720_create_project_mirror_data_ee.rb will remove the table.
end
end
......@@ -7,29 +7,34 @@ class CreateProjectMirrorDataEE < ActiveRecord::Migration
def up
# When moving from CE to EE, project_mirror_data may already exist, but will
# not have all the required columns. These are added in AddMissingColumnsToProjectMirrorData.
return if table_exists?(:project_mirror_data)
execute <<-SQL
CREATE TABLE project_mirror_data
AS (
SELECT id AS project_id,
0 AS retry_count,
CAST(NULL AS #{timestamp}) AS last_update_started_at,
CAST(NULL AS #{timestamp}) AS last_update_scheduled_at,
NOW() AS next_execution_timestamp,
NOW() AS created_at,
NOW() AS updated_at
FROM projects
WHERE mirror IS TRUE
);
SQL
add_column :project_mirror_data, :id, :primary_key
change_column_default :project_mirror_data, :retry_count, 0
change_column_null :project_mirror_data, :retry_count, false
add_concurrent_foreign_key :project_mirror_data, :projects, column: :project_id
add_concurrent_index :project_mirror_data, [:project_id], unique: true
# not have all the required columns.
if table_exists?(:project_mirror_data)
add_column_with_default :project_mirror_data, :retry_count, :integer, default: 0, allow_null: false
add_column :project_mirror_data, :last_update_started_at, :datetime_with_timezone
add_column :project_mirror_data, :last_update_scheduled_at, :datetime_with_timezone
add_column :project_mirror_data, :next_execution_timestamp, :datetime_with_timezone
else
execute <<-SQL
CREATE TABLE project_mirror_data
AS (
SELECT id AS project_id,
0 AS retry_count,
CAST(NULL AS #{timestamp}) AS last_update_started_at,
CAST(NULL AS #{timestamp}) AS last_update_scheduled_at,
NOW() AS next_execution_timestamp,
NOW() AS created_at,
NOW() AS updated_at
FROM projects
WHERE mirror IS TRUE
);
SQL
add_column :project_mirror_data, :id, :primary_key
change_column_default :project_mirror_data, :retry_count, 0
change_column_null :project_mirror_data, :retry_count, false
add_concurrent_foreign_key :project_mirror_data, :projects, column: :project_id
add_concurrent_index :project_mirror_data, [:project_id], unique: true
end
end
def down
......
......@@ -10,38 +10,12 @@ class AddMissingColumnsToProjectMirrorData < ActiveRecord::Migration
disable_ddl_transaction!
def up
# Columns missing when a CE instance is upgraded to EE
unless column_exists? :project_mirror_data, :retry_count
add_column_with_default :project_mirror_data, :retry_count, :integer, default: 0, allow_null: false
add_column :project_mirror_data, :last_update_started_at, :datetime
add_column :project_mirror_data, :last_update_scheduled_at, :datetime
add_column :project_mirror_data, :next_execution_timestamp, :datetime
end
# Columns missing on an EE instance
unless column_exists? :project_mirror_data, :status
add_column :project_mirror_data, :status, :string
add_column :project_mirror_data, :jid, :string
add_column :project_mirror_data, :last_update_at, :datetime_with_timezone
add_column :project_mirror_data, :last_successful_update_at, :datetime_with_timezone
add_column :project_mirror_data, :last_error, :text
end
add_column :project_mirror_data, :last_update_at, :datetime_with_timezone
add_column :project_mirror_data, :last_successful_update_at, :datetime_with_timezone
end
def down
if column_exists? :project_mirror_data, :retry_count
remove_column :project_mirror_data, :retry_count
remove_column :project_mirror_data, :last_update_started_at
remove_column :project_mirror_data, :last_update_scheduled_at
remove_column :project_mirror_data, :next_execution_timestamp
end
if column_exists? :project_mirror_data, :status
remove_column :project_mirror_data, :status
remove_column :project_mirror_data, :jid
remove_column :project_mirror_data, :last_update_at
remove_column :project_mirror_data, :last_successful_update_at
remove_column :project_mirror_data, :last_error
end
remove_column :project_mirror_data, :last_update_at
remove_column :project_mirror_data, :last_successful_update_at
end
end
require 'spec_helper'
require Rails.root.join('ee', 'db', 'post_migrate', '20180430180136_migrate_mirror_attributes_data_from_projects_to_import_state.rb')
require Rails.root.join('ee', 'db', 'post_migrate', '20180502130136_migrate_mirror_attributes_data_from_projects_to_import_state.rb')
describe MigrateMirrorAttributesDataFromProjectsToImportState, :migration do
let(:namespaces) { table(:namespaces) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment