Module: SchemaDev::Travis

Extended by:
Travis
Included in:
Travis
Defined in:
lib/schema_dev/travis.rb

Constant Summary collapse

TRAVIS_FILE =
".travis.yml"
CONFIG_TEMPLATES =
{
  postgresql: {
    default: {
      env: 'POSTGRESQL_DB_USER=postgres'
    },
    '10' => {
      addons: { apt: { packages: %w[postgresql-10 postgresql-client-10] } },
      env: 'POSTGRESQL_DB_USER=postgres'
    },
    '11' => {
      addons: { apt: { packages: %w[postgresql-11 postgresql-client-11] } },
      env: 'POSTGRESQL_DB_USER=travis PGPORT=5433'
    },
    '12' => {
      addons: { apt: { packages: %w[postgresql-12 postgresql-client-12] } },
      env: 'POSTGRESQL_DB_USER=travis PGPORT=5433'
    },
  }
}.freeze

Instance Method Summary collapse

Instance Method Details

#build(config) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/schema_dev/travis.rb', line 41

def build(config)
  env = []
  include = []
  addons = {}
  services = []
  skip_gemfiles = []
  if config.dbms.include?(:postgresql)
    versions = config.dbms_versions_for(:postgresql, ['9.6'])
    if config.db.count == 1
      if versions.count == 1
        # only PG and only 1 DB do it globally
        template = template_for_db(:postgresql, versions.first)
        env << template['env']
        addons.merge!(template['addons'])
      else
        # we only have one DB so we can greatly simplify our include matrix
        include.concat versions.map {|version|
          {}.merge(template_for_db(:postgresql, version))
        }
      end
    else
      # we need to include against the various gemfiles so we only use PG for PG tests (and not other DBs)
      config.matrix(db: 'postgresql').map { |entry|
        gemfile = GemfileSelector.gemfile(entry.slice(:activerecord, :db)).to_s
        skip_gemfiles << gemfile
        include.concat versions.map {|version|
          {
            "gemfile" => gemfile,
            "rvm" => entry[:ruby],
          }.merge(template_for_db(:postgresql, version))
        }
      }
    end
  end
  if config.dbms.include?(:mysql)
    if config.db.count == 1
      env << 'MYSQL_DB_USER=travis'
      services << 'mysql'
    else
      config.matrix(db: 'mysql2').map do |entry|
        gemfile = GemfileSelector.gemfile(entry.slice(:activerecord, :db)).to_s
        skip_gemfiles << gemfile
        include << {
          "gemfile" => gemfile,
          "rvm" => entry[:ruby],
          "services" => ['mysql'],
          "env" => 'MYSQL_DB_USER=travis',
        }
      end
    end
  end
  env = env.join(' ')

  gemfiles = config.matrix.map{|entry| GemfileSelector.gemfile(entry.slice(:activerecord, :db)).to_s}.uniq
  gemfiles.reject! { |gemfile| skip_gemfiles.include?(gemfile) }

  exclude = config.matrix(excluded: :only).map { |entry| {}.tap {|ex|
    ex["rvm"] = entry[:ruby]
    ex["gemfile"] = GemfileSelector.gemfile(entry.slice(:activerecord, :db)).to_s
  }}.reject{|ex| not gemfiles.include? ex["gemfile"]}

  {}.tap { |travis|
    travis["rvm"] = config.ruby.sort
    travis["gemfile"] = gemfiles.sort unless gemfiles.empty?
    travis["env"] = env unless env.empty?
    travis["addons"] = addons unless addons.empty?
    travis["services"] = services unless services.empty?
    if config.dbms.any?
      travis["before_script"] = 'bundle exec rake create_databases'
      travis["after_script"] = 'bundle exec rake drop_databases'
    end
    travis["script"] = "bundle exec rake travis"
    travis["notifications"] = { "email" => config.notify } if config.notify.any?
    travis["jobs"] = {}
    travis["jobs"]["exclude"] = exclude.sort_by{|ex| [ex["rvm"], ex["gemfile"]]} if exclude.any?
    if include.any?
      travis["jobs"]["include"] = include.sort_by{|ex| [ex.fetch('rvm', ''), ex.fetch('gemfile', '')]}
    end
    travis.delete("jobs") if travis["jobs"].empty?
  }
end

#template_for_db(db, version) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/schema_dev/travis.rb', line 32

def template_for_db(db, version)
  {
    addons: {
      db => version
    }
  }.deep_merge(CONFIG_TEMPLATES[db][version] || CONFIG_TEMPLATES[db][:default])
   .deep_stringify_keys
end

#update(config) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/schema_dev/travis.rb', line 123

def update(config)
  filepath = Pathname.new(TRAVIS_FILE)
  newtravis = build(config)
  oldtravis = YAML.load(filepath.read) rescue nil
  if oldtravis != newtravis
    header = <<-ENDYAML
# This file was auto-generated by the schema_dev tool, based on the data in
#                 ./schema_dev.yml
# Please do not edit this file; any changes will be overwritten next time
# schema_dev gets run.
ENDYAML
    filepath.write header + newtravis.to_yaml
    return true
  end
end