Class: PactBroker::DB::DataMigrations::CreateBranches

Inherits:
Object
  • Object
show all
Extended by:
Helpers
Defined in:
lib/pact_broker/db/data_migrations/create_branches.rb

Class Method Summary collapse

Methods included from Helpers

column_exists?, columns_exist?

Class Method Details

.call(connection) ⇒ Object



9
10
11
12
13
14
# File 'lib/pact_broker/db/data_migrations/create_branches.rb', line 9

def self.call connection
  if required_columns_exist?(connection)
    branch_ids = create_branch_versions(connection)
    upsert_branch_heads(connection, branch_ids)
  end
end

.create_branch_version(connection, version) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/pact_broker/db/data_migrations/create_branches.rb', line 49

def self.create_branch_version(connection, version)
  branch_values = {
    name: version[:branch],
    pacticipant_id: version[:pacticipant_id],
    created_at: version[:created_at],
    updated_at: version[:created_at]
  }
  connection[:branches].insert_ignore.insert(branch_values)
  branch_id = connection[:branches].select(:id).where(pacticipant_id: version[:pacticipant_id], name: version[:branch]).single_record[:id]

  branch_version_values = {
    pacticipant_id: version[:pacticipant_id],
    version_id: version[:id],
    version_order: version[:order],
    branch_id: branch_id,
    branch_name: version[:branch],
    created_at: version[:created_at],
    updated_at: version[:created_at]
  }

  connection[:branch_versions].insert_ignore.insert(branch_version_values)
  branch_id
end

.create_branch_versions(connection) ⇒ Object



23
24
25
26
27
# File 'lib/pact_broker/db/data_migrations/create_branches.rb', line 23

def self.create_branch_versions(connection)
  versions_without_a_branch_version(connection).collect do | version |
    create_branch_version(connection, version)
  end.uniq
end

.required_columns_exist?(connection) ⇒ Boolean

Returns:

  • (Boolean)


16
17
18
19
20
21
# File 'lib/pact_broker/db/data_migrations/create_branches.rb', line 16

def self.required_columns_exist?(connection)
  column_exists?(connection, :versions, :branch) &&
    connection.table_exists?(:branches) &&
    connection.table_exists?(:branch_versions) &&
    connection.table_exists?(:branch_heads)
end

.upsert_branch_head(connection, branch_id) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/pact_broker/db/data_migrations/create_branches.rb', line 73

def self.upsert_branch_head(connection, branch_id)
  latest_branch_version = connection[:branch_versions].where(branch_id: branch_id).order(:version_order).last

  if connection[:branch_heads].where(branch_id: branch_id).empty?
    branch_head_values = {
      pacticipant_id: latest_branch_version[:pacticipant_id],
      branch_id: branch_id,
      branch_version_id: latest_branch_version[:id],
      version_id: latest_branch_version[:version_id],
      branch_name: latest_branch_version[:branch_name]
    }
    connection[:branch_heads].insert(branch_head_values)
  else
    connection[:branch_heads]
      .where(branch_id: branch_id)
      .update(
        branch_version_id: latest_branch_version[:id],
        version_id: latest_branch_version[:version_id]
      )
  end
end

.upsert_branch_heads(connection, branch_ids) ⇒ Object



29
30
31
32
33
# File 'lib/pact_broker/db/data_migrations/create_branches.rb', line 29

def self.upsert_branch_heads(connection, branch_ids)
  branch_ids.each do | branch_id |
    upsert_branch_head(connection, branch_id)
  end
end

.versions_without_a_branch_version(connection) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/pact_broker/db/data_migrations/create_branches.rb', line 35

def self.versions_without_a_branch_version(connection)
  branch_versions_join = {
    Sequel[:versions][:id] => Sequel[:branch_versions][:version_id],
    Sequel[:branch_versions][:branch_name] => Sequel[:versions][:branch]
  }

  connection[:versions]
    .select(Sequel[:versions].*)
    .exclude(branch: nil)
    .left_outer_join(:branch_versions, branch_versions_join)
    .where(Sequel[:branch_versions][:branch_name] => nil)
    .order(:pacticipant_id, :order)
end