diff options
author | Kestred <kestred@riotcave.com> | 2014-08-23 22:27:52 -0700 |
---|---|---|
committer | Kestred <kestred@riotcave.com> | 2014-08-23 22:27:52 -0700 |
commit | 18f954b31c242822b6c7430a50ca99a93adc6ea6 (patch) | |
tree | b7ee4acac738d5b64b6fcfaf909756f784dd2d61 /db | |
parent | 2eb3d91e1a6e51d3ca0be2fe0a5df750708b366d (diff) | |
download | gitlab-ci-18f954b31c242822b6c7430a50ca99a93adc6ea6.tar.gz |
Separate Commit model and logic from Build model|etc...
This is an entirely non-user facing change which prepares GitLab CI for future support of Parallel Builds.
See https://about.gitlab.com/2013/12/19/gitlab-ci-with-parallel-builds-and-deployments/.
These changes specifically avoid changing the supported API or changing any of the website views.
Changes to the website views will come in tandem with future features like "Multiple build scripts".
The supported API won't change as part of any future changes on this vein, to maintain support for the unofficial GitLab CI runners.
This closes the following implementation step:
1. A commit has many builds
Signed-off-by: Kestred <kestred@riotcave.com>
Diffstat (limited to 'db')
-rw-r--r-- | db/migrate/20140823225019_create_commits_from_builds.rb | 80 | ||||
-rw-r--r-- | db/schema.rb | 19 |
2 files changed, 92 insertions, 7 deletions
diff --git a/db/migrate/20140823225019_create_commits_from_builds.rb b/db/migrate/20140823225019_create_commits_from_builds.rb new file mode 100644 index 0000000..97252a0 --- /dev/null +++ b/db/migrate/20140823225019_create_commits_from_builds.rb @@ -0,0 +1,80 @@ +class CreateCommitsFromBuilds < ActiveRecord::Migration + def change + create_table :commits do |t| + t.integer :project_id + t.string :ref, nil: false + t.string :sha, nil: false + t.string :before_sha, nil: false + t.text :push_data, nil: false + + t.timestamps + end + + reversible do |migration| + migration.up { init_commits } + migration.down {} + end + + add_column :builds, :commit_id, :integer + + reversible do |migration| + migration.up { associate_builds_with_commit } + migration.down { dissociate_builds_with_commit } + end + + # Remove commit data from builds + # -- don't use change_table, its not very reversible + remove_column :builds, :project_id, :integer + remove_column :builds, :ref, :string + remove_column :builds, :sha, :string + remove_column :builds, :before_sha, :string + remove_column :builds, :push_data, :text + end + + private + + def init_commits + Commit.reset_column_information + + # Create one Commit for each unique sha value + shas = Build.pluck(:sha).uniq + shas.each do |sha| + # Get latest build for a particular commit + build = Build.where(sha: sha).order('created_at desc').first + attributes = { + project_id: build.project_id, + ref: build.ref, + sha: build.sha, + before_sha: build.before_sha, + push_data: build.push_data, + # Original commit status matches the latest build status + status: build.status + } + + Commit.create!(attributes) + end + end + + def associate_builds_with_commit + Build.reset_column_information + Build.find_each do |build| + build.commit_id = Commit.find_by_sha(build.sha).id + build.save! + end + end + + def dissociate_builds_with_commit + Build.reset_column_information + Build.find_each do |build| + # Don't assume an assocation exists + commit = Commit.find(build.commit_id) + + build.project_id = commit.project_id + build.ref = commit.ref + build.sha = commit.sha + build.before_sha = commit.before_sha + build.push_data = commit.push_data + build.save! + end + end +end diff --git a/db/schema.rb b/db/schema.rb index dcb3569..a3edfda 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,30 +11,35 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140506091853) do +ActiveRecord::Schema.define(version: 20140823225019) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "builds", force: true do |t| - t.integer "project_id" - t.string "ref" t.string "status" t.datetime "finished_at" t.text "trace" t.datetime "created_at" t.datetime "updated_at" - t.string "sha" t.datetime "started_at" t.string "tmp_file" - t.string "before_sha" - t.text "push_data" t.integer "runner_id" + t.integer "commit_id" end - add_index "builds", ["project_id"], name: "index_builds_on_project_id", using: :btree add_index "builds", ["runner_id"], name: "index_builds_on_runner_id", using: :btree + create_table "commits", force: true do |t| + t.integer "project_id" + t.string "ref" + t.string "sha" + t.string "before_sha" + t.text "push_data" + t.datetime "created_at" + t.datetime "updated_at" + end + create_table "projects", force: true do |t| t.string "name", null: false t.integer "timeout", default: 1800, null: false |