pg-migrator 1.1.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -12,7 +12,7 @@ namespace :db do
12
12
  end
13
13
  end
14
14
 
15
- desc 'Drops the "public" schema and runs all the migrations'
15
+ desc 'Drops all schemas currently in the search_path and then applies all the migrations'
16
16
  task :reset => :setup do
17
17
  @migrator.reset
18
18
  end
@@ -1,5 +1,5 @@
1
1
  module PG
2
2
  class Migrator
3
- VERSION = "1.1.1"
3
+ VERSION = "1.2.0"
4
4
  end
5
5
  end
data/lib/pg-migrator.rb CHANGED
@@ -19,8 +19,19 @@ module PG
19
19
 
20
20
  def reset
21
21
  @pg.transaction do |conn|
22
- conn.exec 'DROP SCHEMA public CASCADE'
23
- conn.exec 'CREATE SCHEMA public'
22
+ search_path = conn.exec("SHOW search_path").values.first.first
23
+ current_user = conn.exec("SELECT CURRENT_USER").values.first.first
24
+ schemas_sql = "SELECT nspname FROM pg_namespace
25
+ WHERE NOT(nspname LIKE 'pg_%')
26
+ AND nspname != 'information_schema'
27
+ "
28
+ schemas = conn.exec(schemas_sql).values.flatten
29
+ search_path.split(',').each do |p|
30
+ p = p.strip.sub /"\$user"/, current_user
31
+ next unless schemas.include? p
32
+ conn.exec "DROP SCHEMA #{p} CASCADE;
33
+ CREATE SCHEMA #{p};"
34
+ end
24
35
  end
25
36
  migrate_up
26
37
  end
@@ -4,6 +4,7 @@ require './lib/pg-migrator'
4
4
  describe PG::Migrator do
5
5
  before do
6
6
  @pg = PGconn.connect :dbname => 'pg-migrator'
7
+ @pg.exec 'SET client_min_messages = warning'
7
8
  logger = Logger.new STDERR
8
9
  logger.level = Logger::FATAL
9
10
  @migrator = PG::Migrator.new({:dbname => 'pg-migrator'}, '/tmp', logger)
@@ -14,12 +15,32 @@ describe PG::Migrator do
14
15
  end
15
16
 
16
17
  describe 'reset' do
17
- it 'deletes previous tables' do
18
+ it 'drops tables' do
18
19
  @pg.exec 'create table bar (id int)'
19
20
  @migrator.reset
20
21
  tables = @pg.exec "select tablename from pg_tables where schemaname = 'public'"
21
22
  refute_includes tables.values.flatten, 'bar'
22
23
  end
24
+
25
+ it 'drops tables from all schemas in search_path' do
26
+ @pg.exec "
27
+ drop schema if exists foo cascade;
28
+ create schema foo
29
+ create table bar (id int);
30
+ drop schema if exists foobar cascade;
31
+ create schema foobar
32
+ create table bar (id int);
33
+ create table public.bar (id int);
34
+ "
35
+ pg = PGconn.connect :dbname => 'pg-migrator'
36
+ pg.exec 'SET search_path TO foo, public'
37
+ migrator = PG::Migrator.new(pg)
38
+ migrator.reset
39
+ migrator.close
40
+ tables = @pg.exec "select schemaname, tablename from pg_tables where tablename = 'bar'"
41
+ assert_equal [['foobar', 'bar']], tables.values
42
+ @pg.exec 'drop schema if exists foobar cascade'
43
+ end
23
44
  end
24
45
 
25
46
  describe 'migrate' do
@@ -49,9 +70,6 @@ describe PG::Migrator do
49
70
  assert_includes tables.values.flatten, 'foo'
50
71
  end
51
72
 
52
- it 'executes sql files in order' do
53
- end
54
-
55
73
  it 'adds records to the migration table' do
56
74
  @migrator.migrate_up
57
75
  migrations = @pg.exec "select * from migration"
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pg-migrator
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.1
4
+ version: 1.2.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-10-28 00:00:00.000000000Z
12
+ date: 2011-11-04 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: pg
16
- requirement: &70324143022020 !ruby/object:Gem::Requirement
16
+ requirement: &70326906755800 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70324143022020
24
+ version_requirements: *70326906755800
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: minitest
27
- requirement: &70324143021600 !ruby/object:Gem::Requirement
27
+ requirement: &70326906755380 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,7 +32,7 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70324143021600
35
+ version_requirements: *70326906755380
36
36
  description: Rake migration tasks which utilities pure SQL files
37
37
  email:
38
38
  - carl.hoerberg@gmail.com
@@ -69,7 +69,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
69
  version: '0'
70
70
  requirements: []
71
71
  rubyforge_project:
72
- rubygems_version: 1.8.6
72
+ rubygems_version: 1.8.10
73
73
  signing_key:
74
74
  specification_version: 3
75
75
  summary: Drop raw SQL files in db/migrations and add require 'pg-migrator/tasks' to