sequel-rake-tasks 0.0.4 → 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 +4 -4
- data/lib/sequel_rake_tasks.rb +79 -76
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b214379f62b5bfc4bd7d16cdf8df633f55bda179
|
4
|
+
data.tar.gz: 4b6231426549c6cb1d6f11157eb604e4bb9faec0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7fbd79ead48fa9b288aa593c9418724ffd2b3241ec7c1014c857a6877a32247235b4aac1d503dbba507661d1653735820dc5b4320bb40dc33894fcfc3971d0a
|
7
|
+
data.tar.gz: d10bc73755382ac2ba6b53d3fa4b045be0265a1c596d73f1a0c88c86be268766cbb35506ffce3a353fdaf46661caa8fb81f80aa04a5336e42171e240f6b1cac8
|
data/lib/sequel_rake_tasks.rb
CHANGED
@@ -9,42 +9,33 @@ module Sequel
|
|
9
9
|
class RakeTasks < ::Rake::TaskLib
|
10
10
|
include ::Rake::DSL if defined?(::Rake::DSL)
|
11
11
|
|
12
|
-
attr_reader :connection,
|
13
|
-
:connection_config,
|
14
|
-
:migrator_klass,
|
15
|
-
:migrations_dir,
|
16
|
-
:migrations_opts,
|
17
|
-
:schema_file,
|
18
|
-
:seed_file,
|
19
|
-
:structure_file
|
20
|
-
|
21
12
|
def initialize(options)
|
22
|
-
@
|
23
|
-
@connection_config = options[:connection_config]
|
24
|
-
@migrator_klass = options[:migrator]
|
25
|
-
@migrations_dir = options[:migrations_dir]
|
26
|
-
@migrations_opts = options[:migrations_opts]
|
27
|
-
@schema_file = options[:schema_file]
|
28
|
-
@seed_file = options[:seed_file]
|
29
|
-
@structure_file = options[:structure_file]
|
13
|
+
@options = options
|
30
14
|
define_tasks
|
31
15
|
end
|
32
16
|
|
17
|
+
private
|
18
|
+
|
33
19
|
def db_commands
|
34
|
-
|
20
|
+
assert_option(:connection_config) do |connection_config|
|
21
|
+
MysqlDbCommands.new(connection_config)
|
22
|
+
end
|
35
23
|
end
|
36
24
|
|
37
|
-
def
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
25
|
+
def assert_option(option, &block)
|
26
|
+
if value = get_option(option)
|
27
|
+
yield value
|
28
|
+
else
|
29
|
+
raise "`#{option}` option not defined!"
|
42
30
|
end
|
43
31
|
end
|
44
32
|
|
33
|
+
def get_option(option)
|
34
|
+
@options[option]
|
35
|
+
end
|
36
|
+
|
45
37
|
def define_tasks
|
46
38
|
namespace :db do
|
47
|
-
|
48
39
|
desc 'Setup the database.'
|
49
40
|
task :setup => [:create, :migrate]
|
50
41
|
|
@@ -52,7 +43,7 @@ module Sequel
|
|
52
43
|
task :reset => [:drop, :setup]
|
53
44
|
|
54
45
|
desc 'Create the database.'
|
55
|
-
task :create do
|
46
|
+
task :create do
|
56
47
|
db_commands.create_database
|
57
48
|
end
|
58
49
|
|
@@ -61,67 +52,96 @@ module Sequel
|
|
61
52
|
db_commands.drop_database
|
62
53
|
end
|
63
54
|
|
64
|
-
desc 'Load the seeds'
|
55
|
+
desc 'Load the seeds.'
|
65
56
|
task :seed do
|
66
|
-
|
57
|
+
assert_option(:seed_file) do |seed_file|
|
58
|
+
load(seed_file)
|
59
|
+
end
|
67
60
|
end
|
68
61
|
|
69
62
|
desc 'Migrate the database.'
|
70
63
|
task :migrate do
|
71
|
-
migrator
|
72
|
-
|
64
|
+
assert_option(:migrator) do |migrator_klass|
|
65
|
+
assert_option(:migrations_dir) do |migrations_dir|
|
66
|
+
db_commands.migrate(migrator_klass, migrations_dir, get_option(:migrations_opts))
|
67
|
+
Rake::Task['db:schema:dump'].invoke if get_option(:schema_file)
|
68
|
+
end
|
69
|
+
end
|
73
70
|
end
|
74
71
|
|
75
72
|
namespace :schema do
|
76
|
-
|
77
73
|
desc 'Dump the current database schema as a migration.'
|
78
74
|
task :dump do
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
File.open(schema_file, 'w') { |f| f.write(text) }
|
75
|
+
assert_option(:schema_file) do |schema_file|
|
76
|
+
db_commands.dump_schema(schema_file)
|
77
|
+
end
|
83
78
|
end
|
84
79
|
|
85
80
|
desc 'Loads the schema_file into the current environment\'s database'
|
86
81
|
task :load do
|
87
|
-
|
82
|
+
assert_option(:schema_file) do |schema_file|
|
83
|
+
db_commands.load_schema(schema_file)
|
84
|
+
end
|
88
85
|
end
|
89
86
|
end
|
90
87
|
|
91
88
|
namespace :structure do
|
92
|
-
|
93
89
|
desc 'Load the database structure file'
|
94
90
|
task :load do
|
95
|
-
|
96
|
-
|
91
|
+
assert_option(:structure_file) do |structure_file|
|
92
|
+
db_commands.load_sql_file(structure_file)
|
93
|
+
end
|
97
94
|
end
|
98
|
-
|
99
95
|
end
|
100
|
-
|
101
96
|
end
|
102
97
|
end
|
103
|
-
|
104
98
|
end
|
105
99
|
|
106
100
|
class MysqlDbCommands
|
107
|
-
|
108
101
|
def initialize(connection_config)
|
109
102
|
@connection_config = connection_config
|
110
103
|
end
|
111
104
|
|
112
105
|
def drop_database
|
113
|
-
|
106
|
+
with_master_connection do |connection|
|
107
|
+
connection.execute("DROP DATABASE IF EXISTS `#{database_name}`")
|
108
|
+
end
|
114
109
|
end
|
115
110
|
|
116
111
|
def create_database
|
117
|
-
|
112
|
+
with_master_connection do |connection|
|
113
|
+
connection.execute("CREATE DATABASE IF NOT EXISTS `#{database_name}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
|
114
|
+
end
|
118
115
|
end
|
119
116
|
|
120
117
|
def load_sql_file(filename)
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
118
|
+
with_database_connection do |connection|
|
119
|
+
connection.execute("SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1")
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def load_schema(schema_file)
|
124
|
+
with_database_connection do |connection|
|
125
|
+
eval(File.read schema_file).apply(connection, :up)
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
def dump_schema(schema_file)
|
130
|
+
with_database_connection do |connection|
|
131
|
+
connection.extension :schema_dumper
|
132
|
+
text = connection.dump_schema_migration(same_db: false)
|
133
|
+
text = text.gsub(/ +$/, '') # remove trailing whitespace
|
134
|
+
File.open(schema_file, 'w') { |f| f.write(text) }
|
135
|
+
end
|
136
|
+
end
|
137
|
+
|
138
|
+
def migrate(migrator_klass, migrations_dir, migrations_opts)
|
139
|
+
with_database_connection do |connection|
|
140
|
+
args = [connection, migrations_dir]
|
141
|
+
args << migrations_opts if migrations_opts
|
142
|
+
migrator = migrator_klass.new(*args)
|
143
|
+
migrator.run
|
144
|
+
end
|
125
145
|
end
|
126
146
|
|
127
147
|
private
|
@@ -136,21 +156,6 @@ module Sequel
|
|
136
156
|
connection_config[:username]
|
137
157
|
end
|
138
158
|
|
139
|
-
def password
|
140
|
-
connection_config[:password]
|
141
|
-
end
|
142
|
-
|
143
|
-
def host
|
144
|
-
end
|
145
|
-
|
146
|
-
def host
|
147
|
-
connection_config[:host]
|
148
|
-
end
|
149
|
-
|
150
|
-
def port
|
151
|
-
connection_config[:port]
|
152
|
-
end
|
153
|
-
|
154
159
|
def charset
|
155
160
|
connection_config[:charset] || 'utf8'
|
156
161
|
end
|
@@ -159,23 +164,21 @@ module Sequel
|
|
159
164
|
connection_config[:collation] || 'utf8_unicode_ci'
|
160
165
|
end
|
161
166
|
|
162
|
-
def
|
163
|
-
|
164
|
-
commands << '-e' << statement
|
165
|
-
system(*commands)
|
167
|
+
def with_database_connection(&block)
|
168
|
+
with_connection(connection_config, &block)
|
166
169
|
end
|
167
170
|
|
168
|
-
def
|
169
|
-
|
170
|
-
commands << "--user=#{Shellwords.escape(username)}" unless blank?(username)
|
171
|
-
commands << "--password=#{Shellwords.escape(password)}" unless blank?(password)
|
172
|
-
commands << "--host=#{host}" unless blank?(host)
|
173
|
-
commands
|
171
|
+
def with_master_connection(&block)
|
172
|
+
with_connection(connection_config.reject { |key, value| key.to_s == "database" }, &block)
|
174
173
|
end
|
175
174
|
|
176
|
-
def
|
177
|
-
|
175
|
+
def with_connection(config, &block)
|
176
|
+
connection = Sequel.connect(config)
|
177
|
+
begin
|
178
|
+
yield connection
|
179
|
+
ensure
|
180
|
+
connection.disconnect
|
181
|
+
end
|
178
182
|
end
|
179
|
-
|
180
183
|
end
|
181
184
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sequel-rake-tasks
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Myles Megyesi
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2018-03-09 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rake
|
@@ -67,7 +67,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
67
67
|
version: '0'
|
68
68
|
requirements: []
|
69
69
|
rubyforge_project:
|
70
|
-
rubygems_version: 2.
|
70
|
+
rubygems_version: 2.6.14
|
71
71
|
signing_key:
|
72
72
|
specification_version: 4
|
73
73
|
summary: Rake tasks for Sequel
|