scheman-rails 0.0.1

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.
Files changed (65) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +22 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +27 -0
  6. data/Rakefile +2 -0
  7. data/lib/scheman/rails.rb +9 -0
  8. data/lib/scheman/rails/commands/applier.rb +93 -0
  9. data/lib/scheman/rails/commands/base.rb +66 -0
  10. data/lib/scheman/rails/commands/dumper.rb +102 -0
  11. data/lib/scheman/rails/errors.rb +11 -0
  12. data/lib/scheman/rails/railtie.rb +9 -0
  13. data/lib/scheman/rails/tasks.rake +13 -0
  14. data/lib/scheman/rails/version.rb +5 -0
  15. data/scheman-rails.gemspec +27 -0
  16. data/spec/dummy/.gitignore +16 -0
  17. data/spec/dummy/README.rdoc +28 -0
  18. data/spec/dummy/Rakefile +6 -0
  19. data/spec/dummy/app/assets/images/.keep +0 -0
  20. data/spec/dummy/app/assets/javascripts/application.js +16 -0
  21. data/spec/dummy/app/assets/stylesheets/application.css +15 -0
  22. data/spec/dummy/app/controllers/application_controller.rb +5 -0
  23. data/spec/dummy/app/controllers/concerns/.keep +0 -0
  24. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  25. data/spec/dummy/app/mailers/.keep +0 -0
  26. data/spec/dummy/app/models/.keep +0 -0
  27. data/spec/dummy/app/models/concerns/.keep +0 -0
  28. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  29. data/spec/dummy/bin/bundle +3 -0
  30. data/spec/dummy/bin/rails +8 -0
  31. data/spec/dummy/bin/rake +8 -0
  32. data/spec/dummy/bin/spring +18 -0
  33. data/spec/dummy/config.ru +4 -0
  34. data/spec/dummy/config/application.rb +30 -0
  35. data/spec/dummy/config/boot.rb +4 -0
  36. data/spec/dummy/config/database.yml +54 -0
  37. data/spec/dummy/config/environment.rb +5 -0
  38. data/spec/dummy/config/environments/development.rb +37 -0
  39. data/spec/dummy/config/environments/production.rb +82 -0
  40. data/spec/dummy/config/environments/test.rb +39 -0
  41. data/spec/dummy/config/initializers/assets.rb +8 -0
  42. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  43. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -0
  44. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  45. data/spec/dummy/config/initializers/inflections.rb +16 -0
  46. data/spec/dummy/config/initializers/mime_types.rb +4 -0
  47. data/spec/dummy/config/initializers/session_store.rb +3 -0
  48. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  49. data/spec/dummy/config/locales/en.yml +23 -0
  50. data/spec/dummy/config/routes.rb +56 -0
  51. data/spec/dummy/config/secrets.yml +22 -0
  52. data/spec/dummy/db/schema.sql +1 -0
  53. data/spec/dummy/db/seeds.rb +7 -0
  54. data/spec/dummy/lib/assets/.keep +0 -0
  55. data/spec/dummy/lib/tasks/.keep +0 -0
  56. data/spec/dummy/log/.keep +0 -0
  57. data/spec/dummy/public/404.html +67 -0
  58. data/spec/dummy/public/422.html +67 -0
  59. data/spec/dummy/public/500.html +66 -0
  60. data/spec/dummy/public/favicon.ico +0 -0
  61. data/spec/dummy/public/robots.txt +5 -0
  62. data/spec/dummy/vendor/assets/javascripts/.keep +0 -0
  63. data/spec/dummy/vendor/assets/stylesheets/.keep +0 -0
  64. data/spec/spec_helper.rb +11 -0
  65. metadata +269 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b4ba8edf518b26826e1be2563823b2798d92bbdb
