db_vcs 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ad87a4ce6a39d924dfd4b7df99393d0379511b6034f38ef10be59e091b0bf847
4
- data.tar.gz: 01d1173505e38a6abc7e9cceef3541fc13e338e4f90c0020e9b5f3ce35d452ea
3
+ metadata.gz: f22df6cac64b23c5396db8e1ba55b640b7a4fd5ffc2a55d8f05a7185985ba067
4
+ data.tar.gz: 0610b51b4a5ca2025eb8f76b1df56ed12ad4c802be3cbd99a9b00167a6456108
5
5
  SHA512:
6
- metadata.gz: 6a90671b45f5d4c0069c01a37722f2224a0e655b3c3907a90cc255e2114f087e23d3c53a135b21fe6821ae188240f8f4cf9c52deb1416fb73c81dde74037ed39
7
- data.tar.gz: 1952c97adeeb2134ed06b4ecde8036a3ed6adedee0634bf281e1e28a6fda120aa416e1707a3ddd8cd75a7d43fd4929e93290d8a19e6fdc267f9866209dd83c01
6
+ metadata.gz: fb51601226cb01d431ec76cfb76d527f5bd4e28134c8c3a075d588ecd1643b81cbdb5adacf4a50af2deded09abb43fe27c2e7e28aa571446091c53dc3f59649f
7
+ data.tar.gz: fec6984d43ddbbdafb389153b69d8f2a14139382bcba0debc3ad52e18e6520fd0410ca5743b6f286de1eb52c56d676565269da38b961f0e099d0d3bc0cf34948
data/.db_vcs.yml.sample CHANGED
@@ -3,6 +3,9 @@ pg_config:
3
3
  port: 5532
4
4
  mongo_config:
5
5
  mongo_uri: "mongodb://localhost:28017"
6
+ mysql_config:
7
+ username: root
8
+ port: 3406
6
9
  dbs_in_use:
7
10
  <% DbVcs::Manager::ADAPTERS.keys.each do |adapter_name| %>
8
11
  - <%= adapter_name %>
data/CHANGELOG.md CHANGED
@@ -2,6 +2,10 @@
2
2
 
3
3
  Nothing new yet
4
4
 
5
+ ## [1.1.0] - 2022-01-27
6
+
7
+ - Add support of MySQL
8
+
5
9
  ## [1.0.0] - 2022-01-04
6
10
 
7
11
  - Initial release
data/README.md CHANGED
@@ -50,13 +50,22 @@ mongo_config: # Configuration of Mongodb
50
50
  mongodump_path: "/path/to/mongodump" # resolved automatically using which util. Override it otherwise
51
51
  mongorestore_path: "/path/to/mongorestore" # resolved automatically using which util. Override it otherwise
52
52
  mongo_uri: mongodb://localhost:27017
53
+ mysql_config: # Configuration of MySQL
54
+ host: 127.0.0.1
55
+ port: '3306'
56
+ username: root
57
+ password: root
58
+ mysqldump_path: "/path/to/mysqldump" # resolved automatically using which util. Override it otherwise
59
+ mysql_path: "/path/to/mysql" # resolved automatically using which util. Override it otherwise
53
60
  ```
54
61
 
55
62
  Notices:
56
63
 
57
64
  - to be able to use MongoDB - you need to have "mongo" gem installed
58
- - to be able to use MongoDB - you need to have [MongoDB Database Tools](https://docs.mongodb.com/database-tools/) installed
65
+ - to be able to use MongoDB - you need to have [MongoDB Database Tools](https://docs.mongodb.com/database-tools/) installed. You can have it as docker container - just make sure to adjust "mongodump_path" and "mongorestore_path" options in configuration file
59
66
  - to be able to use PostgreSQL - you need to have "pg" gem installed
67
+ - to be able to use MySQL - you need to have "mysql2" gem installed
68
+ - to be able to use MySQL - you need to have `mysql` and `mysqldump` utils installed. You can have it as docker container - just make sure to adjust "mysqldump_path" and "mysql_path" options in configuration file
60
69
 
61
70
  ## Usage
62
71
 
@@ -137,7 +146,7 @@ Example of file's content:
137
146
  # Comparison with third argument is needed to detect whether the checkout is related to the switching between branches. In this case third argument equals to 1.
138
147
  if [ $3 -eq 1 ]
139
148
  then
140
- bundle exec db-vcs check
149
+ bundle exec db-vcs check # or "bundle exec db-vcs create" if you want to create databases automatically on checkout
141
150
  bundle exec pumactl -C config/puma.rb restart
142
151
  fi
143
152
  ```
data/bin/start-docker CHANGED
@@ -7,9 +7,16 @@ require "db_vcs/autoconfigure"
7
7
  pg_user = DbVcs.config.pg_config.username
8
8
  pg_port = DbVcs.config.pg_config.port
9
9
  mongo_port = URI.parse(DbVcs.config.mongo_config.mongo_uri).port
