Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.bundle
**/vendor/bundle
**/volumes
**/.volumes
/examples
6 changes: 3 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
**/.bundle
**/vendor/bundle
/bin
**/volumes
**/schema/spy
examples/suppliers-and-parts/data/tmp/
examples/*/data/tmp/
examples/suppliers-and-parts/data/incity/
examples/suppliers-and-parts/data/new_empty/
examples/suppliers-and-parts/backups/*.sql
pkg/*
/.volumes
.volumes
**/.volumes
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,6 @@ package:

gem.push:
ls pkg/dbagent*.gem | xargs gem push

clean:
find . -name .volumes | xargs rm -rf
4 changes: 1 addition & 3 deletions examples/multi-db/data/base/db1/01-todo.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
[
{
"id": 1,
"title": "Write more documenation",
"description": "Get started guide should probably be longer",
"done": false
"title": "Write more documenation"
}
]
6 changes: 6 additions & 0 deletions examples/multi-db/data/base/db2/01-users.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[
{
"id": 1,
"name": "Bernard Lambeau"
}
]
1 change: 1 addition & 0 deletions examples/multi-db/data/base/db2/metadata.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{ "inherits": "empty" }
10 changes: 5 additions & 5 deletions examples/multi-db/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ services:
postgres:
image: postgres:15
environment:
POSTGRES_USER: postgres
POSTGRES_USER: dbagent
POSTGRES_PASSWORD: dbagent
POSTGRES_DB: postgres
POSTGRES_PASSWORD: password
ports:
- 5432:5432
volumes:
- ./initdb:/docker-entrypoint-initdb.d
- ./volumes/pgdata:/var/lib/postgresql/data
- ./.volumes/pgdata:/var/lib/postgresql/data

# The `database` agent is the logical DbAgent tool. The agent is NOT intended to
# be started in production.
Expand All @@ -36,6 +36,6 @@ services:
- ./schema:/home/app/schema
environment:
DBAGENT_HOST: postgres
DBAGENT_USER: postgres
DBAGENT_USER: dbagent
DBAGENT_PASSWORD: dbagent
DBAGENT_DB: postgres
DBAGENT_PASSWORD: password
4 changes: 3 additions & 1 deletion examples/suppliers-and-parts/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ services:
image: postgres:15
environment:
POSTGRES_USER: dbagent
POSTGRES_PASSWORD: dbagent
POSTGRES_DB: suppliers-and-parts
volumes:
- ./volumes/pgdata:/var/lib/postgresql/data
- ./.volumes/pgdata:/var/lib/postgresql/data

# The `database` agent is the logical DbAgent tool. The agent is NOT intended to
# be started in production.
Expand All @@ -33,4 +34,5 @@ services:
environment:
DBAGENT_HOST: postgres
DBAGENT_USER: dbagent
DBAGENT_PASSWORD: dbagent
DBAGENT_DB: suppliers-and-parts
8 changes: 5 additions & 3 deletions examples/todo/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ services:
postgres:
image: postgres:9.5
environment:
POSTGRES_USER: todo
POSTGRES_USER: dbagent
POSTGRES_PASSWORD: dbagent
POSTGRES_DB: todo
volumes:
- ./volumes/pgdata:/var/lib/postgresql/data
- ./.volumes/pgdata:/var/lib/postgresql/data