4
+ data.tar.gz: 93b9787e3189b2b519c380d15137f17ff4b1ec22
5
+ SHA512:
6
+ metadata.gz: 22bf05e8f53408ab2a6098cde714f7e7e09f75587c552115a61a36d0a1047d286d6c54dda76b88a3b2e2b0a923f28313d089c532891d0a17b46a4f3921c9a1ee
7
+ data.tar.gz: d6327c180d80638778c125e9e015c63316c2e674054ca7389477d120840a04ddf51debaad80297c16296e8a3010b5bc6f80a5f3e750dbf4d24ac3bb0f8ad312f
@@ -0,0 +1,22 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem"s dependencies in scheman-rails.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Ryo Nakamura
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,27 @@
1
+ # Scheman::Rails
2
+ Rails plugin for Scheman.
3
+
4
+ ## Usage
5
+ ```ruby
6
+ # Gemfile
7
+ gem "scheman-rails"
8
+ ```
9
+
10
+ ```sh
11
+ # Write your database schema into db/schema.sql
12
+ $ echo '
13
+ CREATE TABLE `users` (
14
+ `id` int(11) NOT NULL AUTO_INCREMENT,
15
+ `name` varchar(255) NOT NULL,
16
+ `created_at` datetime DEFAULT NULL,
17
+ `updated_at` datetime DEFAULT NULL,
18
+ PRIMARY KEY (`id`)
19
+ );
20
+ ' > db/schema.sql
21
+
22
+ # Show diff between the current database schema and db/schema.sql
23
+ $ rake db:schema:diff
24
+
25
+ # Apply diff
26
+ $ rake db:schema:apply
27
+ ```
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+
@@ -0,0 +1,9 @@
1
+ require "open3"
2
+ require "scheman"
3
+
4
+ require "scheman/rails/commands/base"
5
+ require "scheman/rails/commands/applier"
6
+ require "scheman/rails/commands/dumper"
7
+ require "scheman/rails/errors"
8
+ require "scheman/rails/railtie"
9
+ require "scheman/rails/version"
@@ -0,0 +1,93 @@
1
+ module Scheman
2
+ module Rails
3
+ class Applier < Base
4
+ DEFAULT_SCHEMA_MESSAGE = "# Write your database schema here"
5
+
6
+ def self.call
7
+ new.call
8
+ end
9
+
10
+ def call
11
+ execute_sql
12
+ create_schema_file
13
+ end
14
+
15
+ private
16
+
17
+ def execute_sql
18
+ if has_error?
19
+ abort(error_message)
20
+ elsif executed_sql.present?
21
+ puts executed_sql
22
+ end
23
+ end
24
+
25
+ def create_schema_file
26
+ unless after_schema_pathname.exist?
27
+ after_schema_pathname.open("w") do |io|
28
+ io.puts DEFAULT_SCHEMA_MESSAGE
29
+ end
30
+ end
31
+ end
32
+
33
+ def executed_sql
34
+ @executed_sql ||= begin
35
+ result = []
36
+ unless has_database?
37
+ result << create_database_statement
38
+ result << use_database_steatement
39
+ end
40
+ result << diff
41
+ result.join("\n\n")
42
+ end
43
+ end
44
+
45
+ def result
46
+ @result ||= Open3.capture3(command, stdin_data: executed_sql)
47
+ end
48
+
49
+ def result_message
50
+ result[0]
51
+ end
52
+
53
+ def has_error?
54
+ result[2] != 0
55
+ end
56
+
57
+ def error_message
58
+ result[1]
59
+ end
60
+
61
+ def diff
62
+ dumper.result
63
+ end
64
+
65
+ def dumper
66
+ @dumper ||= Scheman::Rails::Dumper.new
67
+ end
68
+
69
+ def command
70
+ "mysql " + command_options.join(" ")
71
+ end
72
+
73
+ def has_database?
74
+ !dumper.has_unknown_database_error?
75
+ end
76
+
77
+ def command_options
78
+ result = []
79
+ result.concat([database]) if has_database?
80
+ result.concat(["--host", host]) if host
81
+ result.concat(["--password", password]) if password
82
+ result.concat(["--port", port]) if port
83
+ result.concat(["--socket", socket]) if socket
84
+ result.concat(["--user", username]) if username
85
+ result
86
+ end
87
+
88
+ def use_database_steatement
89
+ "USE `#{database}`;"
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,66 @@
1
+ module Scheman
2
+ module Rails
3
+ class Base
4
+ SCHEMA_PATH = "db/schema.sql"
5
+
6
+ def self.call
7
+ new.call
8
+ end
9
+
10
+ private
11
+
12
+ def database
13
+ configuration["database"]
14
+ end
15
+
16
+ def encoding
17
+ configuration["encoding"]
18
+ end
19
+
20
+ def socket
21
+ configuration["socket"]
22
+ end
23
+
24
+ def host
25
+ configuration["host"]
26
+ end
27
+
28
+ def password
29
+ configuration["password"]
30
+ end
31
+
32
+ def port
33
+ configuration["port"]
34
+ end
35
+
36
+ def socket
37
+ configuration["socket"]
38
+ end
39
+
40
+ def username
41
+ configuration["username"]
42
+ end
43
+
44
+ def schema_type
45
+ case configuration["adapter"]
46
+ when "mysql2"
47
+ "mysql"
48
+ else
49
+ raise
50
+ end
51
+ end
52
+
53
+ def configuration
54
+ @configuration ||= ActiveRecord::Tasks::DatabaseTasks.current_config
55
+ end
56
+
57
+ def after_schema_pathname
58
+ @after_schema_path ||= ::Rails.root.join(SCHEMA_PATH)
59
+ end
60
+
61
+ def create_database_statement
62
+ "CREATE DATABASE `#{database}`;"
63
+ end
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,102 @@
1
+ module Scheman
2
+ module Rails
3
+ class Dumper < Base
4
+ def call
5
+ case
6
+ when has_unexpected_dump_error?
7
+ abort(dump_error_message)
8
+ when str = result.presence
9
+ puts str
10
+ end
11
+ end
12
+
13
+ def has_unknown_database_error?
14
+ has_dump_error? && /Unknown database/ === dump_error_message
15
+ end
16
+
17
+ def diff
18
+ @diff ||= Scheman::Diff.new(
19
+ before: before_schema,
20
+ after: after_schema,
21
+ type: schema_type,
22
+ )
23
+ end
24
+
25
+ def result
26
+ @result ||= statements.join("\n\n").rstrip
27
+ end
28
+
29
+ private
30
+
31
+ def statements
32
+ result = []
33
+ result << create_database_statement if has_unknown_database_error?
34
+ result << diff
35
+ result
36
+ end
37
+
38
+ def dump
39
+ @dump ||= Open3.capture3(command)
40
+ end
41
+
42
+ def dump_result
43
+ dump[0]
44
+ end
45
+
46
+ def has_dump_error?
47
+ dump[2] != 0
48
+ end
49
+
50
+ def dump_error_message
51
+ dump[1]
52
+ end
53
+
54
+ def has_unexpected_dump_error?
55
+ has_dump_error? && !has_unknown_database_error?
56
+ end
57
+
58
+ def before_schema
59
+ if has_unknown_database_error?
60
+ ""
61
+ else
62
+ dump_result
63
+ end
64
+ end
65
+
66
+ def after_schema
67
+ if after_schema_pathname.exist?
68
+ after_schema_pathname.read
69
+ else
70
+ ""
71
+ end
72
+ end
73
+ 1
74
+ def command
75
+ "mysqldump " + command_options.join(" ")
76
+ end
77
+
78
+ def command_options
79
+ default_command_options + additional_command_options
80
+ end
81
+
82
+ def default_command_options
83
+ [
84
+ database,
85
+ "--compact",
86
+ "--no-data",
87
+ ]
88
+ end
89
+
90
+ def additional_command_options
91
+ result = []
92
+ result.concat(["--default-character-set", encoding]) if encoding
93
+ result.concat(["--host", host]) if host
94
+ result.concat(["--password", password]) if password
95
+ result.concat(["--port", port]) if port
96
+ result.concat(["--socket", socket]) if socket
97
+ result.concat(["--user", username]) if username
98
+ result
99
+ end
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,11 @@
1
+ module Scheman
2
+ module Rails
3
+ module Errors
4
+ class Base < StandardError
5
+ end
6
+
7
+ class UnexpectedDumpError < Base
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,9 @@
1
+ module Scheman
2
+ module Rails
3
+ class Railtie < ::Rails::Railtie
4
+ rake_tasks do
5
+ load "scheman/rails/tasks.rake"
6
+ end
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,13 @@
1
+ namespace :db do
2
+ namespace :schema do
3
+ desc "Show diff between the current DB schema and db/schema.sql"
4
+ task diff: [:environment, :load_config] do
5
+ Scheman::Rails::Dumper.call
6
+ end
7
+
8
+ desc "Apply diff between the current DB schema and db/schema.sql"
9
+ task apply: [:environment, :load_config] do
10
+ Scheman::Rails::Applier.call
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Scheman
2
+ module Rails
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "scheman/rails/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "scheman-rails"
7
+ spec.version = Scheman::Rails::VERSION
8
+ spec.authors = ["Ryo Nakamura"]
9
+ spec.email = ["r7kamura@gmail.com"]
10
+ spec.summary = "Rails plugin for Scheman."
11
+ spec.homepage = "https://github.com/r7kamura/scheman-rails"
12
+ spec.license = "MIT"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0")
15
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
16
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_dependency "rails", ">= 3.0.0"
20
+ spec.add_dependency "scheman", ">= 0.0.4"
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "mysql2"
23
+ spec.add_development_dependency "pry"
24
+ spec.add_development_dependency "rake"
25
+ spec.add_development_dependency "rspec", "2.14.1"
26
+ spec.add_development_dependency "rspec-rails", "2.14.1"
27
+ end
@@ -0,0 +1,16 @@
1
+ # See https://help.github.com/articles/ignoring-files for more about ignoring files.
2
+ #
3
+ # If you find yourself ignoring temporary files generated by your text editor
4
+ # or operating system, you probably want to add a global ignore instead:
5
+ # git config --global core.excludesfile '~/.gitignore_global'
6
+
7
+ # Ignore bundler config.
8
+ /.bundle
9
+
10
+ # Ignore the default SQLite database.
11
+ /db/*.sqlite3
12
+ /db/*.sqlite3-journal
13
+
14
+ # Ignore all logfiles and tempfiles.
15
+ /log/*.log
16
+ /tmp