sequel-rake-tasks 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. checksums.yaml +7 -0
  2. data/lib/sequel_rake_tasks.rb +166 -0
  3. metadata +88 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 167d9d544d500f354ccd463d7686859aa1b4d134
4
+ data.tar.gz: 80580c372d55a3d7d7f45b24011c28c8e6e43701
5
+ SHA512:
6
+ metadata.gz: a3b2ea9946151cc93a714de72084190159c6d73b083fec1b30b835dad536783935ae139e2ca874e215edeeae2106c8e8fed2f7a2fc939a4766421c2ba24d04a8
7
+ data.tar.gz: 6f7894c9da1dd92b7bd19a99e1745fb3d3ac0b848603f1317e738a2bca650c059586b0ce15832e373bcdf66e167d24c828fbf88405681c07852236a8e105a481
@@ -0,0 +1,166 @@
1
+ require 'rake'
2
+ require 'rake/tasklib'
3
+ require 'sequel'
4
+
5
+ Sequel.extension :migration
6
+
7
+ module Sequel
8
+
9
+ class RakeTasks < ::Rake::TaskLib
10
+ include ::Rake::DSL if defined?(::Rake::DSL)
11
+
12
+ attr_reader :connection,
13
+ :connection_config,
14
+ :migrator_klass,
15
+ :migrations_dir,
16
+ :schema_file,
17
+ :seed_file,
18
+ :structure_file
19
+
20
+ def initialize(options)
21
+ @connection = options[:connection]
22
+ @connection_config = options[:connection_config]
23
+ @migrator_klass = options[:migrator]
24
+ @migrations_dir = options[:migrations_dir]
25
+ @schema_file = options[:schema_file]
26
+ @seed_file = options[:seed_file]
27
+ @structure_file = options[:structure_file]
28
+ define_tasks
29
+ end
30
+
31
+ def db_commands
32
+ @db_commands ||= MysqlDbCommands.new(connection_config)
33
+ end
34
+
35
+ def migrator
36
+ @migrator ||= migrator_klass.new(connection, migrations_dir)
37
+ end
38
+
39
+ def define_tasks
40
+ namespace :db do
41
+
42
+ desc 'Setup the database.'
43
+ task :setup => [:create, :migrate]
44
+
45
+ desc 'Reset the database.'
46
+ task :reset => [:drop, :setup]
47
+
48
+ desc 'Create the database.'
49
+ task :create do |t, args|
50
+ db_commands.create_database
51
+ end
52
+
53
+ desc 'Drop the database.'
54
+ task :drop do
55
+ db_commands.drop_database
56
+ end
57
+
58
+ desc 'Load the seeds'
59
+ task :seed do
60
+ load(seed_file)
61
+ end
62
+
63
+ desc 'Migrate the database.'
64
+ task :migrate do
65
+ migrator.run
66
+ Rake::Task['db:schema:dump'].invoke
67
+ end
68
+
69
+ namespace :schema do
70
+
71
+ desc 'Dump the current database schema as a migration.'
72
+ task :dump do
73
+ connection.extension :schema_dumper
74
+ text = connection.dump_schema_migration(same_db: false)
75
+ text = text.gsub(/ +$/, '') # remove trailing whitespace
76
+ File.open(schema_file, 'w') { |f| f.write(text) }
77
+ end
78
+ end
79
+
80
+ namespace :structure do
81
+
82
+ desc 'Load the database structure file'
83
+ task :load do
84
+ raise '`structure_file` option not defined!' unless structure_file
85
+ db_commands.load_sql_file(structure_file)
86
+ end
87
+
88
+ end
89
+
90
+ end
91
+ end
92
+
93
+ end
94
+
95
+ class MysqlDbCommands
96
+
97
+ def initialize(connection_config)
98
+ @connection_config = connection_config
99
+ end
100
+
101
+ def drop_database
102
+ execute("DROP DATABASE IF EXISTS `#{database_name}`")
103
+ end
104
+
105
+ def create_database
106
+ execute("CREATE DATABASE IF NOT EXISTS `#{database_name}` DEFAULT CHARACTER SET #{charset} DEFAULT COLLATE #{collation}")
107
+ end
108
+
109
+ def load_sql_file(filename)
110
+ commands = base_commands
111
+ commands << '--execute' << %{SET FOREIGN_KEY_CHECKS = 0; SOURCE #{filename}; SET FOREIGN_KEY_CHECKS = 1}
112
+ commands << '--database' << database_name
113
+ system(*commands)
114
+ end
115
+
116
+ private
117
+
118
+ attr_reader :connection_config
119
+
120
+ def database_name
121
+ connection_config['database']
122
+ end
123
+
124
+ def username
125
+ connection_config['username']
126
+ end
127
+
128
+ def password
129
+ connection_config['password']
130
+ end
131
+
132
+ def host
133
+ end
134
+
135
+ def host
136
+ connection_config['host']
137
+ end
138
+
139
+ def port
140
+ connection_config['port']
141
+ end
142
+
143
+ def charset
144
+ connection_config['charset'] || 'utf8'
145
+ end
146
+
147
+ def collation
148
+ connection_config['collation'] || 'utf8_unicode_ci'
149
+ end
150
+
151
+ def execute(statement)
152
+ commands = base_commands
153
+ commands << '-e' << statement
154
+ system(*commands)
155
+ end
156
+
157
+ def base_commands
158
+ commands = %w(mysql)
159
+ commands << "--user=#{Shellwords.escape(username)}" unless username.blank?
160
+ commands << "--password=#{Shellwords.escape(password)}" unless password.blank?
161
+ commands << "--host=#{host}" unless host.blank?
162
+ commands
163
+ end
164
+
165
+ end
166
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sequel-rake-tasks
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Myles Megyesi
8
+ - Steve Kim
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-10-14 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: rake
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - ~>
19
+ - !ruby/object:Gem::Version
20
+ version: 10.1.0
21
+ type: :runtime
22
+ prerelease: false
23
+ version_requirements: !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ~>
26
+ - !ruby/object:Gem::Version
27
+ version: 10.1.0
28
+ - !ruby/object:Gem::Dependency
29
+ name: sequel
30
+ requirement: !ruby/object:Gem::Requirement
31
+ requirements:
32
+ - - ~>
33
+ - !ruby/object:Gem::Version
34
+ version: '4.2'
35
+ type: :runtime
36
+ prerelease: false
37
+ version_requirements: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ~>
40
+ - !ruby/object:Gem::Version
41
+ version: '4.2'
42
+ - !ruby/object:Gem::Dependency
43
+ name: rake
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ~>
47
+ - !ruby/object:Gem::Version
48
+ version: 10.1.0
49
+ type: :development
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: 10.1.0
56
+ description: Rake tasks for Sequel
57
+ email:
58
+ - myles.megyesi@gmail.com
59
+ - skim.la@gmail.com
60
+ executables: []
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - lib/sequel_rake_tasks.rb
65
+ homepage:
66
+ licenses: []
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - '>='
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.0.3
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Rake tasks for Sequel
88
+ test_files: []