From 4dd68244e2babfe6bf640581effc494519f5a445 Mon Sep 17 00:00:00 2001 From: Cire DIA Date: Thu, 25 Feb 2021 09:41:13 +0000 Subject: [PATCH 01/10] Db Tasks depending on database adapter --- lib/db_agent.rb | 1 + lib/db_agent/db_tasks.rb | 26 +++++++++++++++++++++++ lib/db_agent/db_tasks/mssql.rb | 13 ++++++++++++ lib/db_agent/db_tasks/mysql.rb | 13 ++++++++++++ lib/db_agent/db_tasks/postgresql.rb | 32 +++++++++++++++++++++++++++++ tasks/db.rake | 21 ++----------------- 6 files changed, 87 insertions(+), 19 deletions(-) create mode 100644 lib/db_agent/db_tasks.rb create mode 100644 lib/db_agent/db_tasks/mssql.rb create mode 100644 lib/db_agent/db_tasks/mysql.rb create mode 100644 lib/db_agent/db_tasks/postgresql.rb diff --git a/lib/db_agent.rb b/lib/db_agent.rb index 3863582..270f4aa 100644 --- a/lib/db_agent.rb +++ b/lib/db_agent.rb @@ -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_tasks' DbAgent.require_viewpoints diff --git a/lib/db_agent/db_tasks.rb b/lib/db_agent/db_tasks.rb new file mode 100644 index 0000000..3476e5b --- /dev/null +++ b/lib/db_agent/db_tasks.rb @@ -0,0 +1,26 @@ +require_relative "db_tasks/postgresql" +module DbAgent + module DbTasks + def self.create(adapter) + klass_for(adapter).create + end + + def self.drop(adapter) + klass_for(adapter).drop + end + + def self.klass_for(adapter) + case adapter + when 'postgres' + PostgreSQL + when 'mssql' + MSSQL + when 'mysql' + MySQL + else + PostgreSQL + end + end + + end # module DbTasks +end # module DbAgent diff --git a/lib/db_agent/db_tasks/mssql.rb b/lib/db_agent/db_tasks/mssql.rb new file mode 100644 index 0000000..2b7b95b --- /dev/null +++ b/lib/db_agent/db_tasks/mssql.rb @@ -0,0 +1,13 @@ +module DbAgent + module DbTasks + module MSSQl + def self.create + # Create for MSSQL + end + + def self.drop + # Drop commands for MSSQL + end + end + end +end \ No newline at end of file diff --git a/lib/db_agent/db_tasks/mysql.rb b/lib/db_agent/db_tasks/mysql.rb new file mode 100644 index 0000000..56fe3f2 --- /dev/null +++ b/lib/db_agent/db_tasks/mysql.rb @@ -0,0 +1,13 @@ +module DbAgent + module DbTasks + module MySQL + def self.create + # Create for MySQL + end + + def self.drop + # Drop commands for MySQL + end + end + end +end \ No newline at end of file diff --git a/lib/db_agent/db_tasks/postgresql.rb b/lib/db_agent/db_tasks/postgresql.rb new file mode 100644 index 0000000..ac40132 --- /dev/null +++ b/lib/db_agent/db_tasks/postgresql.rb @@ -0,0 +1,32 @@ +module DbAgent + module DbTasks + module PostgreSQL + def self.create + shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",DATABASE_CONFIG[:user]), + pg_cmd("createdb","--owner=#{DATABASE_CONFIG[:user]}", DATABASE_CONFIG[:database]) + end + + def self.drop + shell pg_cmd("dropdb", DATABASE_CONFIG[:database]), + pg_cmd("dropuser", DATABASE_CONFIG[:user]) + end + + private + def self.pg_cmd(cmd, *args) + puts "Im here" + conf = DATABASE_CONFIG + %Q{#{cmd} -h #{conf[:host]} -p #{conf[:port]} -U #{conf[:user]} #{args.join(' ')}} + end + + def self.psql(*args) + pg_cmd('psql', *args) + end + + def self.shell(*cmds) + cmd = cmds.join("\n") + puts cmd + system cmd + end + end + end # module DbTasks +end # module DbAgent \ No newline at end of file diff --git a/tasks/db.rake b/tasks/db.rake index d91d339..4b19391 100644 --- a/tasks/db.rake +++ b/tasks/db.rake @@ -6,21 +6,6 @@ namespace :db do include DbAgent end - def pg_cmd(cmd, *args) - conf = DATABASE_CONFIG - %Q{#{cmd} -h #{conf[:host]} -p #{conf[:port]} -U #{conf[:user]} #{args.join(' ')}} - end - - def psql(*args) - pg_cmd('psql', *args) - end - - def shell(*cmds) - cmd = cmds.join("\n") - puts cmd - system cmd - end - desc "Pings the database, making sure everything's ready for migration" task :ping => :require do puts "Using #{DATABASE_CONFIG}" @@ -65,14 +50,12 @@ namespace :db do desc "Drops the user & database (USE WITH CARE)" task :drop => :require do - shell pg_cmd("dropdb", DATABASE_CONFIG[:database]), - pg_cmd("dropuser", DATABASE_CONFIG[:user]) + DbTasks.drop(DATABASE_CONFIG[:adapter]) end desc "Creates an fresh new user & database (USE WITH CARE)" task :create => :require do - shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",DATABASE_CONFIG[:user]), - pg_cmd("createdb","--owner=#{DATABASE_CONFIG[:user]}", DATABASE_CONFIG[:database]) + DbTasks.create(DATABASE_CONFIG[:adapter]) end desc "Dump a database backup" From b30b597865df20c3f9cff6bd176bf7152e15629a Mon Sep 17 00:00:00 2001 From: Cire DIA Date: Sun, 28 Feb 2021 12:45:13 +0000 Subject: [PATCH 02/10] Switch to OOP with db handler --- lib/db_agent.rb | 2 +- lib/db_agent/db_handler.rb | 27 ++++++++++++++++++++++ lib/db_agent/db_handler/postgresql.rb | 32 +++++++++++++++++++++++++++ lib/db_agent/db_tasks.rb | 26 ---------------------- lib/db_agent/db_tasks/mssql.rb | 13 ----------- lib/db_agent/db_tasks/mysql.rb | 13 ----------- lib/db_agent/db_tasks/postgresql.rb | 32 --------------------------- tasks/db.rake | 6 ++++- 8 files changed, 65 insertions(+), 86 deletions(-) create mode 100644 lib/db_agent/db_handler.rb create mode 100644 lib/db_agent/db_handler/postgresql.rb delete mode 100644 lib/db_agent/db_tasks.rb delete mode 100644 lib/db_agent/db_tasks/mssql.rb delete mode 100644 lib/db_agent/db_tasks/mysql.rb delete mode 100644 lib/db_agent/db_tasks/postgresql.rb diff --git a/lib/db_agent.rb b/lib/db_agent.rb index 270f4aa..bc2507c 100644 --- a/lib/db_agent.rb +++ b/lib/db_agent.rb @@ -86,5 +86,5 @@ def self.require_viewpoints require 'db_agent/seeder' require 'db_agent/table_orderer' require 'db_agent/webapp' -require 'db_agent/db_tasks' +require 'db_agent/db_handler' DbAgent.require_viewpoints diff --git a/lib/db_agent/db_handler.rb b/lib/db_agent/db_handler.rb new file mode 100644 index 0000000..f2e7c2b --- /dev/null +++ b/lib/db_agent/db_handler.rb @@ -0,0 +1,27 @@ +module DbAgent + class DbHandler + + def initialize() + end + # def self.create(adapter) + # klass_for(adapter).create + # end + + # def self.drop(adapter) + # klass_for(adapter).drop + # end + + # def self.klass_for(adapter) + # case adapter + # when 'postgres' + # PostgreSQL + # when 'mssql' + # MSSQL + # when 'mysql' + # MySQL + # else + # PostgreSQL + # end + # end + end # class DbHandler +end # module DbAgent diff --git a/lib/db_agent/db_handler/postgresql.rb b/lib/db_agent/db_handler/postgresql.rb new file mode 100644 index 0000000..52205a4 --- /dev/null +++ b/lib/db_agent/db_handler/postgresql.rb @@ -0,0 +1,32 @@ +# module DbAgent +# module DbHandler +# module PostgreSQL +# def self.create +# shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",DATABASE_CONFIG[:user]), +# pg_cmd("createdb","--owner=#{DATABASE_CONFIG[:user]}", DATABASE_CONFIG[:database]) +# end + +# def self.drop +# shell pg_cmd("dropdb", DATABASE_CONFIG[:database]), +# pg_cmd("dropuser", DATABASE_CONFIG[:user]) +# end + +# private +# def self.pg_cmd(cmd, *args) +# puts "Im here" +# conf = DATABASE_CONFIG +# %Q{#{cmd} -h #{conf[:host]} -p #{conf[:port]} -U #{conf[:user]} #{args.join(' ')}} +# end + +# def self.psql(*args) +# pg_cmd('psql', *args) +# end + +# def self.shell(*cmds) +# cmd = cmds.join("\n") +# puts cmd +# system cmd +# end +# end +# end # module DbHandler +# end # module DbAgent \ No newline at end of file diff --git a/lib/db_agent/db_tasks.rb b/lib/db_agent/db_tasks.rb deleted file mode 100644 index 3476e5b..0000000 --- a/lib/db_agent/db_tasks.rb +++ /dev/null @@ -1,26 +0,0 @@ -require_relative "db_tasks/postgresql" -module DbAgent - module DbTasks - def self.create(adapter) - klass_for(adapter).create - end - - def self.drop(adapter) - klass_for(adapter).drop - end - - def self.klass_for(adapter) - case adapter - when 'postgres' - PostgreSQL - when 'mssql' - MSSQL - when 'mysql' - MySQL - else - PostgreSQL - end - end - - end # module DbTasks -end # module DbAgent diff --git a/lib/db_agent/db_tasks/mssql.rb b/lib/db_agent/db_tasks/mssql.rb deleted file mode 100644 index 2b7b95b..0000000 --- a/lib/db_agent/db_tasks/mssql.rb +++ /dev/null @@ -1,13 +0,0 @@ -module DbAgent - module DbTasks - module MSSQl - def self.create - # Create for MSSQL - end - - def self.drop - # Drop commands for MSSQL - end - end - end -end \ No newline at end of file diff --git a/lib/db_agent/db_tasks/mysql.rb b/lib/db_agent/db_tasks/mysql.rb deleted file mode 100644 index 56fe3f2..0000000 --- a/lib/db_agent/db_tasks/mysql.rb +++ /dev/null @@ -1,13 +0,0 @@ -module DbAgent - module DbTasks - module MySQL - def self.create - # Create for MySQL - end - - def self.drop - # Drop commands for MySQL - end - end - end -end \ No newline at end of file diff --git a/lib/db_agent/db_tasks/postgresql.rb b/lib/db_agent/db_tasks/postgresql.rb deleted file mode 100644 index ac40132..0000000 --- a/lib/db_agent/db_tasks/postgresql.rb +++ /dev/null @@ -1,32 +0,0 @@ -module DbAgent - module DbTasks - module PostgreSQL - def self.create - shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",DATABASE_CONFIG[:user]), - pg_cmd("createdb","--owner=#{DATABASE_CONFIG[:user]}", DATABASE_CONFIG[:database]) - end - - def self.drop - shell pg_cmd("dropdb", DATABASE_CONFIG[:database]), - pg_cmd("dropuser", DATABASE_CONFIG[:user]) - end - - private - def self.pg_cmd(cmd, *args) - puts "Im here" - conf = DATABASE_CONFIG - %Q{#{cmd} -h #{conf[:host]} -p #{conf[:port]} -U #{conf[:user]} #{args.join(' ')}} - end - - def self.psql(*args) - pg_cmd('psql', *args) - end - - def self.shell(*cmds) - cmd = cmds.join("\n") - puts cmd - system cmd - end - end - end # module DbTasks -end # module DbAgent \ No newline at end of file diff --git a/tasks/db.rake b/tasks/db.rake index 4b19391..098f9b6 100644 --- a/tasks/db.rake +++ b/tasks/db.rake @@ -6,6 +6,10 @@ namespace :db do include DbAgent end + def db_handler + @db_handler ||= DbHandler.new + end + desc "Pings the database, making sure everything's ready for migration" task :ping => :require do puts "Using #{DATABASE_CONFIG}" @@ -55,7 +59,7 @@ namespace :db do desc "Creates an fresh new user & database (USE WITH CARE)" task :create => :require do - DbTasks.create(DATABASE_CONFIG[:adapter]) + puts "OK I'm here" end desc "Dump a database backup" From 49c199e4e36e22c2341ed7d75cd477ec6419db5d Mon Sep 17 00:00:00 2001 From: Cire DIA Date: Sun, 28 Feb 2021 13:11:08 +0000 Subject: [PATCH 03/10] memoize and use DbHandler instance. --- lib/db_agent/db_handler.rb | 37 ++++++++++-------- lib/db_agent/db_handler/postgresql.rb | 56 +++++++++++++-------------- tasks/db.rake | 4 +- 3 files changed, 50 insertions(+), 47 deletions(-) diff --git a/lib/db_agent/db_handler.rb b/lib/db_agent/db_handler.rb index f2e7c2b..2e23f31 100644 --- a/lib/db_agent/db_handler.rb +++ b/lib/db_agent/db_handler.rb @@ -1,27 +1,30 @@ +require_relative 'db_handler/postgresql' module DbAgent class DbHandler - def initialize() + def initialize + end + + def create + klass_for(DATABASE_CONFIG[:adapter]).create end - # def self.create(adapter) - # klass_for(adapter).create - # end - # def self.drop(adapter) + # def drop(adapter) # klass_for(adapter).drop # end - # def self.klass_for(adapter) - # case adapter - # when 'postgres' - # PostgreSQL - # when 'mssql' - # MSSQL - # when 'mysql' - # MySQL - # else - # PostgreSQL - # end - # end + private + def klass_for(adapter) + case adapter + when 'postgres' + PostgreSQL + when 'mssql' + MSSQL + when 'mysql' + MySQL + else + PostgreSQL + end + end end # class DbHandler end # module DbAgent diff --git a/lib/db_agent/db_handler/postgresql.rb b/lib/db_agent/db_handler/postgresql.rb index 52205a4..f66a0ac 100644 --- a/lib/db_agent/db_handler/postgresql.rb +++ b/lib/db_agent/db_handler/postgresql.rb @@ -1,32 +1,32 @@ -# module DbAgent -# module DbHandler -# module PostgreSQL -# def self.create -# shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",DATABASE_CONFIG[:user]), -# pg_cmd("createdb","--owner=#{DATABASE_CONFIG[:user]}", DATABASE_CONFIG[:database]) -# end +module DbAgent + class DbHandler + class PostgreSQL + def self.create + shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",DATABASE_CONFIG[:user]), + pg_cmd("createdb","--owner=#{DATABASE_CONFIG[:user]}", DATABASE_CONFIG[:database]) + end -# def self.drop -# shell pg_cmd("dropdb", DATABASE_CONFIG[:database]), -# pg_cmd("dropuser", DATABASE_CONFIG[:user]) -# end + def self.drop + shell pg_cmd("dropdb", DATABASE_CONFIG[:database]), + pg_cmd("dropuser", DATABASE_CONFIG[:user]) + end -# private -# def self.pg_cmd(cmd, *args) -# puts "Im here" -# conf = DATABASE_CONFIG -# %Q{#{cmd} -h #{conf[:host]} -p #{conf[:port]} -U #{conf[:user]} #{args.join(' ')}} -# end + private + def self.pg_cmd(cmd, *args) + puts "Im here" + conf = DATABASE_CONFIG + %Q{#{cmd} -h #{conf[:host]} -p #{conf[:port]} -U #{conf[:user]} #{args.join(' ')}} + end -# def self.psql(*args) -# pg_cmd('psql', *args) -# end + def self.psql(*args) + pg_cmd('psql', *args) + end -# def self.shell(*cmds) -# cmd = cmds.join("\n") -# puts cmd -# system cmd -# end -# end -# end # module DbHandler -# end # module DbAgent \ No newline at end of file + def self.shell(*cmds) + cmd = cmds.join("\n") + puts cmd + system cmd + end + end + end # module DbHandler +end # module DbAgent \ No newline at end of file diff --git a/tasks/db.rake b/tasks/db.rake index 098f9b6..47260ae 100644 --- a/tasks/db.rake +++ b/tasks/db.rake @@ -54,12 +54,12 @@ namespace :db do desc "Drops the user & database (USE WITH CARE)" task :drop => :require do - DbTasks.drop(DATABASE_CONFIG[:adapter]) + db_handler.drop end desc "Creates an fresh new user & database (USE WITH CARE)" task :create => :require do - puts "OK I'm here" + db_handler.create end desc "Dump a database backup" From 8082fdc84680e6b224920885a89c48fff25d76ea Mon Sep 17 00:00:00 2001 From: Cire DIA Date: Mon, 1 Mar 2021 10:42:29 +0000 Subject: [PATCH 04/10] OOP more classes --- lib/db_agent/db_handler.rb | 15 +++++++++++---- lib/db_agent/db_handler/mssql.rb | 10 ++++++++++ lib/db_agent/db_handler/postgresql.rb | 19 +++++++------------ 3 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 lib/db_agent/db_handler/mssql.rb diff --git a/lib/db_agent/db_handler.rb b/lib/db_agent/db_handler.rb index 2e23f31..15a55d8 100644 --- a/lib/db_agent/db_handler.rb +++ b/lib/db_agent/db_handler.rb @@ -1,17 +1,20 @@ require_relative 'db_handler/postgresql' +require_relative 'db_handler/mssql' + module DbAgent class DbHandler + attr_reader :adapter_klass def initialize end def create - klass_for(DATABASE_CONFIG[:adapter]).create + adapter_klass.create end - # def drop(adapter) - # klass_for(adapter).drop - # end + def drop + adapter_klass.drop + end private def klass_for(adapter) @@ -26,5 +29,9 @@ def klass_for(adapter) PostgreSQL end end + + def adapter_klass + @adapter_klass ||= klass_for(DATABASE_CONFIG[:adapter]).new + end end # class DbHandler end # module DbAgent diff --git a/lib/db_agent/db_handler/mssql.rb b/lib/db_agent/db_handler/mssql.rb new file mode 100644 index 0000000..c73ddcc --- /dev/null +++ b/lib/db_agent/db_handler/mssql.rb @@ -0,0 +1,10 @@ +module DbAgent + class DbHandler + class MSSQL + def initialize ;end + + def create + end + end # MSSQL + end # DbHandler +end # DbAgent \ No newline at end of file diff --git a/lib/db_agent/db_handler/postgresql.rb b/lib/db_agent/db_handler/postgresql.rb index f66a0ac..1233dab 100644 --- a/lib/db_agent/db_handler/postgresql.rb +++ b/lib/db_agent/db_handler/postgresql.rb @@ -1,32 +1,27 @@ module DbAgent class DbHandler class PostgreSQL - def self.create + def initialize ;end + + def create shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",DATABASE_CONFIG[:user]), pg_cmd("createdb","--owner=#{DATABASE_CONFIG[:user]}", DATABASE_CONFIG[:database]) end - def self.drop + def drop shell pg_cmd("dropdb", DATABASE_CONFIG[:database]), pg_cmd("dropuser", DATABASE_CONFIG[:user]) end private - def self.pg_cmd(cmd, *args) - puts "Im here" + def pg_cmd(cmd, *args) conf = DATABASE_CONFIG %Q{#{cmd} -h #{conf[:host]} -p #{conf[:port]} -U #{conf[:user]} #{args.join(' ')}} end - def self.psql(*args) + def psql(*args) pg_cmd('psql', *args) - end - - def self.shell(*cmds) - cmd = cmds.join("\n") - puts cmd - system cmd - end + end end end # module DbHandler end # module DbAgent \ No newline at end of file From ba5d0453d10146abaa15c05d053b2a8478cdd36a Mon Sep 17 00:00:00 2001 From: Bernard Lambeau Date: Mon, 1 Mar 2021 16:35:13 +0100 Subject: [PATCH 05/10] Towards a OO architecture for DbHandler, without global state. --- lib/db_agent/db_handler.rb | 94 ++++++++++++++++++++++---- lib/db_agent/db_handler/mssql.rb | 6 +- lib/db_agent/db_handler/mysql.rb | 10 +++ lib/db_agent/db_handler/postgresql.rb | 38 +++++++---- tasks/db.rake | 96 ++++++++------------------- 5 files changed, 148 insertions(+), 96 deletions(-) create mode 100644 lib/db_agent/db_handler/mysql.rb diff --git a/lib/db_agent/db_handler.rb b/lib/db_agent/db_handler.rb index 15a55d8..2243590 100644 --- a/lib/db_agent/db_handler.rb +++ b/lib/db_agent/db_handler.rb @@ -1,37 +1,103 @@ require_relative 'db_handler/postgresql' require_relative 'db_handler/mssql' +require_relative 'db_handler/mysql' module DbAgent class DbHandler - attr_reader :adapter_klass - - def initialize + + def initialize(config, backup_folder, schema_folder, superconfig = nil ) + @config = config + @superconfig = superconfig + @backup_folder = backup_folder + @schema_folder = schema_folder + end + attr_reader :config, :superconfig, :backup_folder; :schema_folder + + def ping + puts "Using #{config}" + sequel_db.test_connection + puts "Everything seems fine!" end def create - adapter_klass.create + raise NotImplementedError end def drop - adapter_klass.drop + raise NotImplementedError end - private - def klass_for(adapter) - case adapter + def backup + 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) + 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 + + def migrate + Sequel.extension :migration + if (sf = MIGRATIONS_FOLDER/'superuser').exists? + Sequel::Migrator.run(SUPERUSER_DATABASE, MIGRATIONS_FOLDER/'superuser', table: 'superuser_migrations') + end + Sequel::Migrator.run(SEQUEL_DATABASE, MIGRATIONS_FOLDER) + end + + def repl + raise NotImplementedError + end + + def spy + raise NotImplementedError + end + + def self.factor(config, backup_folder, schema_folder, superconfig) + case config[:adapter] when 'postgres' - PostgreSQL + PostgreSQL.new(config, backup_folder, schema_folder, superconfig) when 'mssql' - MSSQL + MSSQL.new(config, superconfig, backup_folder) when 'mysql' - MySQL + MySQL.new(config, superconfig, backup_folder) else - PostgreSQL + PostgreSQL.new(config, superconfig, backup_folder) end end - def adapter_klass - @adapter_klass ||= klass_for(DATABASE_CONFIG[:adapter]).new + 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 diff --git a/lib/db_agent/db_handler/mssql.rb b/lib/db_agent/db_handler/mssql.rb index c73ddcc..98587ed 100644 --- a/lib/db_agent/db_handler/mssql.rb +++ b/lib/db_agent/db_handler/mssql.rb @@ -1,10 +1,10 @@ module DbAgent class DbHandler - class MSSQL - def initialize ;end + class MSSQL < DbHandler def create end + end # MSSQL end # DbHandler -end # DbAgent \ No newline at end of file +end # DbAgent diff --git a/lib/db_agent/db_handler/mysql.rb b/lib/db_agent/db_handler/mysql.rb new file mode 100644 index 0000000..4eaee78 --- /dev/null +++ b/lib/db_agent/db_handler/mysql.rb @@ -0,0 +1,10 @@ +module DbAgent + class DbHandler + class MySQL < DbHandler + + def create + end + + end # MySQL + end # DbHandler +end # DbAgent diff --git a/lib/db_agent/db_handler/postgresql.rb b/lib/db_agent/db_handler/postgresql.rb index 1233dab..7c444b9 100644 --- a/lib/db_agent/db_handler/postgresql.rb +++ b/lib/db_agent/db_handler/postgresql.rb @@ -1,27 +1,41 @@ module DbAgent class DbHandler - class PostgreSQL - def initialize ;end + class PostgreSQL < DbHandler def create - shell pg_cmd("createuser","--no-createdb","--no-createrole","--no-superuser","--no-password",DATABASE_CONFIG[:user]), - pg_cmd("createdb","--owner=#{DATABASE_CONFIG[:user]}", DATABASE_CONFIG[:database]) + 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", DATABASE_CONFIG[:database]), - pg_cmd("dropuser", DATABASE_CONFIG[:user]) + 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 + private + def pg_cmd(cmd, *args) - conf = DATABASE_CONFIG - %Q{#{cmd} -h #{conf[:host]} -p #{conf[:port]} -U #{conf[:user]} #{args.join(' ')}} + %Q{#{cmd} -h #{config[:host]} -p #{config[:port]} -U #{config[:user]} #{args.join(' ')}} end - + def psql(*args) pg_cmd('psql', *args) - end - end + end + end # class PostgreSQL end # module DbHandler end # module DbAgent \ No newline at end of file diff --git a/tasks/db.rake b/tasks/db.rake index 47260ae..de99781 100644 --- a/tasks/db.rake +++ b/tasks/db.rake @@ -7,104 +7,66 @@ namespace :db do end def db_handler - @db_handler ||= DbHandler.new + @db_handler ||= DbHandler.factor(DATABASE_CONFIG, BACKUP_FOLDER, SCHEMA_FOLDER, SUPERUSER_CONFIG) end desc "Pings the database, making sure everything's ready for migration" task :ping => :require do - puts "Using #{DATABASE_CONFIG}" - SEQUEL_DATABASE.test_connection - puts "Everything seems fine!" + db_handler.ping end - - desc "Waits for the database server to ping, up to 15 seconds" - task :wait_server => :require do - require 'net/ping' - raise "No host found" unless DATABASE_CONFIG[:host] - check = Net::Ping::External.new(DATABASE_CONFIG[:host]) - puts "Trying to ping `#{DATABASE_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 - - desc "Waits for the database to ping, up to 15 seconds" - task :wait => :require do - 15.downto(0) do |i| - begin - puts "Using #{DATABASE_CONFIG}" - SEQUEL_DATABASE.test_connection - puts "Database is there. Great." - break - rescue Sequel::Error - raise if i==0 - sleep(1) - end - end - end - + desc "Drops the user & database (USE WITH CARE)" task :drop => :require do db_handler.drop end - + desc "Creates an fresh new user & database (USE WITH CARE)" task :create => :require do db_handler.create end + + desc "Waits for the database server to ping, up to 15 seconds" + task :wait_server => :require do + db_handler.wait_server + end + + desc "Waits for the database to ping, up to 15 seconds" + task :wait => :require do + db_handler.wait + end desc "Dump a database backup" task :backup => :require do - datetime = Time.now.strftime("%Y%m%dT%H%M%S") - shell pg_cmd("pg_dump", "--clean", "> #{BACKUP_FOLDER}/backup-#{datetime}.sql") + db_handler.backup end desc "Restore from the last database backup" task :restore, :pattern do |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', DATABASE_CONFIG[:database], '<', file.to_s) + db_handler.restore(t, args) end task :restore => :require - desc "Rebuilds the database from scratch (USE WITH CARE)" - task :rebuild => [ :drop, :create, :migrate ] - - desc "Revive the database from the last backup" - task :revive => [ :restore, :migrate ] - desc "Runs migrations on the current database" task :migrate => :require do - Sequel.extension :migration - if (sf = MIGRATIONS_FOLDER/'superuser').exists? - Sequel::Migrator.run(SUPERUSER_DATABASE, MIGRATIONS_FOLDER/'superuser', table: 'superuser_migrations') - end - Sequel::Migrator.run(SEQUEL_DATABASE, MIGRATIONS_FOLDER) + db_handler.migrate + end + + desc "Opens a database REPL" + task :repl => :require do + db_handler.repl end desc "Dumps the schema documentation into database/schema" task :spy => :require do - 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 #{DATABASE_CONFIG[:host]} -u #{DATABASE_CONFIG[:user]} -db #{DATABASE_CONFIG[:database]} -s public -o #{SCHEMA_FOLDER}/spy} - system %Q{open #{SCHEMA_FOLDER}/spy/index.html} + db_handler.spy end - desc "Opens a database REPL" - task :repl => :require do - shell pg_cmd('psql', DATABASE_CONFIG[:database]) - end + desc "Rebuilds the database from scratch (USE WITH CARE)" + task :rebuild => [ :drop, :create, :migrate ] + + desc "Revive the database from the last backup" + task :revive => [ :restore, :migrate ] + desc "Checks that all seeds can be installed correctly" task :"check-seeds" do From 50b1ccf86ee8bcfac04a201dfaeedc963fbd0645 Mon Sep 17 00:00:00 2001 From: Cire DIA Date: Tue, 2 Mar 2021 09:48:07 +0000 Subject: [PATCH 06/10] Global state in one options hash --- lib/db_agent/db_handler.rb | 22 +++++++++++----------- tasks/db.rake | 3 ++- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/db_agent/db_handler.rb b/lib/db_agent/db_handler.rb index 2243590..942647b 100644 --- a/lib/db_agent/db_handler.rb +++ b/lib/db_agent/db_handler.rb @@ -5,11 +5,11 @@ module DbAgent class DbHandler - def initialize(config, backup_folder, schema_folder, superconfig = nil ) - @config = config - @superconfig = superconfig - @backup_folder = backup_folder - @schema_folder = schema_folder + def initialize(options) + @config = options[:config] + @backup_folder = options[:backup] + @schema_folder = options[:schema] + @superconfig = options[:superconfig] end attr_reader :config, :superconfig, :backup_folder; :schema_folder @@ -75,16 +75,16 @@ def spy raise NotImplementedError end - def self.factor(config, backup_folder, schema_folder, superconfig) - case config[:adapter] + def self.factor(options) + case options[:config][:adapter] when 'postgres' - PostgreSQL.new(config, backup_folder, schema_folder, superconfig) + PostgreSQL.new(options) when 'mssql' - MSSQL.new(config, superconfig, backup_folder) + MSSQL.new(options) when 'mysql' - MySQL.new(config, superconfig, backup_folder) + MySQL.new(options) else - PostgreSQL.new(config, superconfig, backup_folder) + PostgreSQL.new(options) end end diff --git a/tasks/db.rake b/tasks/db.rake index de99781..6a7c571 100644 --- a/tasks/db.rake +++ b/tasks/db.rake @@ -7,7 +7,8 @@ namespace :db do end def db_handler - @db_handler ||= DbHandler.factor(DATABASE_CONFIG, BACKUP_FOLDER, SCHEMA_FOLDER, SUPERUSER_CONFIG) + options = { config: DATABASE_CONFIG, backup: BACKUP_FOLDER, schema: SCHEMA_FOLDER, superconfig: SUPERUSER_CONFIG} + @db_handler ||= DbHandler.factor(options) end desc "Pings the database, making sure everything's ready for migration" From 3f32fc6b396cc4a0d109d5c8ab12934c52d06b72 Mon Sep 17 00:00:00 2001 From: Cire DIA Date: Tue, 2 Mar 2021 10:22:16 +0000 Subject: [PATCH 07/10] Extract migration folder global var --- lib/db_agent/db_handler.rb | 11 ++++++----- tasks/db.rake | 9 +++++++-- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/lib/db_agent/db_handler.rb b/lib/db_agent/db_handler.rb index 942647b..2b4f856 100644 --- a/lib/db_agent/db_handler.rb +++ b/lib/db_agent/db_handler.rb @@ -7,11 +7,12 @@ class DbHandler def initialize(options) @config = options[:config] + @superconfig = options[:superconfig] @backup_folder = options[:backup] @schema_folder = options[:schema] - @superconfig = options[:superconfig] + @migrations_folder = options[:migrations] end - attr_reader :config, :superconfig, :backup_folder; :schema_folder + attr_reader :config, :superconfig, :backup_folder, :schema_folder, :migrations_folder def ping puts "Using #{config}" @@ -61,10 +62,10 @@ def restore(t, args) def migrate Sequel.extension :migration - if (sf = MIGRATIONS_FOLDER/'superuser').exists? - Sequel::Migrator.run(SUPERUSER_DATABASE, MIGRATIONS_FOLDER/'superuser', table: 'superuser_migrations') + if (sf = migrations_folder/'superuser').exists? + Sequel::Migrator.run(sequel_superdb, migrations_folder/'superuser', table: 'superuser_migrations') end - Sequel::Migrator.run(SEQUEL_DATABASE, MIGRATIONS_FOLDER) + Sequel::Migrator.run(sequel_db, migrations_folder) end def repl diff --git a/tasks/db.rake b/tasks/db.rake index 6a7c571..f607f86 100644 --- a/tasks/db.rake +++ b/tasks/db.rake @@ -7,8 +7,13 @@ namespace :db do end def db_handler - options = { config: DATABASE_CONFIG, backup: BACKUP_FOLDER, schema: SCHEMA_FOLDER, superconfig: SUPERUSER_CONFIG} - @db_handler ||= DbHandler.factor(options) + @db_handler ||= DbHandler.factor({ + config: DATABASE_CONFIG, + superconfig: SUPERUSER_CONFIG, + backup: BACKUP_FOLDER, + schema: SCHEMA_FOLDER, + migrations: MIGRATIONS_FOLDER + }) end desc "Pings the database, making sure everything's ready for migration" From 5290c80dafee817b5e5d792cb5d8de6f59d15302 Mon Sep 17 00:00:00 2001 From: Cire DIA Date: Thu, 4 Mar 2021 16:30:23 +0000 Subject: [PATCH 08/10] move restore to adapter classes --- lib/db_agent/db_handler.rb | 7 +------ lib/db_agent/db_handler/postgresql.rb | 9 +++++++++ 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/lib/db_agent/db_handler.rb b/lib/db_agent/db_handler.rb index 2b4f856..8c40903 100644 --- a/lib/db_agent/db_handler.rb +++ b/lib/db_agent/db_handler.rb @@ -52,12 +52,7 @@ def wait_server 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) + raise NotImplementedError end def migrate diff --git a/lib/db_agent/db_handler/postgresql.rb b/lib/db_agent/db_handler/postgresql.rb index 7c444b9..e5c65ff 100644 --- a/lib/db_agent/db_handler/postgresql.rb +++ b/lib/db_agent/db_handler/postgresql.rb @@ -27,6 +27,15 @@ def 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) From d9a9456e9086e50fd80b22e38a42db549567e26d Mon Sep 17 00:00:00 2001 From: Cire DIA Date: Thu, 4 Mar 2021 16:51:21 +0000 Subject: [PATCH 09/10] Method's to implement for adapter classes --- lib/db_agent/db_handler/mssql.rb | 8 ++++++++ lib/db_agent/db_handler/mysql.rb | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/lib/db_agent/db_handler/mssql.rb b/lib/db_agent/db_handler/mssql.rb index 98587ed..a9213d7 100644 --- a/lib/db_agent/db_handler/mssql.rb +++ b/lib/db_agent/db_handler/mssql.rb @@ -5,6 +5,14 @@ class MSSQL < DbHandler def create end + def drop + end + + def backup + end + + def restore(t, args) + end end # MSSQL end # DbHandler end # DbAgent diff --git a/lib/db_agent/db_handler/mysql.rb b/lib/db_agent/db_handler/mysql.rb index 4eaee78..cf75bc9 100644 --- a/lib/db_agent/db_handler/mysql.rb +++ b/lib/db_agent/db_handler/mysql.rb @@ -5,6 +5,14 @@ class MySQL < DbHandler def create end + def drop + end + + def backup + end + + def restore(t, args) + end end # MySQL end # DbHandler end # DbAgent From a068c278dc532254727d54b7a5da5d2b13e79f7c Mon Sep 17 00:00:00 2001 From: Cire DIA Date: Thu, 4 Mar 2021 17:35:21 +0000 Subject: [PATCH 10/10] Implements methods for MySQL Class --- lib/db_agent/db_handler.rb | 4 ++++ lib/db_agent/db_handler/mssql.rb | 13 +++++++++++ lib/db_agent/db_handler/mysql.rb | 40 ++++++++++++++++++++++++++++++-- 3 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/db_agent/db_handler.rb b/lib/db_agent/db_handler.rb index 8c40903..abbcaa5 100644 --- a/lib/db_agent/db_handler.rb +++ b/lib/db_agent/db_handler.rb @@ -32,6 +32,10 @@ def backup raise NotImplementedError end + def repl + raise NotImplementedError + end + def wait_server require 'net/ping' raise "No host found" unless config[:host] diff --git a/lib/db_agent/db_handler/mssql.rb b/lib/db_agent/db_handler/mssql.rb index a9213d7..054d799 100644 --- a/lib/db_agent/db_handler/mssql.rb +++ b/lib/db_agent/db_handler/mssql.rb @@ -3,12 +3,25 @@ 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) diff --git a/lib/db_agent/db_handler/mysql.rb b/lib/db_agent/db_handler/mysql.rb index cf75bc9..2c3da8e 100644 --- a/lib/db_agent/db_handler/mysql.rb +++ b/lib/db_agent/db_handler/mysql.rb @@ -1,17 +1,53 @@ +# 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 restore(t, args) + def mysqldump(*args) + mysql_cmd('mysqldump', *args) end end # MySQL end # DbHandler