Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tasks for adapters #10

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions lib/db_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,5 @@ def self.require_viewpoints
require 'db_agent/seeder'
require 'db_agent/table_orderer'
require 'db_agent/webapp'
require 'db_agent/db_handler'
DbAgent.require_viewpoints
103 changes: 103 additions & 0 deletions lib/db_agent/db_handler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
require_relative 'db_handler/postgresql'
require_relative 'db_handler/mssql'
require_relative 'db_handler/mysql'

module DbAgent
class DbHandler

def initialize(options)
@config = options[:config]
@superconfig = options[:superconfig]
@backup_folder = options[:backup]
@schema_folder = options[:schema]
@migrations_folder = options[:migrations]
end
attr_reader :config, :superconfig, :backup_folder, :schema_folder, :migrations_folder

def ping
puts "Using #{config}"
sequel_db.test_connection
puts "Everything seems fine!"
end

def create
raise NotImplementedError
end

def drop
raise NotImplementedError
end

def backup
raise NotImplementedError
end

def repl
raise NotImplementedError
end

def wait_server
require 'net/ping'
raise "No host found" unless config[:host]
check = Net::Ping::External.new(config[:host])
puts "Trying to ping `#{config[:host]}`"
15.downto(0) do |i|
print "."
if check.ping?
print "\nServer found.\n"
break
elsif i == 0
print "\n"
raise "Server not found, I give up."
else
sleep(1)
end
end
end

def restore(t, args)
raise NotImplementedError
end

def migrate
Sequel.extension :migration
if (sf = migrations_folder/'superuser').exists?
Sequel::Migrator.run(sequel_superdb, migrations_folder/'superuser', table: 'superuser_migrations')
end
Sequel::Migrator.run(sequel_db, migrations_folder)
end

def repl
raise NotImplementedError
end

def spy
raise NotImplementedError
end

def self.factor(options)
case options[:config][:adapter]
when 'postgres'
PostgreSQL.new(options)
when 'mssql'
MSSQL.new(options)
when 'mysql'
MySQL.new(options)
else
PostgreSQL.new(options)
end
end

private

def sequel_db
@sequel_db ||= ::Sequel.connect(config)
end

def sequel_superdb
raise "No superconfig set" if superconfig.nil?
@sequel_superdb ||= ::Sequel.connect(superconfig)
end

end # class DbHandler
end # module DbAgent
31 changes: 31 additions & 0 deletions lib/db_agent/db_handler/mssql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module DbAgent
class DbHandler
class MSSQL < DbHandler

def create
raise
end

def drop
raise
end

def backup
raise
end

def repl
raise
end

def spy
jdbc_jar = (Path.dir.parent/'vendor').glob('mssql*.jar').first
system %Q{java -jar vendor/schemaSpy_5.0.0.jar -dp #{jdbc_jar} -t mssql05 -host #{DATABASE_CONFIG[:host]} -u #{DATABASE_CONFIG[:user]} -p #{DATABASE_CONFIG[:password]} -db #{DATABASE_CONFIG[:database]} -port #{DATABASE_CONFIG[:port]} -s dbo -o #{SCHEMA_FOLDER}/spy}
system %Q{open #{schema_folder}/spy/index.html}
end

def restore(t, args)
end
end # MSSQL
end # DbHandler
end # DbAgent
54 changes: 54 additions & 0 deletions lib/db_agent/db_handler/mysql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# frozen_string_literal: true

module DbAgent
class DbHandler
class MySQL < DbHandler
def create
raise
end

def drop
raise
end

def backup
datetime = Time.now.strftime('%Y%m%dT%H%M%S')
shell mysqldump(config[:database], "> #{backup_folder}/backup-#{datetime}.sql")
end

def repl
shell mysql(config[:database])
end

def spy
jdbc_jar = (Path.dir.parent / 'vendor').glob('mysql*.jar').first
system %(java -jar vendor/schemaSpy_5.0.0.jar -dp #{jdbc_jar} -t mysql -host #{config[:host]} -u #{config[:user]} -p #{config[:password]} -db #{config[:database]} -s public -o #{schema_folder}/spy)
system %(open #{schema_folder}/spy/index.html)
end

def restore(_t, args)
candidates = backup_folder.glob('*.sql').sort
if args[:pattern] && rx = Regexp.new(args[:pattern])
candidates = candidates.select { |f| f.basename.to_s =~ rx }
end
file = candidates.last
shell mysql(config[:database], '<', file.to_s)
end

private

def mysql_cmd(cmd, *args)
conf = config
%(#{cmd} -h #{config[:host]} --password=#{config[:password]} -P #{config[:port]} -u #{config[:user]} #{args.join(' ')})
end

def mysql(*args)
mysql_cmd('mysql', *args)
end

def mysqldump(*args)
mysql_cmd('mysqldump', *args)
end
end # MySQL
end # DbHandler
end # DbAgent
50 changes: 50 additions & 0 deletions lib/db_agent/db_handler/postgresql.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
module DbAgent
class DbHandler
class PostgreSQL < DbHandler

def create
shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",config[:user]),
pg_cmd("createdb","--owner=#{config[:user]}", config[:database])
end

def drop
shell pg_cmd("dropdb", config[:database]),
pg_cmd("dropuser", config[:user])
end

def backup
datetime = Time.now.strftime("%Y%m%dT%H%M%S")
shell pg_cmd("pg_dump", "--clean", "> #{backup_folder}/backup-#{datetime}.sql")
end

def repl
shell pg_cmd('psql', config[:database])
end

def spy
jdbc_jar = (Path.dir.parent/'vendor').glob('postgresql*.jar').first
system %Q{java -jar vendor/schemaSpy_5.0.0.jar -dp #{jdbc_jar} -t pgsql -host #{config[:host]} -u #{config[:user]} -db #{config[:database]} -s public -o #{schema_folder}/spy}
system %Q{open #{schema_folder}/spy/index.html}
end

def restore(t, args)
candidates = backup_folder.glob("*.sql").sort
if args[:pattern] && rx = Regexp.new(args[:pattern])
candidates = candidates.select{|f| f.basename.to_s =~ rx }
end
file = candidates.last
shell pg_cmd('psql', config[:database], '<', file.to_s)
end

private

def pg_cmd(cmd, *args)
%Q{#{cmd} -h #{config[:host]} -p #{config[:port]} -U #{config[:user]} #{args.join(' ')}}
end

def psql(*args)
pg_cmd('psql', *args)
end
end # class PostgreSQL
end # module DbHandler
end # module DbAgent
Loading