Sha256: dfc2391579a0aaf2bbff618969ed3f27447041b6ea7f5adf8fa55d5760551f77

Contents?: true

Size: 1.42 KB

Versions: 3

Compression:

Stored size: 1.42 KB

Contents

require 'sequel'
require 'sequel/extensions/schema_dumper'
require 'sequel/extensions/migration'

require 'json'

module Taps
module Schema
	extend self

	def dump(database_url)
		db = Sequel.connect(database_url)
		db.dump_schema_migration(:indexes => false)
	end

	def dump_table(database_url, table)
		Sequel.connect(database_url) do |db|
			<<END_MIG
Class.new(Sequel::Migration) do
	def up
		#{db.dump_table_schema(table, :indexes => false)}
	end
end
END_MIG
		end
	end

	def indexes(database_url)
		db = Sequel.connect(database_url)
		db.dump_indexes_migration
	end

	def indexes_individual(database_url)
		idxs = {}
		Sequel.connect(database_url) do |db|
			tables = db.tables
			tables.each do |table|
				idxs[table] = db.send(:dump_table_indexes, table, :add_index, {}).split("\n")
			end
		end

		idxs.each do |table, indexes|
			idxs[table] = indexes.map do |idx|
				<<END_MIG
Class.new(Sequel::Migration) do
	def up
		#{idx}
	end
end
END_MIG
			end
		end
		idxs.to_json
	end

	def load(database_url, schema)
		Sequel.connect(database_url) do |db|
			eval(schema).apply(db, :up)
		end
	end

	def load_indexes(database_url, indexes)
		Sequel.connect(database_url) do |db|
			eval(indexes).apply(db, :up)
		end
	end

	def reset_db_sequences(database_url)
		db = Sequel.connect(database_url)
		return unless db.respond_to?(:reset_primary_key_sequence)
		db.tables.each do |table|
			db.reset_primary_key_sequence(table)
		end
	end
end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
taps-0.3.2 lib/taps/schema.rb
taps-0.3.1 lib/taps/schema.rb
taps-0.3.0 lib/taps/schema.rb