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

Migration checker as a CI

089fe44e41bb1fa2d9421f919a99173c?s=47 Shimpei Otsubo
July 05, 2018
190

Migration checker as a CI

089fe44e41bb1fa2d9421f919a99173c?s=128

Shimpei Otsubo

July 05, 2018
Tweet

Transcript

  1. ©2018 Wantedly, Inc. Migration checker as a CI ΦϨΦϨCIͷ࡞Γํ දࢀಓ.rb

    #36 5.Jul.2018 - Shimpei Otsubo - @potsbo
  2. ©2018 Wantedly, Inc. 3BJMTͷNJHSBUJPO๨ΕͰࢮ͵໰୊

  3. ©2018 Wantedly, Inc. खಈEFQMPZ͍ͯ͠Δ ECNJHSBUF͸EFQMPZ͢Δਓ͕΍Δ (JU)VCΛத৺ʹͨ͠։ൃ 8BOUFEMZͷSBJMT։ൃࣄ৘

  4. ©2018 Wantedly, Inc. ࣗಈNJHSBUJPO͸Ͱ͖ͳ͍ͱ͢Δ ͤΊͯࣗಈͰQFOEJOH͕͋Δ͔஌Γ͍ͨʂʂ NJHSBUJPO๨ΕΔ໰୊͕͋Δ 13NFSHFલʹඞͣNJHSBUJPO͢Δੈք

  5. ©2018 Wantedly, Inc. push CI? ? pending... success!!!

  6. ©2018 Wantedly, Inc. push migration checking CI? ? pending... success!!!

    migration? ready!!
  7. ©2018 Wantedly, Inc. push Third Party migration checking CI? pending...

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

    push pending... success!!! migration? ready!! ֎෦ʹ%#ΛFYQPTF͠ͳͯ͘ྑ͍ ΞϓϦέʔγϣϯςετΛॏ͘͠ͳ͍
  9. ©2018 Wantedly, Inc.

  10. ©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
  11. ©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
  12. ©2018 Wantedly, Inc. ೝূ RVFVF͢Δ QJOHड෇ ࣗಈXFCIPPLઃఆ ઃఆGJMF ΦϚέଞʹ΍Βͳ͍ͱ͍͚ͳ͍͜ͱ

  13. ©2018 Wantedly, Inc. 13NFSHFલʹඞͣNJHSBUJPO͢Δੈք