10
+ mysql_port = DbVcs.config.mysql_config.port
10
11
 
11
12
  pid = Kernel.spawn(
12
- { "PGUSER" => pg_user.to_s, "PG_PORT" => pg_port.to_s, "MONGO_PORT" => mongo_port.to_s }, "docker-compose up 1>&2",
13
+ {
14
+ "PGUSER" => pg_user.to_s,
15
+ "PG_PORT" => pg_port.to_s,
16
+ "MONGO_PORT" => mongo_port.to_s,
17
+ "MYSQL_PORT" => mysql_port.to_s
18
+ },
19
+ "docker-compose up 1>&2",
13
20
  close_others: true
14
21
  )
15
22
  Process.waitpid(pid)
data/db_vcs.gemspec CHANGED
@@ -33,6 +33,7 @@ Gem::Specification.new do |spec|
33
33
 
34
34
  spec.add_development_dependency "pg", "~> 1.2"
35
35
  spec.add_development_dependency "mongo", "~> 2.17"
36
+ spec.add_development_dependency "mysql2", "~> 0.5.3"
36
37
  spec.add_development_dependency "rspec", "~> 3.10"
37
38
  spec.add_development_dependency "rspec-its", "~> 1.3"
38
39
  spec.add_development_dependency "fivemat", "~> 1.3"
data/docker-compose.yml CHANGED
@@ -18,6 +18,15 @@ services:
18
18
  ports:
19
19
  - ${MONGO_PORT}:27017
20
20
 
21
+ mysql:
22
+ image: mysql:latest
23
+ environment:
24
+ - MYSQL_ALLOW_EMPTY_PASSWORD=true
25
+ volumes:
26
+ - mysql_db:/var/lib/mysql
27
+ ports:
28
+ - ${MYSQL_PORT}:3306
21
29
  volumes:
22
30
  mongo_db:
23
31
  pg_db:
32
+ mysql_db:
@@ -0,0 +1,95 @@
1
+ # frozen_string_literal: true
2
+
3
+ module DbVcs
4
+ module Adapters
5
+ class Mysql
6
+ class Config
7
+ include DbVcs::ConfigAttributes
8
+
9
+ attr_accessor :host, :port, :username, :password
10
+ # Path to mysqldump util. It is resolved automatically.
11
+ attr_accessor :mysqldump_path
12
+ # Path to mysql util. It is resolved automatically.
13
+ attr_accessor :mysql_path
14
+
15
+ def initialize
16
+ @host = "127.0.0.1"
17
+ @port = "3306"
18
+ @username = "root"
19
+ @password = nil
20
+ @mysqldump_path = DbVcs::Utils.resolve_exec_path("mysqldump")
21
+ @mysql_path = DbVcs::Utils.resolve_exec_path("mysql")
22
+ end
23
+ end
24
+
25
+ class << self
26
+ include DbVcs::AdapterInterface
27
+
28
+ # @return [DbVcs::Adapters::Mysql::Config]
29
+ def config
30
+ DbVcs.config.mysql_config
31
+ end
32
+
33
+ # @return [Mysql2::Client]
34
+ def connection
35
+ @connection ||=
36
+ begin
37
+ require "mysql2"
38
+ Mysql2::Client.new(
39
+ host: config.host,
40
+ username: config.username,
41
+ port: config.port,
42
+ password: config.password
43
+ )
44
+ end
45
+ end
46
+
47
+ # @param db_name [String]
48
+ # @return [Boolean]
49
+ def db_exists?(db_name)
50
+ !connection
51
+ .query("SELECT 1 as one FROM `INFORMATION_SCHEMA`.`SCHEMATA` WHERE `SCHEMA_NAME` = '#{db_name}' LIMIT 1")
52
+ .first.nil?
53
+ end
54
+
55
+ # @param to_db [String]
56
+ # @param from_db [String]
57
+ # @return void
58
+ def copy_database(to_db, from_db)
59
+ create_opts =
60
+ connection.query(<<~SQL).first
61
+ SELECT `DEFAULT_CHARACTER_SET_NAME` as charset, `DEFAULT_COLLATION_NAME` as collation
62
+ FROM `INFORMATION_SCHEMA`.`SCHEMATA` WHERE `SCHEMA_NAME` = '#{from_db}' LIMIT 1
63
+ SQL
64
+ connection.query(<<~SQL)
65
+ CREATE DATABASE #{to_db} CHARACTER SET #{create_opts["charset"]} COLLATE #{create_opts["collation"]}
66
+ SQL
67
+ password_opt = config.password.to_s.strip.empty? ? "" : "-p #{config.password}"
68
+ command =
69
+ <<~SH
70
+ #{config.mysqldump_path} -u #{config.username} #{password_opt} -h #{config.host} -P #{config.port} #{from_db} \
71
+ | #{config.mysql_path} -u #{config.username} #{password_opt} -h #{config.host} -P #{config.port} #{to_db}
72
+ SH
73
+ `#{command}`
74
+ end
75
+
76
+ # @param db_name [String]
77
+ # @return void
78
+ def create_database(db_name)
79
+ connection.query("CREATE DATABASE #{db_name}")
80
+ end
81
+
82
+ # @return [Array<String>]
83
+ def list_databases
84
+ connection.query("SELECT `SCHEMA_NAME` FROM `INFORMATION_SCHEMA`.`SCHEMATA`").to_a.flat_map(&:values)
85
+ end
86
+
87
+ # @param db_name [String]
88
+ # @return [void]
89
+ def drop_by_dbname(db_name)
90
+ connection.query("DROP DATABASE #{db_name}")
91
+ end
92
+ end
93
+ end
94
+ end
95
+ end
data/lib/db_vcs/config.rb CHANGED
@@ -12,7 +12,7 @@ module DbVcs
12
12
  # available values. Defaults to empty array.
