Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Migration checker as a CI

Shimpei Otsubo
July 05, 2018
270

Migration checker as a CI

Shimpei Otsubo

July 05, 2018
Tweet

Transcript

  1. ©2018 Wantedly, Inc. push Third Party migration checking CI? pending...

    success!!! migration? ready!! Bad ֎෦ʹ%#ΛҰ෦Ͱ΋FYQPTFͨ͘͠ͳ͍ $*͕٧·Γ΍͘͢ͳΔ
  2. ©2018 Wantedly, Inc. First Party migration checking CI? Good CI

    push pending... success!!! migration? ready!! ֎෦ʹ%#ΛFYQPTF͠ͳͯ͘ྑ͍ ΞϓϦέʔγϣϯςετΛॏ͘͠ͳ͍
  3. ©2018 Wantedly, Inc. NAME = 'wantedly/migration-checker' post '/builds' do params

    = JSON.parse request.body.read repo = params['repository']['full_name'] hash = params['head'] Octokit.create_status(repo, hash, :pending, context: NAME) state = :success desc = "Database is ready" unless SchemaMigration.pending_versions(user, repo, hash).empty? state = :failure description = "Pending migration exists: #{pendings.join(' ')}" end Octokit.create_status(repo, hash, state, context: NAME, description: desc) 'ok' end ©2018 Wantedly, Inc. IUUQTPDUPLJUHJUIVCJPPDUPLJUSC0DUPLJU$MJFOU4UBUVTFTIUNM
  4. ©2018 Wantedly, Inc. PATH = 'db/migrate' class SchemaMigration < ActiveRecord::Base

    def self.pending_versions(repo, ref) versions = Octokit.contents(slug, path: PATH, ref: ref). map { |c| c.name.split('_')[0] } key = "#{repo.upcase}_DATABASE_URL" ActiveRecord::Base.establish_connection(ENV[key]) migrated = SchemaMigration.pluck(:version) ActiveRecord::Base.connection.close versions - migrated end end ©2018 Wantedly, Inc. database=# \d schema_migrations Table "public.schema_migrations" Column | Type | Modifiers ---------+-------------------+----------- version | character varying | not null