# The `database` agent is the logical DbAgent tool. The agent is NOT intended to
# be started in production.
Expand All @@ -31,5 +32,6 @@ services:
- ./data:/home/app/data
- ./schema:/home/app/schema
environment:
DBAGENT_USER: todo
DBAGENT_USER: dbagent
DBAGENT_PASSWORD: dbagent
DBAGENT_DB: todo
1 change: 1 addition & 0 deletions lib/db_agent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ def self.default_superconfig(cfg = default_config)
def self.default_handler
cfg = default_config
DbHandler.factor({
databases: ENV['DBAGENT_DATABASES'],
config: cfg,
superconfig: default_superconfig(cfg),
root: ROOT_FOLDER,
Expand Down
19 changes: 16 additions & 3 deletions lib/db_agent/data_folder.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,26 @@
module DbAgent
class DataFolder

def initialize(db_handler)
def initialize(db_handler, database_suffix = nil)
@db_handler = db_handler
@database_suffix = database_suffix
end
attr_reader :db_handler

def seed_folder(seed, database = nil)
SeedFolder.new(self, seed, database)
def seed_folder(seed, database_suffix = @database_suffix)
SeedFolder.new(self, seed, database_suffix)
end

def path
db_handler.data_folder
end

def /(part)
path/part
end

def glob(*args, &bl)
path.glob(*args, &bl)
end

end # class DataFolder
Expand Down
118 changes: 18 additions & 100 deletions lib/db_agent/db_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module DbAgent
class DbHandler

def initialize(options)
@original_options = options
@options = options
@config = options[:config]
@superconfig = options[:superconfig]
@root_folder = options[:root]
Expand All @@ -15,113 +15,28 @@ def initialize(options)
@superuser_migrations_table = options[:superuser_migrations_table] || 'superuser_migrations'
require_viewpoints!
end
attr_reader :config, :superconfig
attr_reader :options, :config, :superconfig
attr_reader :backup_folder, :schema_folder, :migrations_folder
attr_reader :data_folder, :viewpoints_folder
attr_reader :migrations_table, :superuser_migrations_table

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])
print "Trying to ping `#{config[:host]}`\n"
wait_timeout_in_seconds.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 wait
print "Using #{config}\n"
wait_timeout_in_seconds.downto(0) do |i|
print "."
begin
sequel_db.test_connection
print "\nDatabase is there. Great.\n"
break
rescue Sequel::Error
if i==0
print "\n"
raise
end
sleep(1)
end
end
end

def restore(t, args)
raise NotImplementedError
end

def migrate(version = nil)
Sequel.extension :migration
sf = migrations_folder/'superuser'
if sf.exists? && !sf.glob('*.rb').empty?
Sequel::Migrator.run(sequel_superdb, migrations_folder/'superuser', table: superuser_migrations_table, target: version)
end
Sequel::Migrator.run(sequel_db, migrations_folder, table: migrations_table, target: version)
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)
if options[:databases]
Composite.new(options)
else
PostgreSQL.new(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
end

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

def system(cmd, *args)
puts cmd
::Kernel.system(cmd, *args)
Expand All @@ -135,7 +50,7 @@ def require_viewpoints!
# Forking

def fork(options = {})
DbHandler.new(@original_options.merge(options))
DbHandler.factor(@options.merge(options))
end

def fork_config(partial_config = {})
Expand All @@ -154,6 +69,9 @@ def print(*args)

end # class DbHandler
end # module DbAgent
require_relative 'db_handler/actions'
require_relative 'db_handler/composite'
require_relative 'db_handler/relational'
require_relative 'db_handler/postgresql'
require_relative 'db_handler/mssql'
require_relative 'db_handler/mysql'
40 changes: 40 additions & 0 deletions lib/db_agent/db_handler/actions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module DbAgent
class DbHandler
module Actions

def ping
end

def create
end

def drop
end

def backup
end

def repl
end

def wait_server
end

def wait
end

def restore(t, args)
end

def migrate(version = nil)
end

def repl
end

def spy
end

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

def each_db_handler
database_names.each do |name|
yield(name, self.fork_config(database: name).fork({
databases: nil,
backup: @backup_folder/name,
schema: @schema_folder/name,
migrations: @migrations_folder/name,
}))
end
end

def database_names
case dbs = options[:databases]
when '/from-empty-seeds'
(data_folder/'empty').glob('*').filter{|f| f.directory? }.map{|f| f.basename.to_s }
when NilClass
[config[:database]]
else
dbs.split(/\s*,\s*/)
end
end

Actions.public_instance_methods.each do |meth|
define_method(meth) do |*args, &bl|
each_db_handler do |dbname, db|
db.send(meth, *args, &bl)
end
end
end

def seeder(database_suffix = nil)
Seeder::Composite.new(self)
end

end # class Composite
end # class DbHandler
end # module DbAgent
1 change: 1 addition & 0 deletions lib/db_agent/db_handler/mssql.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module DbAgent
class DbHandler
class MSSQL < DbHandler
include Relational

def create
raise
Expand Down
Loading
Loading