13
13
  attr_accessor :dbs_in_use
14
14
  # Configuration of dbs clients.
15
- attr_reader :pg_config, :mongo_config
15
+ attr_reader :pg_config, :mongo_config, :mysql_config
16
16
  # A name of branch to be used as a default branch to copy databases from.
17
17
  attr_accessor :main_branch
18
18
 
@@ -23,6 +23,7 @@ module DbVcs
23
23
  @main_branch = "main"
24
24
  @pg_config = DbVcs::Adapters::Postgres::Config.new
25
25
  @mongo_config = DbVcs::Adapters::Mongo::Config.new
26
+ @mysql_config = DbVcs::Adapters::Mysql::Config.new
26
27
  end
27
28
 
28
29
  # @param hash [Hash]
@@ -36,5 +37,11 @@ module DbVcs
36
37
  def mongo_config=(hash)
37
38
  mongo_config.assign_attributes(hash)
38
39
  end
40
+
41
+ # @param hash [Hash]
42
+ # @return [void]
43
+ def mysql_config=(hash)
44
+ mysql_config.assign_attributes(hash)
45
+ end
39
46
  end
40
47
  end
@@ -2,7 +2,11 @@
2
2
 
3
3
  module DbVcs
4
4
  class Manager
5
- ADAPTERS = { "postgres" => DbVcs::Adapters::Postgres, "mongo" => DbVcs::Adapters::Mongo }.freeze
5
+ ADAPTERS = {
6
+ "postgres" => DbVcs::Adapters::Postgres,
7
+ "mongo" => DbVcs::Adapters::Mongo,
8
+ "mysql" => DbVcs::Adapters::Mysql
9
+ }.freeze
6
10
 
7
11
  class << self
8
12
  # @param adapter_name [String]
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DbVcs
4
- VERSION = "1.0.0"
4
+ VERSION = "1.1.0"
5
5
  end
data/lib/db_vcs.rb CHANGED
@@ -8,6 +8,7 @@ require_relative "db_vcs/config"
8
8
  require_relative "db_vcs/adapter_interface"
9
9
  require_relative "db_vcs/adapters/mongo"
10
10
  require_relative "db_vcs/adapters/postgres"
11
+ require_relative "db_vcs/adapters/mysql"
11
12
  require_relative "db_vcs/utils"
12
13
  require_relative "db_vcs/manager"
13
14
  require_relative "db_vcs/version"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: db_vcs
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ivan Dzyzenko
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-01-20 00:00:00.000000000 Z
11
+ date: 2022-01-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '2.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: mysql2
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.5.3
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.5.3
41
55
  - !ruby/object:Gem::Dependency
42
56
  name: rspec
43
57
  requirement: !ruby/object:Gem::Requirement
@@ -135,6 +149,7 @@ files:
135
149
  - lib/db_vcs.rb
136
150
  - lib/db_vcs/adapter_interface.rb
137
151
  - lib/db_vcs/adapters/mongo.rb
152
+ - lib/db_vcs/adapters/mysql.rb
138
153
  - lib/db_vcs/adapters/postgres.rb
139
154
  - lib/db_vcs/autoconfigure.rb
140
155
  - lib/db_vcs/config.rb
@@ -148,8 +163,8 @@ licenses:
148
163
  metadata:
149
164
  allowed_push_host: https://rubygems.org
150
165
  homepage_uri: https://github.com/intale/db_vcs
151
- source_code_uri: https://github.com/intale/vcs_db/tree/v1.0.0
152
- changelog_uri: https://github.com/intale/vcs_db/blob/v1.0.0/CHANGELOG.md
166
+ source_code_uri: https://github.com/intale/vcs_db/tree/v1.1.0
167
+ changelog_uri: https://github.com/intale/vcs_db/blob/v1.1.0/CHANGELOG.md
153
168
  post_install_message:
154
169
  rdoc_options: []
155
170
  require_paths: