Skip to content

Commit

Permalink
Make migrator work when a default_schema is set
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremyevans committed Mar 1, 2011
1 parent 0d79af5 commit 40879f1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG
@@ -1,3 +1,7 @@
=== HEAD

* Make migrator work correctly when a default_schema is set (jeremyevans) (#331)

=== 3.21.0 (2011-03-01) === 3.21.0 (2011-03-01)


* Make symbol splitting (:table__column___alias) work correctly for identifiers that are not in the \w character class (authorNari) * Make symbol splitting (:table__column___alias) work correctly for identifiers that are not in the \w character class (authorNari)
Expand Down
3 changes: 2 additions & 1 deletion lib/sequel/extensions/migration.rb
Expand Up @@ -375,7 +375,8 @@ def initialize(db, directory, opts={})
@db = db @db = db
@directory = directory @directory = directory
@files = get_migration_files @files = get_migration_files
@table = opts[:table] || self.class.const_get(:DEFAULT_SCHEMA_TABLE) schema, table = @db.send(:schema_and_table, opts[:table] || self.class.const_get(:DEFAULT_SCHEMA_TABLE))
@table = schema ? Sequel::SQL::QualifiedIdentifier.new(schema, table) : table
@column = opts[:column] || self.class.const_get(:DEFAULT_SCHEMA_COLUMN) @column = opts[:column] || self.class.const_get(:DEFAULT_SCHEMA_COLUMN)
@ds = schema_dataset @ds = schema_dataset
end end
Expand Down
34 changes: 17 additions & 17 deletions spec/extensions/migration_spec.rb
Expand Up @@ -202,7 +202,7 @@ def initialize(*args)
@drops = [] @drops = []
@tables_created = [] @tables_created = []
@columns_created = [] @columns_created = []
@versions = {} @versions = Hash.new{|h,k| h[k.to_sym]}
end end


def version; versions.values.first || 0; end def version; versions.values.first || 0; end
Expand All @@ -212,7 +212,7 @@ def drop_table(*a); super; @drops.concat(a.map{|x| y = x.to_s; y !~ /\Asm(\d+)/;
def create_table(name, opts={}, &block) def create_table(name, opts={}, &block)
super super
@columns_created << / \(?(\w+) integer.*\)?\z/.match(sqls.last)[1].to_sym @columns_created << / \(?(\w+) integer.*\)?\z/.match(sqls.last)[1].to_sym
@tables_created << name @tables_created << name.to_sym
end end


def dataset(opts={}) def dataset(opts={})
Expand All @@ -228,7 +228,7 @@ def fetch_rows(sql); db.execute(sql); yield(db.versions) unless db.versions.empt
end end


def table_exists?(name) def table_exists?(name)
@tables_created.include?(name) @tables_created.include?(name.to_sym)
end end
end end
@db = dbc.new @db = dbc.new
Expand All @@ -250,7 +250,7 @@ def table_exists?(name)


specify "should add a column name if it doesn't already exist in the schema_info table" do specify "should add a column name if it doesn't already exist in the schema_info table" do
@db.create_table(:schema_info){Integer :v} @db.create_table(:schema_info){Integer :v}
@db.should_receive(:alter_table).with(:schema_info) @db.should_receive(:alter_table).with('schema_info')
Sequel::Migrator.apply(@db, @dirname) Sequel::Migrator.apply(@db, @dirname)
end end


Expand Down Expand Up @@ -320,55 +320,55 @@ def table_exists?(name)
@dsc = dsc = Class.new(MockDataset) do @dsc = dsc = Class.new(MockDataset) do
def columns def columns
case opts[:from].first case opts[:from].first
when :schema_info when :schema_info, 'schema_info'
[:version] [:version]
when :schema_migrations when :schema_migrations, 'schema_migrations'
[:filename] [:filename]
when :sm when :sm, 'sm'
[:fn] [:fn]
end end
end end


def fetch_rows(sql) def fetch_rows(sql)
case opts[:from].first case opts[:from].first
when :schema_info when :schema_info, 'schema_info'
yield({:version=>$sequel_migration_version}) yield({:version=>$sequel_migration_version})
when :schema_migrations when :schema_migrations, 'schema_migrations'
$sequel_migration_files.sort.each{|f| yield(:filename=>f)} $sequel_migration_files.sort.each{|f| yield(:filename=>f)}
when :sm when :sm, 'sm'
$sequel_migration_files.sort.each{|f| yield(:fn=>f)} $sequel_migration_files.sort.each{|f| yield(:fn=>f)}
end end
end end


def insert(h={}) def insert(h={})
case opts[:from].first case opts[:from].first
when :schema_info when :schema_info, 'schema_info'
$sequel_migration_version = h.values.first $sequel_migration_version = h.values.first
when :schema_migrations, :sm when :schema_migrations, :sm, 'schema_migrations', 'sm'
$sequel_migration_files << h.values.first $sequel_migration_files << h.values.first
end end
end end


def update(h={}) def update(h={})
case opts[:from].first case opts[:from].first
when :schema_info when :schema_info, 'schema_info'
$sequel_migration_version = h.values.first $sequel_migration_version = h.values.first
end end
end end


def delete def delete
case opts[:from].first case opts[:from].first
when :schema_migrations, :sm when :schema_migrations, :sm, 'schema_migrations', 'sm'
$sequel_migration_files.delete(opts[:where].args.last) $sequel_migration_files.delete(opts[:where].args.last)
end end
end end
end end
dbc = Class.new(MockDatabase) do dbc = Class.new(MockDatabase) do
tables = {} tables = {}
define_method(:dataset){|*a| dsc.new(self, *a)} define_method(:dataset){|*a| dsc.new(self, *a)}
define_method(:create_table){|name, *args| tables[name] = true} define_method(:create_table){|name, *args| tables[name.to_sym] = true}
define_method(:drop_table){|*names| names.each{|n| tables.delete(n)}} define_method(:drop_table){|*names| names.each{|n| tables.delete(n.to_sym)}}
define_method(:table_exists?){|name| tables.has_key?(name)} define_method(:table_exists?){|name| tables.has_key?(name.to_sym)}
end end
@db = dbc.new @db = dbc.new
@m = Sequel::Migrator @m = Sequel::Migrator
Expand Down

0 comments on commit 40879f1

Please sign in to comment.