Skip to content

Commit

Permalink
fix connection leak
Browse files Browse the repository at this point in the history
  • Loading branch information
martonarbocz committed Jan 8, 2024
1 parent 025747b commit 83ad649
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 31 deletions.
2 changes: 2 additions & 0 deletions bin/setup
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ set -euo pipefail
IFS=$'\n\t'
set -vx

brew install pkg-config

bundle install

# Do any other automated setup that you need to do here
64 changes: 33 additions & 31 deletions lib/sequel_data/migrate/migrator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,39 +15,41 @@ def initialize(config)
end

def migrate
db = connect_database
dataset = ensure_table_exists(db)

already_migrated = dataset.select_map(column).to_set
migration_files = fetch_migration_files.reject { |file| already_migrated.include?(File.basename(file)) }.sort
migrations = fetch_migrations(migration_files)

migrations.zip(migration_files).each do |migration, file|
db.log_info("Begin applying migration file #{file}")
migration.new.up
set_migration_version(db, file)
db.log_info("Finished applying migration version #{file}")
connect_database do |db|
dataset = ensure_table_exists(db)

already_migrated = dataset.select_map(column).to_set
migration_files = fetch_migration_files.reject { |file| already_migrated.include?(File.basename(file)) }.sort
migrations = fetch_migrations(migration_files)

migrations.zip(migration_files).each do |migration, file|
db.log_info("Begin applying migration file #{file}")
migration.new.up
set_migration_version(db, file)
db.log_info("Finished applying migration version #{file}")
end
end
end

def rollback(step = 1)
db = connect_database
dataset = ensure_table_exists(db)

already_migrated = dataset.select_map(column).to_set
migration_files = fetch_migration_files.select do |file|
already_migrated.include?(File.basename(file))
end.sort.reverse!
migrations = fetch_migrations(migration_files)

migrations.zip(migration_files).each do |migration, file|
step -= 1
break if step.negative?

db.log_info("Begin rolling back migration file #{file}")
migration.new.down
remove_migration_version(db, file)
db.log_info("Finished rolling back migration version #{file}")
connect_database do |db|
dataset = ensure_table_exists(db)

already_migrated = dataset.select_map(column).to_set
migration_files = fetch_migration_files.select do |file|
already_migrated.include?(File.basename(file))
end.sort.reverse!
migrations = fetch_migrations(migration_files)

migrations.zip(migration_files).each do |migration, file|
step -= 1
break if step.negative?

db.log_info("Begin rolling back migration file #{file}")
migration.new.down
remove_migration_version(db, file)
db.log_info("Finished rolling back migration version #{file}")
end
end
end

Expand All @@ -63,10 +65,10 @@ def table
:data_migrations
end

def connect_database
def connect_database(&block)
raise ConfigurationError, "db_configuration is not set" if config.db_configuration.host.nil?

Sequel.connect(config.db_configuration.host)
Sequel.connect(config.db_configuration.host, &block)
end

def ensure_table_exists(db)
Expand Down

0 comments on commit 83ad649

Please sign in to comment.