Sha256: d0b536661cdd38f71aec356473e5011f071ab53a087de91294729a6c4224aded

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

require 'active_record'
require 'active_support'
require 'stringio'
require 'uri'

require File.dirname(__FILE__) + '/adapter_hacks'

module Taps
module Schema
	extend self

	def create_config(url)
		uri = URI.parse(url)
		adapter = uri.scheme
		adapter = 'postgresql' if adapter == 'postgres'
		adapter = 'sqlite3' if adapter == 'sqlite'
		config = {
			'adapter' => adapter,
			'database' => uri.path.blank? ? uri.host : uri.path.split('/')[1],
			'username' => uri.user,
			'password' => uri.password,
			'host' => uri.host,
		}
		config = sqlite_config(url) if config['adapter'] == 'sqlite3'
		config
	end

	def sqlite_config(url)
		m = %r{(sqlite3?)://(.+)}.match(url)
		{ 'adapter' => 'sqlite3', 'database' => m[2] }
	end

	def connection(database_url)
		config = create_config(database_url)
		Taps::AdapterHacks.load(config['adapter'])
		ActiveRecord::Base.establish_connection(config)
	end

	def dump(database_url)
		connection(database_url)

		stream = StringIO.new
		ActiveRecord::SchemaDumper.ignore_tables = []
		ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream)
		stream.string
	end

	def dump_without_indexes(database_url)
		schema = dump(database_url)
		schema.split("\n").collect do |line|
			if line =~ /^\s+add_index/
				line = "##{line}"
			end
			line
		end.join("\n")
	end

	def indexes(database_url)
		schema = dump(database_url)
		schema.split("\n").collect do |line|
			line if line =~ /^\s+add_index/
		end.uniq.join("\n")
	end

	def load(database_url, schema)
		connection(database_url)
		eval(schema)
	end

	def load_indexes(database_url, indexes)
		connection(database_url)

		schema =<<EORUBY
ActiveRecord::Schema.define do
	#{indexes}
end
EORUBY
		eval(schema)
	end

	def reset_db_sequences(database_url)
		connection(database_url)

		if ActiveRecord::Base.connection.respond_to?(:reset_pk_sequence!)
			ActiveRecord::Base.connection.tables.each do |table|
				ActiveRecord::Base.connection.reset_pk_sequence!(table)
			end
		end
	end
end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
ricardochimal-taps-0.2.7 lib/taps/schema.rb
taps-0.2.7 lib/taps/schema.rb