conflict_free_schema 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 65dd602a264a1ea9cfa015453ed0900eded7695b999b8e6d337c888a0b65508e
4
+ data.tar.gz: 2c6656c296c93327c1e6522354bce75bb8d167f7bcec85e73d112ef9c1c5f6b8
5
+ SHA512:
6
+ metadata.gz: 56cf7aa6bf7ffbb148d58ddb72332e0d9fc4afbd2e9b52fcdd2850a377d241f9784ab6c0e8b44f7554a0364cf3e54dea346cb2b168466e7551985b39b036906b
7
+ data.tar.gz: 935c49c912879ca8ee86aff19fdb83c55136ac82ccc2511000392aa18188e8b71c7b8b1146d7ab77ae3fbcbceb4010feaa8b2f62b028d6a7e41fc42ca6812763
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ .DS_Store
2
+ .idea/
3
+ .rspec_status
4
+ Gemfile.lock
5
+ gemfiles/*.lock
6
+ log/
7
+ structure.sql
8
+ .bundle/
data/Appraisals ADDED
@@ -0,0 +1,7 @@
1
+ appraise "rails-6.0" do
2
+ gem "rails", "6.0.3.5"
3
+ end
4
+
5
+ appraise "rails-6.1" do
6
+ gem "rails", "6.1.3"
7
+ end
data/CHANGELOG.md ADDED
@@ -0,0 +1,3 @@
1
+ ### 0.1.0
2
+
3
+ - Initial release
data/Dockerfile ADDED
@@ -0,0 +1,22 @@
1
+ FROM ruby:2.6.6-buster
2
+
3
+ ENV BUNDLE_JOBS=4 \
4
+ BUNDLE_RETRY=5
5
+
6
+ WORKDIR /app
7
+
8
+ RUN sh -c 'echo "deb http://apt.postgresql.org/pub/repos/apt buster-pgdg main" > /etc/apt/sources.list.d/pgdg.list' && \
9
+ wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | apt-key add - && \
10
+ apt-get update && apt-get install --no-install-recommends -y \
11
+ postgresql-client-12 \
12
+ && rm -rf /var/lib/apt/lists/*
13
+
14
+ COPY Gemfile Gemfile.lock conflict_free_schema.gemspec ./
15
+
16
+ ENV BUNDLE_VERSION=2.0.2
17
+
18
+ RUN gem update --system && gem install bundler:$BUNDLE_VERSION
19
+ RUN bundle install
20
+
21
+ COPY Appraisals ./
22
+ RUN bundle exec appraisal install
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in conflict-free-schema.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Rafal Wojsznis
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,45 @@
1
+ ![71wdVdp0ncL](https://user-images.githubusercontent.com/1527612/111843245-1a0d8b80-8901-11eb-987a-f382d2396f8d.jpg)
2
+
3
+ ### Why stolen ruby?
4
+
5
+ _Stolen ruby_ is an idea behind extracting open-source code into more accessible packages. I noticed that a lot of interesting projects have quite a lot of brilliant code that is unfortunately often _embedded_ into the source and never extracted - which is a shame because often it's very useful stuff and a lot of different people could benefit from it. The idea is to change that - extract those hidden gems and publish them into the world.
6
+
7
+ _Note: cover stolen [from well known book](https://austinkleon.com/steal/)._
8
+
9
+ # conflict_free_schema
10
+
11
+ ![status](https://github.com/stolen-ruby/conflict_free_schema/actions/workflows/ci.yml/badge.svg)
12
+
13
+ **Stolen from:** [GitLab](https://gitlab.com); references: [merge request](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/30109#65c74abf34b80b85fc4b56a98b029c7e2d884bac), [issue](https://gitlab.com/gitlab-org/gitlab/-/issues/218590)
14
+
15
+ Original author: Patrick Bair; extracted code with very minor modifications, added higher-level Rails integrations tests on top
16
+
17
+ ## Requirements
18
+
19
+ Rails 6+ with postgresql
20
+
21
+ ## Installation
22
+
23
+ Add the following to your `Gemfile`:
24
+
25
+ ~~~ruby
26
+ gem 'conflict_free_schema'
27
+ ~~~
28
+
29
+ Then install the gem using bundle:
30
+
31
+ ~~~bash
32
+ bundle install
33
+ ~~~
34
+
35
+ Gem will auto-hook into your rails app via `Rails::Railtie`
36
+
37
+ ## What does it do?
38
+
39
+ > Remove conflicts from structure.sql that are caused by tracking the schema_migrations.versions information. To do this, store an empty file named after each version under the db/schema_migrations directory. That way when we versions are added or removed, we add or delete files from the directory, which should not conflict with other changes.
40
+
41
+ If you're using `structure.sql` in your Rails project with postgresql - instead of putting schema version in the sql file upon dump, each migration will be stored as a separate file in `db/schema_migrations` (with a SHA hash as content so it's a unique file from git's perspective). If you're doing a lot of structure changes across your organization it should make the life of your developers much easier - resolving conflicts in structure is pain already - even without the need to resolve schema migrations version numbers.
42
+
43
+ ## Development
44
+
45
+ You can spin up docker-based development environment with provided `docker-compose` file (useful for Apple M1 users) or simply install dependencies locally.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,32 @@
1
+ Gem::Specification.new do |spec|
2
+ spec.name = "conflict_free_schema"
3
+ spec.version = "0.1.0"
4
+ spec.authors = ["Rafal Wojsznis"]
5
+ spec.email = ["wojsznis@pm.me"]
6
+
7
+ spec.summary = "Conflict-free schema version handling"
8
+ spec.description = "Remove conflicts from structure.sql that are caused by tracking the schema_migrations.versions information."
9
+ spec.homepage = "https://github.com/stolen-ruby/conflict-free-schema"
10
+ spec.license = "MIT"
11
+
12
+ spec.metadata["homepage_uri"] = spec.homepage
13
+ spec.metadata["source_code_uri"] = "https://github.com/stolen-ruby/conflict-free-schema"
14
+ spec.metadata["changelog_uri"] = "https://github.com/stolen-ruby/conflict_free_schema/blob/main/CHANGELOG.md"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(\.github|spec|docker|gemfiles|bin)/}i) }
20
+ end
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "pg"
26
+ spec.add_dependency "rails", "> 5"
27
+
28
+ spec.add_development_dependency "bundler", "~> 2.0"
29
+ spec.add_development_dependency "rake", "~> 10.0"
30
+ spec.add_development_dependency "rspec", "~> 3.0"
31
+ spec.add_development_dependency "appraisal"
32
+ end
@@ -0,0 +1,37 @@
1
+ version: "3"
2
+ services:
3
+ ruby:
4
+ build:
5
+ context: .
6
+ dockerfile: Dockerfile
7
+ image: conflict_free_schema
8
+ environment:
9
+ BOOTSNAP_CACHE_DIR: /usr/local/bundle/_bootsnap
10
+ HISTFILE: /app/log/.bash_history
11
+ PSQL_HISTFILE: /app/log/.psql_history
12
+ depends_on:
13
+ postgres:
14
+ condition: service_healthy
15
+ volumes:
16
+ - .:/app:cached
17
+ tmpfs:
18
+ - /tmp
19
+ stdin_open: true
20
+ tty: true
21
+ postgres:
22
+ image: postgres:12.5-alpine
23
+ volumes:
24
+ - postgres:/var/lib/postgresql/data
25
+ - ./log:/root/log:cached
26
+ environment:
27
+ POSTGRES_PASSWORD: postgres
28
+ POSTGRES_USER: postgres
29
+ POSTGRES_DB: conflict_free_schema
30
+ PSQL_HISTFILE: /root/log/.psql_history
31
+ healthcheck:
32
+ test: pg_isready -U postgres -h 127.0.0.1
33
+ interval: 10s
34
+ timeout: 3s
35
+
36
+ volumes:
37
+ postgres:
@@ -0,0 +1,4 @@
1
+ require_relative 'conflict_free_schema/database/postgresql_adapter/dump_schema_version_mixin'
2
+ require_relative 'conflict_free_schema/database/postgresql_database_tasks/load_schema_version_mixin'
3
+ require_relative 'conflict_free_schema/database/schema_version_files'
4
+ require_relative 'conflict_free_schema/railtie'
@@ -0,0 +1,18 @@
1
+ require 'active_support/concern'
2
+
3
+ module ConflictFreeSchema
4
+ module Database
5
+ module PostgresqlAdapter
6
+ module DumpSchemaVersionsMixin
7
+ extend ActiveSupport::Concern
8
+
9
+ def dump_schema_information # :nodoc:
10
+ versions = schema_migration.all_versions
11
+ ConflictFreeSchema::Database::SchemaVersionFiles.touch_all(versions) if versions.any?
12
+
13
+ nil
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,14 @@
1
+ module ConflictFreeSchema
2
+ module Database
3
+ module PostgresqlDatabaseTasks
4
+ module LoadSchemaVersionsMixin
5
+ extend ActiveSupport::Concern
6
+
7
+ def structure_load(*args)
8
+ super(*args)
9
+ ConflictFreeSchema::Database::SchemaVersionFiles.load_all
10
+ end
11
+ end
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,72 @@
1
+ require 'tmpdir'
2
+ require 'digest'
3
+ require 'rails'
4
+
5
+ module ConflictFreeSchema
6
+ module Database
7
+ class SchemaVersionFiles
8
+ SCHEMA_DIRECTORY = 'db/schema_migrations'
9
+ MIGRATION_DIRECTORIES = %w[db/migrate db/post_migrate].freeze
10
+ MIGRATION_VERSION_GLOB = '20[0-9][0-9]*'
11
+
12
+ def self.touch_all(versions_from_database)
13
+ init_schema_directory
14
+ versions_from_migration_files = find_versions_from_migration_files
15
+
16
+ version_filepaths = find_version_filenames.map { |f| schema_directory.join(f) }
17
+ FileUtils.rm(version_filepaths)
18
+
19
+ versions_to_create = versions_from_database & versions_from_migration_files
20
+ versions_to_create.each do |version|
21
+ version_filepath = schema_directory.join(version)
22
+
23
+ File.open(version_filepath, 'w') do |file|
24
+ file << Digest::SHA256.hexdigest(version)
25
+ end
26
+ end
27
+ end
28
+
29
+ def self.load_all
30
+ init_schema_directory
31
+ version_filenames = find_version_filenames
32
+ return if version_filenames.empty?
33
+
34
+ values = version_filenames.map { |vf| "('#{connection.quote_string(vf)}')" }
35
+ connection.execute(<<~SQL)
36
+ INSERT INTO schema_migrations (version)
37
+ VALUES #{values.join(',')}
38
+ ON CONFLICT DO NOTHING
39
+ SQL
40
+ end
41
+
42
+ def self.schema_directory
43
+ @schema_directory ||= Rails.root.join(SCHEMA_DIRECTORY)
44
+ end
45
+
46
+ def self.migration_directories
47
+ @migration_directories ||= MIGRATION_DIRECTORIES.map { |dir| Rails.root.join(dir) }
48
+ end
49
+
50
+ def self.find_version_filenames
51
+ Dir.glob(MIGRATION_VERSION_GLOB, base: schema_directory)
52
+ end
53
+
54
+ def self.init_schema_directory
55
+ FileUtils.mkdir_p(schema_directory)
56
+ end
57
+
58
+ def self.find_versions_from_migration_files
59
+ migration_directories.each_with_object([]) do |directory, migration_versions|
60
+ directory_migrations = Dir.glob(MIGRATION_VERSION_GLOB, base: directory)
61
+ directory_versions = directory_migrations.map! { |m| m.split('_').first }
62
+
63
+ migration_versions.concat(directory_versions)
64
+ end
65
+ end
66
+
67
+ def self.connection
68
+ ActiveRecord::Base.connection
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,15 @@
1
+ require 'rails/railtie'
2
+
3
+ module ConflictFreeSchema
4
+ class Railtie < Rails::Railtie
5
+ initializer "conflict_free_schema.extend_active_record" do
6
+ require 'active_record/connection_adapters/postgresql_adapter'
7
+
8
+ # Patch to write version information as empty files under the db/schema_migrations directory
9
+ # This is intended to reduce potential for merge conflicts in db/structure.sql
10
+ ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(ConflictFreeSchema::Database::PostgresqlAdapter::DumpSchemaVersionsMixin)
11
+ # Patch to load version information from empty files under the db/schema_migrations directory
12
+ ActiveRecord::Tasks::PostgreSQLDatabaseTasks.prepend(ConflictFreeSchema::Database::PostgresqlDatabaseTasks::LoadSchemaVersionsMixin)
13
+ end
14
+ end
15
+ end
metadata ADDED
@@ -0,0 +1,146 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: conflict_free_schema
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Rafal Wojsznis
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-03-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: pg
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">"
32
+ - !ruby/object:Gem::Version
33
+ version: '5'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">"
39
+ - !ruby/object:Gem::Version
40
+ version: '5'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: appraisal
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Remove conflicts from structure.sql that are caused by tracking the schema_migrations.versions
98
+ information.
99
+ email:
100
+ - wojsznis@pm.me
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - Appraisals
107
+ - CHANGELOG.md
108
+ - Dockerfile
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - conflict_free_schema.gemspec
114
+ - docker-compose.yml
115
+ - lib/conflict_free_schema.rb
116
+ - lib/conflict_free_schema/database/postgresql_adapter/dump_schema_version_mixin.rb
117
+ - lib/conflict_free_schema/database/postgresql_database_tasks/load_schema_version_mixin.rb
118
+ - lib/conflict_free_schema/database/schema_version_files.rb
119
+ - lib/conflict_free_schema/railtie.rb
120
+ homepage: https://github.com/stolen-ruby/conflict-free-schema
121
+ licenses:
122
+ - MIT
123
+ metadata:
124
+ homepage_uri: https://github.com/stolen-ruby/conflict-free-schema
125
+ source_code_uri: https://github.com/stolen-ruby/conflict-free-schema
126
+ changelog_uri: https://github.com/stolen-ruby/conflict_free_schema/blob/main/CHANGELOG.md
127
+ post_install_message:
128
+ rdoc_options: []
129
+ require_paths:
130
+ - lib
131
+ required_ruby_version: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - ">="
134
+ - !ruby/object:Gem::Version
135
+ version: '0'
136
+ required_rubygems_version: !ruby/object:Gem::Requirement
137
+ requirements:
138
+ - - ">="
139
+ - !ruby/object:Gem::Version
140
+ version: '0'
141
+ requirements: []
142
+ rubygems_version: 3.0.3
143
+ signing_key:
144
+ specification_version: 4
145
+ summary: Conflict-free schema version handling
146
+ test_files: []