Commit ddb0fefb authored by Andreas Brandl's avatar Andreas Brandl

Merge branch 'ab/structure-conflicts' into 'master'

Use COPY to populate schema_migrations table

Closes #212051

See merge request gitlab-org/gitlab!27885
parents 48c93b93 e94dc5ff
# frozen_string_literal: true
# Patch to use COPY in db/structure.sql when populating schema_migrations table
# This is intended to reduce potential for merge conflicts in db/structure.sql
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(Gitlab::Database::PostgresqlAdapter::SchemaVersionsCopyMixin)
This diff is collapsed.
# frozen_string_literal: true
module Gitlab
module Database
module PostgresqlAdapter
module SchemaVersionsCopyMixin
extend ActiveSupport::Concern
def dump_schema_information # :nodoc:
versions = schema_migration.all_versions
copy_versions_sql(versions) if versions.any?
end
private
def copy_versions_sql(versions)
sm_table = quote_table_name(schema_migration.table_name)
sql = +"COPY #{sm_table} (version) FROM STDIN;\n"
sql << versions.map { |v| Integer(v) }.sort.join("\n")
sql << "\n\\.\n"
sql
end
end
end
end
end
# frozen_string_literal: true
require 'spec_helper'
describe Gitlab::Database::PostgresqlAdapter::SchemaVersionsCopyMixin do
let(:schema_migration) { double('schem_migration', table_name: table_name, all_versions: versions) }
let(:versions) { %w(5 2 1000 200 4 93 2) }
let(:table_name) { "schema_migrations" }
let(:instance) do
Object.new.extend(described_class)
end
before do
allow(instance).to receive(:schema_migration).and_return(schema_migration)
allow(instance).to receive(:quote_table_name).with(table_name).and_return("\"#{table_name}\"")
end
subject { instance.dump_schema_information }
it 'uses COPY FROM STDIN' do
expect(subject.split("\n").first).to match(/COPY "schema_migrations" \(version\) FROM STDIN;/)
end
it 'contains a sorted list of versions by their numeric value' do
version_lines = subject.split("\n")[1..-2].map(&:to_i)
expect(version_lines).to eq(versions.map(&:to_i).sort)
end
it 'contains a end-of-data marker' do
expect(subject).to end_with("\\.\n")
end
context 'with non-Integer versions' do
let(:versions) { %w(5 2 4 abc) }
it 'raises an error' do
expect { subject }.to raise_error(/invalid value for Integer/)
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment