sql_migrate 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: cb6b95ebc3c9de7fc3e37965f250133727913c3a13f152ded31f68185a27a589
4
+ data.tar.gz: 4df9e485ed39fedb2a1900f5fc1deeac62484b8a68ff72e4c912ee98a2f22c27
5
+ SHA512:
6
+ metadata.gz: 190cb10d634e367e8a86640ef7b95ee27c5947a48864abd49f41d30550b65da8746faffb1908e05180ba3e718f30216b73c08087168dec49c68c965332b2a5b8
7
+ data.tar.gz: c9d305f8ef5cdd1b364b019a4c9127db59a8b852f89a17629ac8fa680e4d948782456cf7b1b6dae8af398071db205a486222c032d7c0c60838f7621b2ca56976
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /Gemfile.lock
6
+ /InstalledFiles
7
+ /pkg/
8
+ /spec/reports/
9
+ /spec/examples.txt
10
+ /test/tmp/
11
+ /test/version_tmp/
12
+ /tmp/
13
+
14
+ ## Documentation cache and generated files:
15
+ /.yardoc/
16
+ /_yardoc/
17
+ /doc/
18
+ /rdoc/
19
+
20
+ ## Environment normalization:
21
+ /.bundle/
22
+ /vendor/bundle
23
+ /lib/bundler/man/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in sql_migrate.gemspec
6
+ gemspec
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # SqlMigrate
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/sql_migrate`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'sql_migrate'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install sql_migrate
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/i2bskn/sql_migrate.
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "sql_migrate"
5
+
6
+ require "pry"
7
+ Pry.start
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install --path .bundle
7
+
8
+ # Do any other automated setup that you need to do here
data/exe/sql_migrate ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "sql_migrate/cli"
4
+
5
+ SqlMigrate::CLI.run
@@ -0,0 +1,9 @@
1
+ require "forwardable"
2
+ require "logger"
3
+ require "mysql2"
4
+ require "sql_migrate/version"
5
+ require "sql_migrate/config"
6
+ require "sql_migrate/migrator"
7
+
8
+ module SqlMigrate
9
+ end
@@ -0,0 +1,61 @@
1
+ require "optparse"
2
+ require "yaml"
3
+ require "sql_migrate"
4
+
5
+ module SqlMigrate
6
+ class CLI
7
+ attr_reader :config
8
+
9
+ def self.run(argv = ARGV)
10
+ cli = new
11
+ cli.parse(argv)
12
+ cli.execute
13
+ end
14
+
15
+ def initialize
16
+ @config = Config.new
17
+ end
18
+
19
+ def parse(argv)
20
+ args = argv.dup
21
+ options = {}
22
+ OptionParser.new do |opt|
23
+ opt.version = VERSION
24
+ opt.banner = "sql_migrate [options] MIGRATIONS_PATH"
25
+ opt.on("-h HOST") { |v| options[:host] = v }
26
+ opt.on("-p PORT") { |v| options[:port] = v }
27
+ opt.on("-d DATABASE") { |v| options[:database] = v }
28
+ opt.on("-u USER") { |v| options[:user] = v }
29
+ opt.on("-p PASSWORD") { |v| options[:password] = v }
30
+ opt.on("-v", "--verbose") { |v| options[:verbose] = v }
31
+ opt.on("-n", "--dry-run") { |v| options[:dryrun] = v }
32
+ opt.on("-f CONFIG") { |v| options[:config] = v }
33
+ opt.parse!(args)
34
+ unless args.size.positive?
35
+ STDERR << "required MIGRATIONS_PATH\n"
36
+ STDERR << "#{opt.banner}\n"
37
+ exit(1)
38
+ end
39
+ end
40
+
41
+ load_from_config(options.delete(:config)) if options.has_key?(:config)
42
+ config.merge(options)
43
+ config.migrations_path = args.first
44
+
45
+ args
46
+ end
47
+
48
+ def execute
49
+ migrator = Migrator.new(config)
50
+ migrator.migrate
51
+ end
52
+
53
+ private
54
+
55
+ def load_from_config(path)
56
+ path = File.expand_path(path)
57
+ yaml = YAML.load_file(path)
58
+ config.merge(yaml)
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,48 @@
1
+ module SqlMigrate
2
+ class Config
3
+ VALID_OPTIONS = [
4
+ :host,
5
+ :port,
6
+ :database,
7
+ :user,
8
+ :password,
9
+ :migrations_path,
10
+ :logger,
11
+ :dryrun,
12
+ :verbose,
13
+ ].freeze
14
+
15
+ attr_accessor *VALID_OPTIONS
16
+
17
+ def initialize
18
+ reset
19
+ end
20
+
21
+ def merge(params)
22
+ params.each do |key, value|
23
+ self.send("#{key}=", value)
24
+ end
25
+ self
26
+ end
27
+
28
+ def reset
29
+ self.host = "localhost"
30
+ self.port = 3306
31
+ self.user = "root"
32
+ self.migrations_path = "migrations"
33
+ self.logger = default_logger
34
+ self.dryrun = false
35
+ self.verbose = false
36
+ end
37
+
38
+ private
39
+
40
+ def default_logger
41
+ logger = Logger.new(STDOUT)
42
+ logger.formatter = proc { |severity, datetime, progname, message|
43
+ "#{datetime.strftime('%F %H:%M:%S.%N')}\t#{severity}\t#{message}\n"
44
+ }
45
+ logger
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,83 @@
1
+ module SqlMigrate
2
+ class Migrator
3
+ extend Forwardable
4
+
5
+ attr_accessor :config
6
+ def_delegator :config, :logger
7
+
8
+ VERSION_TABLE_NAME = "migrate_versions".freeze
9
+
10
+ def initialize(config = nil)
11
+ @config = config || Config.new
12
+ end
13
+
14
+ def migrate
15
+ create_migrate_versions_if_not_exist
16
+ versions = applied_versions
17
+ migration_files.sort.each do |migration|
18
+ version_name = File.basename(migration)
19
+ next if versions.include?(version_name)
20
+ logger.info("apply migration #{version_name}")
21
+ queries_from_migration_file(migration).each { |sql| execute(sql) }
22
+ sql = "insert into #{VERSION_TABLE_NAME} (`version`) values (\"#{version_name}\")"
23
+ execute(sql)
24
+ end
25
+ end
26
+
27
+ def create_migrate_versions_if_not_exist
28
+ unless table_names.include?(VERSION_TABLE_NAME)
29
+ sql = <<-EOS
30
+ create table `#{VERSION_TABLE_NAME}` (
31
+ `version` varchar(256),
32
+ PRIMARY KEY (`version`)
33
+ ) ENGINE=InnoDB DEFAULT CHARSET=utf8
34
+ EOS
35
+ logger.info("create #{VERSION_TABLE_NAME}")
36
+ connection.query(sql)
37
+ end
38
+ end
39
+
40
+ def table_names
41
+ connection.query("show tables", as: :array).to_a.flatten
42
+ end
43
+
44
+ def applied_versions
45
+ connection.query("select * from #{VERSION_TABLE_NAME}", as: :array).to_a.flatten
46
+ end
47
+
48
+ def migration_files
49
+ Dir.glob(File.join(config.migrations_path, "*"))
50
+ end
51
+
52
+ private
53
+
54
+ def execute(sql)
55
+ logger.info("execute sql:\n#{sql}") if config.verbose
56
+ connection.query(sql) unless dryrun?
57
+ end
58
+
59
+ def queries_from_migration_file(path)
60
+ migration_text = File.read(path)
61
+ migration_text.split(";").map(&:strip).reject(&:empty?)
62
+ end
63
+
64
+ def dryrun?
65
+ !!config.dryrun
66
+ end
67
+
68
+ def connection
69
+ @connection ||= Mysql2::Client.new(**db_options)
70
+ end
71
+
72
+ def db_options
73
+ options = {
74
+ host: config.host,
75
+ port: config.port,
76
+ database: config.database,
77
+ username: config.user,
78
+ }
79
+ options[:password] = config.password if config.password
80
+ options
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,3 @@
1
+ module SqlMigrate
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,29 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "sql_migrate/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "sql_migrate"
8
+ spec.version = SqlMigrate::VERSION
9
+ spec.authors = ["i2bskn"]
10
+ spec.email = ["i2bskn@gmail.com"]
11
+
12
+ spec.summary = %q{Schema Migration utilities for MySQL.}
13
+ spec.description = %q{Schema Migration utilities for MySQL.}
14
+ spec.homepage = "https://github.com/i2bskn/sql_migrate"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.0"
26
+ spec.add_development_dependency "pry"
27
+
28
+ spec.add_dependency "mysql2"
29
+ end
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sql_migrate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - i2bskn
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-06-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: pry
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mysql2
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Schema Migration utilities for MySQL.
84
+ email:
85
+ - i2bskn@gmail.com
86
+ executables:
87
+ - sql_migrate
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/setup
98
+ - exe/sql_migrate
99
+ - lib/sql_migrate.rb
100
+ - lib/sql_migrate/cli.rb
101
+ - lib/sql_migrate/config.rb
102
+ - lib/sql_migrate/migrator.rb
103
+ - lib/sql_migrate/version.rb
104
+ - sql_migrate.gemspec
105
+ homepage: https://github.com/i2bskn/sql_migrate
106
+ licenses: []
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.7.6
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Schema Migration utilities for MySQL.
128
+ test_files: []