sequel_migrations_toys 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +17 -0
- data/LICENSE.txt +21 -0
- data/README.md +68 -0
- data/lib/sequel_migrations_toys/template/_base.rb +47 -0
- data/lib/sequel_migrations_toys/template/_migration_file.rb +196 -0
- data/lib/sequel_migrations_toys/template/check.rb +34 -0
- data/lib/sequel_migrations_toys/template/create/_base.rb +33 -0
- data/lib/sequel_migrations_toys/template/create/regular.rb +22 -0
- data/lib/sequel_migrations_toys/template/create.rb +26 -0
- data/lib/sequel_migrations_toys/template/disable.rb +21 -0
- data/lib/sequel_migrations_toys/template/enable.rb +21 -0
- data/lib/sequel_migrations_toys/template/list.rb +21 -0
- data/lib/sequel_migrations_toys/template/reversion.rb +21 -0
- data/lib/sequel_migrations_toys/template/rollback.rb +34 -0
- data/lib/sequel_migrations_toys/template/run.rb +70 -0
- data/lib/sequel_migrations_toys/template.rb +40 -0
- data/lib/sequel_migrations_toys/version.rb +5 -0
- data/lib/sequel_migrations_toys.rb +4 -0
- metadata +248 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 83fa357138765e4330e1cfd34d6256e443713a5ee3250bef3ac031e4953f06bb
|
4
|
+
data.tar.gz: f5877a3207f55059fada114765a898d45c61ed7027e5e6399aa0e32b9b6bf04f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: '0192a4596ebd537cd014a02ef6295f2590a69a1dd39a3143e1096ec8141c3cdfa02479e25ddec512f3c8cb5c1ccd487b9eb3c56d85e454bc2bff2f0734910bd0'
|
7
|
+
data.tar.gz: 1f8dbc63e493f9b3912bd94462a4d5de3c37469853ccbcd7308b3586f55145f3b37e657b7538415d18901cb49f46dfb4d4c3ae94ae6e43a92d71c97a234ebb65
|
data/CHANGELOG.md
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2020 Alexander Popov
|
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,68 @@
|
|
1
|
+
# Sequel Migrations Toys
|
2
|
+
|
3
|
+
[![Cirrus CI - Base Branch Build Status](https://img.shields.io/cirrus/github/AlexWayfer/sequel_migrations_toys?style=flat-square)](https://cirrus-ci.com/github/AlexWayfer/sequel_migrations_toys)
|
4
|
+
[![Codecov branch](https://img.shields.io/codecov/c/github/AlexWayfer/sequel_migrations_toys/master.svg?style=flat-square)](https://codecov.io/gh/AlexWayfer/sequel_migrations_toys)
|
5
|
+
[![Code Climate](https://img.shields.io/codeclimate/maintainability/AlexWayfer/sequel_migrations_toys.svg?style=flat-square)](https://codeclimate.com/github/AlexWayfer/sequel_migrations_toys)
|
6
|
+
[![Depfu](https://img.shields.io/depfu/AlexWayfer/sequel_migrations_toys?style=flat-square)](https://depfu.com/repos/github/AlexWayfer/sequel_migrations_toys)
|
7
|
+
[![Inline docs](https://inch-ci.org/github/AlexWayfer/sequel_migrations_toys.svg?branch=master)](https://inch-ci.org/github/AlexWayfer/sequel_migrations_toys)
|
8
|
+
[![license](https://img.shields.io/github/license/AlexWayfer/sequel_migrations_toys.svg?style=flat-square)](https://github.com/AlexWayfer/sequel_migrations_toys/blob/master/LICENSE.txt)
|
9
|
+
[![Gem](https://img.shields.io/gem/v/sequel_migrations_toys.svg?style=flat-square)](https://rubygems.org/gems/sequel_migrations_toys)
|
10
|
+
|
11
|
+
Toys template for [Sequel](https://sequel.jeremyevans.net/) migrations.
|
12
|
+
|
13
|
+
## Installation
|
14
|
+
|
15
|
+
Add this line to your application's Gemfile:
|
16
|
+
|
17
|
+
```ruby
|
18
|
+
gem 'sequel_migrations_toys'
|
19
|
+
```
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
```shell
|
24
|
+
bundle install
|
25
|
+
```
|
26
|
+
|
27
|
+
Or install it yourself as:
|
28
|
+
|
29
|
+
```shell
|
30
|
+
gem install sequel_migrations_toys
|
31
|
+
```
|
32
|
+
|
33
|
+
## Usage
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
require 'sequel_migrations_toys'
|
37
|
+
expand SequelMigrationsToys::Template,
|
38
|
+
# optional, has such default
|
39
|
+
db_migrations_dir: 'db/migrations',
|
40
|
+
db_connection_proc: (proc do
|
41
|
+
require "#{context_directory}/config/main"
|
42
|
+
MyProject::Application.db_connection
|
43
|
+
end)
|
44
|
+
|
45
|
+
# `database` namespace created
|
46
|
+
# aliases are optional, but handful
|
47
|
+
alias_tool :db, :database
|
48
|
+
```
|
49
|
+
|
50
|
+
## Development
|
51
|
+
|
52
|
+
After checking out the repo, run `bundle install` to install dependencies.
|
53
|
+
|
54
|
+
Then, run `toys rspec` to run the tests.
|
55
|
+
|
56
|
+
To install this gem onto your local machine, run `toys gem install`.
|
57
|
+
|
58
|
+
To release a new version, run `toys gem release %version%`.
|
59
|
+
See how it works [here](https://github.com/AlexWayfer/gem_toys#release).
|
60
|
+
|
61
|
+
## Contributing
|
62
|
+
|
63
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/AlexWayfer/sequel_migrations_toys).
|
64
|
+
|
65
|
+
## License
|
66
|
+
|
67
|
+
The gem is available as open source under the terms of the
|
68
|
+
[MIT License](https://opensource.org/licenses/MIT).
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
## Base class for templates
|
6
|
+
class Base
|
7
|
+
include Toys::Template
|
8
|
+
|
9
|
+
attr_reader :db_migrations_dir, :db_connection_proc
|
10
|
+
|
11
|
+
def initialize(db_migrations_dir:, db_connection_proc:)
|
12
|
+
@db_migrations_dir = db_migrations_dir
|
13
|
+
@db_connection_proc = db_connection_proc
|
14
|
+
end
|
15
|
+
|
16
|
+
def db_connection
|
17
|
+
return @db_connection if defined? @db_connection
|
18
|
+
|
19
|
+
@db_connection = db_connection_proc.call
|
20
|
+
|
21
|
+
# @db_connection.loggers << Logger.new($stdout)
|
22
|
+
|
23
|
+
@db_connection
|
24
|
+
end
|
25
|
+
|
26
|
+
# private
|
27
|
+
|
28
|
+
## Module with common code for migrations
|
29
|
+
CommonMigrationsCode = Module.new do
|
30
|
+
private
|
31
|
+
|
32
|
+
def migration_file_class(db_migrations_dir, db_connection = nil)
|
33
|
+
return @migration_file_class if defined? @migration_file_class
|
34
|
+
|
35
|
+
require "#{__dir__}/_migration_file"
|
36
|
+
|
37
|
+
context_directory = self.context_directory
|
38
|
+
@migration_file_class = Class.new(MigrationFile) do
|
39
|
+
self.root_dir = File.expand_path context_directory
|
40
|
+
self.db_migrations_dir = File.expand_path db_migrations_dir
|
41
|
+
self.db_connection = db_connection
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,196 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'memery'
|
4
|
+
require 'paint'
|
5
|
+
require 'time'
|
6
|
+
|
7
|
+
module SequelMigrationsToys
|
8
|
+
## Migration file
|
9
|
+
class MigrationFile # rubocop:disable Metrics/ClassLength
|
10
|
+
CONTENT = proc do |content|
|
11
|
+
content ||= +<<~DEFAULT
|
12
|
+
change do
|
13
|
+
end
|
14
|
+
DEFAULT
|
15
|
+
|
16
|
+
## /^(?!$)/ - searches for a position that starts at the
|
17
|
+
## "start of line" position and
|
18
|
+
## is not followed by the "end of line" position
|
19
|
+
|
20
|
+
<<~STR
|
21
|
+
# frozen_string_literal: true
|
22
|
+
|
23
|
+
Sequel.migration do
|
24
|
+
#{content.gsub!(/^(?!$)/, "\t").strip!}
|
25
|
+
end
|
26
|
+
STR
|
27
|
+
end
|
28
|
+
|
29
|
+
DISABLING_EXT = '.bak'
|
30
|
+
|
31
|
+
class << self
|
32
|
+
include Memery
|
33
|
+
|
34
|
+
attr_accessor :root_dir, :db_migrations_dir, :db_connection
|
35
|
+
|
36
|
+
memoize def database_schema_migrations
|
37
|
+
db_connection[:schema_migrations].select_map(:filename)
|
38
|
+
end
|
39
|
+
|
40
|
+
def find_all(query, enabled: true, disabled: true)
|
41
|
+
filenames = Dir["#{db_migrations_dir}/*#{query}*"].select { |filename| File.file? filename }
|
42
|
+
files = filenames.map { |filename| new filename: filename }.sort!
|
43
|
+
files.reject!(&:disabled) unless disabled
|
44
|
+
files.select!(&:disabled) unless enabled
|
45
|
+
files
|
46
|
+
end
|
47
|
+
|
48
|
+
def find_one(query, **options)
|
49
|
+
all = find_all query, **options
|
50
|
+
|
51
|
+
return all.first if all.size < 2
|
52
|
+
|
53
|
+
raise 'More than one file mathes the query'
|
54
|
+
end
|
55
|
+
|
56
|
+
memoize def applied
|
57
|
+
database_schema_migrations.map { |one| new filename: one }
|
58
|
+
end
|
59
|
+
|
60
|
+
memoize def existing
|
61
|
+
find_all '*', disabled: false
|
62
|
+
end
|
63
|
+
|
64
|
+
memoize def applied_not_existing
|
65
|
+
existing_names = existing.map(&:basename)
|
66
|
+
|
67
|
+
applied.reject do |one|
|
68
|
+
existing_names.include? one.basename
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
memoize def existing_not_applied
|
73
|
+
existing.reject do |one|
|
74
|
+
database_schema_migrations.include? one.basename
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
attr_accessor :version
|
80
|
+
attr_reader :name, :disabled
|
81
|
+
|
82
|
+
def initialize(filename: nil, name: nil, content: nil)
|
83
|
+
self.filename = filename
|
84
|
+
self.name = name if name
|
85
|
+
@content = content
|
86
|
+
end
|
87
|
+
|
88
|
+
## Accessors
|
89
|
+
|
90
|
+
def basename
|
91
|
+
File.basename(@filename)
|
92
|
+
end
|
93
|
+
|
94
|
+
def filename=(value)
|
95
|
+
parse_filename value if value.is_a? String
|
96
|
+
@filename = value
|
97
|
+
end
|
98
|
+
|
99
|
+
def name=(value)
|
100
|
+
@name = value.tr(' ', '_').downcase
|
101
|
+
end
|
102
|
+
|
103
|
+
def disabled=(value)
|
104
|
+
@disabled =
|
105
|
+
case value
|
106
|
+
when String
|
107
|
+
[DISABLING_EXT, DISABLING_EXT[1..-1]].include? value
|
108
|
+
else
|
109
|
+
value
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
113
|
+
def <=>(other)
|
114
|
+
version <=> other.version
|
115
|
+
end
|
116
|
+
|
117
|
+
def applied?
|
118
|
+
self.class.database_schema_migrations.include?(basename)
|
119
|
+
end
|
120
|
+
|
121
|
+
## Behavior
|
122
|
+
|
123
|
+
def print
|
124
|
+
datetime = Time.parse(version).strftime('%F %R')
|
125
|
+
|
126
|
+
puts [
|
127
|
+
Paint["[#{version}]", :white],
|
128
|
+
Paint[datetime, disabled ? :white : :cyan],
|
129
|
+
Paint[fullname, disabled ? :white : :default],
|
130
|
+
(Paint['(not applied)', :red] unless applied?)
|
131
|
+
].join(' ')
|
132
|
+
end
|
133
|
+
|
134
|
+
def generate
|
135
|
+
self.version = new_version
|
136
|
+
FileUtils.mkdir_p File.dirname new_filename
|
137
|
+
File.write new_filename, CONTENT.call(@content)
|
138
|
+
puts "Migration #{relative_filename} created."
|
139
|
+
end
|
140
|
+
|
141
|
+
def reversion
|
142
|
+
rename version: new_version
|
143
|
+
end
|
144
|
+
|
145
|
+
def disable
|
146
|
+
abort 'Migration already disabled' if disabled
|
147
|
+
|
148
|
+
rename disabled: true
|
149
|
+
|
150
|
+
puts "Migration #{relative_filename} disabled."
|
151
|
+
end
|
152
|
+
|
153
|
+
def enable
|
154
|
+
abort 'Migration already enabled' unless disabled
|
155
|
+
|
156
|
+
rename disabled: false
|
157
|
+
|
158
|
+
puts "Migration #{relative_filename} enabled."
|
159
|
+
end
|
160
|
+
|
161
|
+
private
|
162
|
+
|
163
|
+
def fullname
|
164
|
+
result = name.tr('_', ' ').capitalize
|
165
|
+
disabled ? "- #{result} (disabled)" : result
|
166
|
+
end
|
167
|
+
|
168
|
+
def parse_filename(value = @filename)
|
169
|
+
basename = File.basename value
|
170
|
+
self.version, parts = basename.split('_', 2)
|
171
|
+
self.name, _ext, self.disabled = parts.split('.')
|
172
|
+
end
|
173
|
+
|
174
|
+
def new_version
|
175
|
+
Time.now.strftime('%Y%m%d%H%M%S')
|
176
|
+
end
|
177
|
+
|
178
|
+
def rename(vars = {})
|
179
|
+
vars.each { |key, value| send :"#{key}=", value }
|
180
|
+
|
181
|
+
return unless @filename.is_a? String
|
182
|
+
|
183
|
+
File.rename @filename, new_filename
|
184
|
+
self.filename = new_filename
|
185
|
+
end
|
186
|
+
|
187
|
+
def new_filename
|
188
|
+
new_basename = "#{version}_#{name}.rb#{DISABLING_EXT if disabled}"
|
189
|
+
"#{self.class.db_migrations_dir}/#{new_basename}"
|
190
|
+
end
|
191
|
+
|
192
|
+
def relative_filename
|
193
|
+
new_filename.gsub("#{self.class.root_dir}/", '')
|
194
|
+
end
|
195
|
+
end
|
196
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
## Define toys for migrations check
|
6
|
+
class Check < Base
|
7
|
+
on_expand do |template|
|
8
|
+
tool :check do
|
9
|
+
desc 'Check applied migrations'
|
10
|
+
|
11
|
+
to_run do
|
12
|
+
@template = template
|
13
|
+
|
14
|
+
migration_file_class = migration_file_class(
|
15
|
+
@template.db_migrations_dir, @template.db_connection
|
16
|
+
)
|
17
|
+
|
18
|
+
if migration_file_class.applied_not_existing.any?
|
19
|
+
puts 'Applied, but not existing'
|
20
|
+
migration_file_class.applied_not_existing.each(&:print)
|
21
|
+
|
22
|
+
puts "\n" if migration_file_class.existing_not_applied.any?
|
23
|
+
end
|
24
|
+
|
25
|
+
return if migration_file_class.existing_not_applied.empty?
|
26
|
+
|
27
|
+
puts 'Existing, but not applied'
|
28
|
+
migration_file_class.existing_not_applied.each(&:print)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,33 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
class Create
|
6
|
+
## Base class for Sequel migrations creation toys
|
7
|
+
class Base < Template::Base
|
8
|
+
include Toys::Template
|
9
|
+
|
10
|
+
# private
|
11
|
+
|
12
|
+
## Common code for migrations creation
|
13
|
+
module CommonMigrationsCreateCode
|
14
|
+
private
|
15
|
+
|
16
|
+
def create_migration_file(db_migrations_dir, name, content = nil)
|
17
|
+
file = migration_file_class(db_migrations_dir).new(name: name, content: content)
|
18
|
+
|
19
|
+
file.generate
|
20
|
+
end
|
21
|
+
|
22
|
+
def render_template(filename)
|
23
|
+
require 'erb'
|
24
|
+
filename = "#{db_migrations_dir}/templates/#{filename}.rb.erb"
|
25
|
+
renderer = ERB.new File.read filename
|
26
|
+
renderer.filename = filename
|
27
|
+
renderer.result binding
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
class Create
|
6
|
+
## Define toys for Sequel migrations creation
|
7
|
+
class Regular < Create::Base
|
8
|
+
on_expand do |template|
|
9
|
+
tool :regular do
|
10
|
+
desc 'Create regular migration'
|
11
|
+
|
12
|
+
required_arg :name
|
13
|
+
|
14
|
+
to_run do
|
15
|
+
create_migration_file template.db_migrations_dir, name
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
## Define toys for Sequel migrations creation
|
6
|
+
class Create < Template::Base
|
7
|
+
on_expand do |template|
|
8
|
+
tool :create do
|
9
|
+
require_relative 'create/_base'
|
10
|
+
|
11
|
+
subtool_apply do
|
12
|
+
include Create::Base::CommonMigrationsCreateCode
|
13
|
+
end
|
14
|
+
|
15
|
+
require_relative 'create/regular'
|
16
|
+
expand Create::Regular,
|
17
|
+
db_migrations_dir: template.db_migrations_dir,
|
18
|
+
db_connection_proc: template.db_connection_proc
|
19
|
+
end
|
20
|
+
|
21
|
+
alias_tool :create, 'create:regular'
|
22
|
+
alias_tool :new, :create
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
## Define toys for migrations disable
|
6
|
+
class Disable < Base
|
7
|
+
on_expand do |template|
|
8
|
+
tool :disable do
|
9
|
+
desc 'Disable migration'
|
10
|
+
|
11
|
+
required_arg :filename
|
12
|
+
|
13
|
+
to_run do
|
14
|
+
file = migration_file_class(template.db_migrations_dir).find_one filename
|
15
|
+
file.disable
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
## Define toys for migrations enable
|
6
|
+
class Enable < Base
|
7
|
+
on_expand do |template|
|
8
|
+
tool :enable do
|
9
|
+
desc 'Enable migration'
|
10
|
+
|
11
|
+
required_arg :filename
|
12
|
+
|
13
|
+
to_run do
|
14
|
+
file = migration_file_class(template.db_migrations_dir).find_one filename
|
15
|
+
file.enable
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
## Define toys for migrations list
|
6
|
+
class List < Base
|
7
|
+
on_expand do |template|
|
8
|
+
tool :list do
|
9
|
+
desc 'Show all migrations'
|
10
|
+
|
11
|
+
to_run do
|
12
|
+
files =
|
13
|
+
migration_file_class(template.db_migrations_dir, template.db_connection)
|
14
|
+
.find_all '*'
|
15
|
+
files.each(&:print)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
## Define toys for migrations reversion
|
6
|
+
class Reversion < Base
|
7
|
+
on_expand do |template|
|
8
|
+
tool :reversion do
|
9
|
+
desc 'Change version of migration to latest'
|
10
|
+
|
11
|
+
required_arg :filename
|
12
|
+
|
13
|
+
to_run do
|
14
|
+
file = migration_file_class(template.db_migrations_dir).find_one filename
|
15
|
+
file.reversion
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
## Define toys for rollback migrations
|
6
|
+
class Rollback < Base
|
7
|
+
on_expand do |template|
|
8
|
+
tool :rollback do
|
9
|
+
include :exec, exit_on_nonzero_status: true
|
10
|
+
|
11
|
+
desc 'Rollback the database N steps'
|
12
|
+
|
13
|
+
optional_arg :step, accept: Integer, default: 1
|
14
|
+
|
15
|
+
to_run do
|
16
|
+
@template = template
|
17
|
+
|
18
|
+
files = migration_file_class(@template.db_migrations_dir).find_all '*'
|
19
|
+
file = files[-1 - step.abs]
|
20
|
+
|
21
|
+
target = file ? file.version : 0
|
22
|
+
|
23
|
+
## https://github.com/dazuma/toys/issues/33
|
24
|
+
exec_tool ['database:migrations:run', "--target=#{target}"]
|
25
|
+
|
26
|
+
target_string = file ? file.basename : 'empty database.'
|
27
|
+
|
28
|
+
puts "Rolled back to #{target_string}"
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,70 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SequelMigrationsToys
|
4
|
+
class Template
|
5
|
+
## Define toys for run migrations
|
6
|
+
class Run < Base
|
7
|
+
on_expand do |template|
|
8
|
+
tool :run do
|
9
|
+
include :exec, exit_on_nonzero_status: true
|
10
|
+
|
11
|
+
desc 'Run migrations'
|
12
|
+
|
13
|
+
flag :target, '-t', '--target=VERSION'
|
14
|
+
flag :current, '-c', '--current=VERSION', default: 'current'
|
15
|
+
flag :force, '-f', '--force', desc: 'Allow missing migration files'
|
16
|
+
|
17
|
+
SEQUEL_EXTENSIONS = %i[migration inflector].freeze
|
18
|
+
|
19
|
+
to_run do
|
20
|
+
@template = template
|
21
|
+
|
22
|
+
## PSQL tools or analog are required
|
23
|
+
exec_tool 'database:dump' unless ENV['SKIP_DB_DUMP']
|
24
|
+
|
25
|
+
## https://github.com/jeremyevans/sequel/issues/1182#issuecomment-217696754
|
26
|
+
require 'sequel'
|
27
|
+
SEQUEL_EXTENSIONS.each { |extension| Sequel.extension extension }
|
28
|
+
|
29
|
+
Sequel::Migrator.run(
|
30
|
+
@template.db_connection, @template.db_migrations_dir, options
|
31
|
+
)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def options
|
37
|
+
result = { allow_missing_migration_files: force }
|
38
|
+
|
39
|
+
return result.merge! target_options if target
|
40
|
+
|
41
|
+
puts 'Migrating to latest'
|
42
|
+
result
|
43
|
+
end
|
44
|
+
|
45
|
+
def target_options
|
46
|
+
target_version =
|
47
|
+
if target == '0'
|
48
|
+
puts 'Migrating all the way down'
|
49
|
+
target
|
50
|
+
else
|
51
|
+
find_target_file_version
|
52
|
+
end
|
53
|
+
|
54
|
+
{ current: current.to_i, target: target_version.to_i }
|
55
|
+
end
|
56
|
+
|
57
|
+
def find_target_file_version
|
58
|
+
file =
|
59
|
+
migration_file_class(@template.db_migrations_dir).find_one target, disabled: false
|
60
|
+
|
61
|
+
abort 'Migration with this version not found' if file.nil?
|
62
|
+
|
63
|
+
puts "Migrating from #{current} to #{file.basename}"
|
64
|
+
file.version
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'toys-core'
|
4
|
+
|
5
|
+
module SequelMigrationsToys
|
6
|
+
## Define toys for Sequel migrations
|
7
|
+
class Template
|
8
|
+
include Toys::Template
|
9
|
+
|
10
|
+
attr_reader :db_migrations_dir, :db_connection_proc
|
11
|
+
|
12
|
+
def initialize(db_migrations_dir: 'db/migrations', db_connection_proc:)
|
13
|
+
@db_migrations_dir = db_migrations_dir
|
14
|
+
@db_connection_proc = db_connection_proc
|
15
|
+
end
|
16
|
+
|
17
|
+
on_expand do |template|
|
18
|
+
tool :database do
|
19
|
+
tool :migrations do
|
20
|
+
require_relative 'template/_base'
|
21
|
+
|
22
|
+
subtool_apply do
|
23
|
+
include Base::CommonMigrationsCode
|
24
|
+
end
|
25
|
+
|
26
|
+
%w[Create List Check Enable Disable Reversion Run Rollback]
|
27
|
+
.each do |template_name|
|
28
|
+
require_relative "template/#{template_name.downcase}"
|
29
|
+
expand Template.const_get(template_name, false),
|
30
|
+
db_migrations_dir: template.db_migrations_dir,
|
31
|
+
db_connection_proc: template.db_connection_proc
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
alias_tool :migrate, 'migrations:run'
|
36
|
+
alias_tool :migrations, 'migrations:list'
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,248 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: sequel_migrations_toys
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.2
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alexander Popov
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-09-14 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: alt_memery
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: sequel
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: toys-core
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.11.0
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.11.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: pry-byebug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '3.9'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '3.9'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '2.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '2.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: gem_toys
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: 0.3.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.3.0
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: toys
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: 0.11.0
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: 0.11.0
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: codecov
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: 0.2.0
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: 0.2.0
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: rspec
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '3.9'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '3.9'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: simplecov
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: 0.19.0
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: 0.19.0
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: rubocop
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: 0.89.1
|
160
|
+
type: :development
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: 0.89.1
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: rubocop-performance
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - "~>"
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '1.0'
|
174
|
+
type: :development
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - "~>"
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '1.0'
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: rubocop-rspec
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - "~>"
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '1.0'
|
188
|
+
type: :development
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - "~>"
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '1.0'
|
195
|
+
description: 'Toys template for Sequel migrations.
|
196
|
+
|
197
|
+
'
|
198
|
+
email:
|
199
|
+
- alex.wayfer@gmail.com
|
200
|
+
executables: []
|
201
|
+
extensions: []
|
202
|
+
extra_rdoc_files: []
|
203
|
+
files:
|
204
|
+
- CHANGELOG.md
|
205
|
+
- LICENSE.txt
|
206
|
+
- README.md
|
207
|
+
- lib/sequel_migrations_toys.rb
|
208
|
+
- lib/sequel_migrations_toys/template.rb
|
209
|
+
- lib/sequel_migrations_toys/template/_base.rb
|
210
|
+
- lib/sequel_migrations_toys/template/_migration_file.rb
|
211
|
+
- lib/sequel_migrations_toys/template/check.rb
|
212
|
+
- lib/sequel_migrations_toys/template/create.rb
|
213
|
+
- lib/sequel_migrations_toys/template/create/_base.rb
|
214
|
+
- lib/sequel_migrations_toys/template/create/regular.rb
|
215
|
+
- lib/sequel_migrations_toys/template/disable.rb
|
216
|
+
- lib/sequel_migrations_toys/template/enable.rb
|
217
|
+
- lib/sequel_migrations_toys/template/list.rb
|
218
|
+
- lib/sequel_migrations_toys/template/reversion.rb
|
219
|
+
- lib/sequel_migrations_toys/template/rollback.rb
|
220
|
+
- lib/sequel_migrations_toys/template/run.rb
|
221
|
+
- lib/sequel_migrations_toys/version.rb
|
222
|
+
homepage: https://github.com/AlexWayfer/sequel_migrations_toys
|
223
|
+
licenses:
|
224
|
+
- MIT
|
225
|
+
metadata:
|
226
|
+
source_code_uri: https://github.com/AlexWayfer/sequel_migrations_toys
|
227
|
+
homepage_uri: https://github.com/AlexWayfer/sequel_migrations_toys
|
228
|
+
changelog_uri: https://github.com/AlexWayfer/sequel_migrations_toys/blob/master/CHANGELOG.md
|
229
|
+
post_install_message:
|
230
|
+
rdoc_options: []
|
231
|
+
require_paths:
|
232
|
+
- lib
|
233
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
234
|
+
requirements:
|
235
|
+
- - ">="
|
236
|
+
- !ruby/object:Gem::Version
|
237
|
+
version: '2.5'
|
238
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
239
|
+
requirements:
|
240
|
+
- - ">="
|
241
|
+
- !ruby/object:Gem::Version
|
242
|
+
version: '0'
|
243
|
+
requirements: []
|
244
|
+
rubygems_version: 3.1.2
|
245
|
+
signing_key:
|
246
|
+
specification_version: 4
|
247
|
+
summary: Toys template for Sequel migrations.
|
248
|
+
test_files: []
|