pg-migrator 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'http://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in pg-migrator.gemspec
4
+ gemspec
data/README.md ADDED
@@ -0,0 +1,43 @@
1
+ PG Migrator
2
+ ===========
3
+
4
+ A simple migration, which runs pure Postgres SQL files.
5
+
6
+ Drop ```<timestamp>_desc.sql``` files in ```db/migrations``` (and corresponding ```<timestamp>_desc_down.sql``` files if you want to be able to downgrade).
7
+
8
+ Gemfile:
9
+
10
+ require 'pg-migrator/tasks'
11
+ # Make sure to have a DATABASE_URL environment variable
12
+ ENV['DATABASE_URL'] = "postgres://user:passwd@host/db"
13
+ # or if using sockets and default user
14
+ ENV['DATABASE_URL'] = "postgres:db"
15
+
16
+ Then as usually:
17
+
18
+ rake db:reset # drops the "public" schema in your database (not the database it self or any other schemas)
19
+ rake db:migrate:up # runs all migrations in order
20
+ rake db:migrate:down # runs the down migration for the latest applied migration
21
+
22
+ License
23
+ =======
24
+
25
+ Copyright (C) 2011 by Carl Hörberg
26
+
27
+ Permission is hereby granted, free of charge, to any person obtaining a copy
28
+ of this software and associated documentation files (the "Software"), to deal
29
+ in the Software without restriction, including without limitation the rights
30
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
31
+ copies of the Software, and to permit persons to whom the Software is
32
+ furnished to do so, subject to the following conditions:
33
+
34
+ The above copyright notice and this permission notice shall be included in
35
+ all copies or substantial portions of the Software.
36
+
37
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
38
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
39
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
40
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
41
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
42
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
43
+ THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ Rake::TestTask.new(:spec) do |t|
6
+ t.pattern = "spec/*_spec.rb"
7
+ end
@@ -0,0 +1,5 @@
1
+ module PG
2
+ class Migrator
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,61 @@
1
+ #encoding: utf-8
2
+ require_relative 'pg-migrator/version'
3
+ require 'pg'
4
+
5
+ module PG
6
+ class Migrator
7
+ def initialize(database_url, migrations_dir = './db/migrations')
8
+ @pg = PGconn.connect database_url
9
+ @pg.exec 'SET client_min_messages = warning'
10
+ @migrations_dir = migrations_dir
11
+ end
12
+
13
+ def close
14
+ @pg.finish
15
+ end
16
+
17
+ def reset
18
+ @pg.transaction do |conn|
19
+ conn.exec 'DROP SCHEMA public CASCADE'
20
+ conn.exec 'CREATE SCHEMA public'
21
+ end
22
+ migrate_up
23
+ end
24
+
25
+ def migrate_up
26
+ @pg.transaction do |conn|
27
+ conn.exec "CREATE TABLE IF NOT EXISTS migration (
28
+ id bigint PRIMARY KEY,
29
+ name varchar(255),
30
+ applied timestamp DEFAULT current_timestamp)
31
+ "
32
+ applied = conn.exec 'SELECT id, name FROM migration ORDER BY id'
33
+ Dir["#{@migrations_dir}/*.sql"].sort.each do |m|
34
+ next if m.end_with? '_down.sql'
35
+ id = m.sub /.*\/(\d+)_.*/, '\1'
36
+ name = m.sub /.*\/\d+_([^\.]*).sql$/, '\1'
37
+ next if applied.any? { |a| a['id'] == id }
38
+ puts "Applying #{File.basename m}"
39
+ conn.exec File.read m
40
+ conn.exec 'INSERT INTO migration (id, name) VALUES ($1, $2)', [id, name]
41
+ end
42
+ end
43
+ end
44
+
45
+ def migrate_down
46
+ @pg.transaction do |conn|
47
+ applied = conn.exec 'SELECT id, name FROM migration ORDER BY applied DESC LIMIT 1'
48
+ id = applied.first['id']
49
+ name = applied.first['name']
50
+ down_sql_file = "#{@migrations_dir}/#{id}_#{name}_down.sql"
51
+ unless File.exists? down_sql_file
52
+ puts "No down migration found for #{id} #{name} at #{down_sql_file}"
53
+ return
54
+ end
55
+ puts "Applying #{File.basename down_sql_file}"
56
+ conn.exec File.read down_sql_file
57
+ conn.exec "DELETE FROM migration WHERE id = $1", [id]
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,34 @@
1
+ require_relative '../pg-migrator'
2
+ require 'uri'
3
+
4
+ namespace :db do
5
+ task :setup do
6
+ uri = URI.parse ENV['DATABASE_URL']
7
+ if uri.opaque
8
+ @migrator = PG::Migrator.new {:dbname => uri.opaque}
9
+ else
10
+ @migrator = PG::Migrator.new(uri.host, uri.port, nil, nil, uri.path.sub(/\//, ''), uri.user, uri.password)
11
+ end
12
+ end
13
+
14
+ desc 'Drops the "public" schema and runs all the migrations'
15
+ task :reset => :setup do
16
+ @migrator.reset
17
+ end
18
+
19
+ desc 'Migrate up'
20
+ task :migrate => 'migrate:up'
21
+
22
+ namespace :migrate do
23
+ desc 'Migrate up'
24
+ task :up => :setup do
25
+ @migrator.migrate_up
26
+ end
27
+
28
+ desc 'Migrate down the latest applied migration'
29
+ task :down => :setup do
30
+ @migrator.migrate_down
31
+ end
32
+ end
33
+ end
34
+
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path('../lib/pg-migrator/version', __FILE__)
3
+
4
+ Gem::Specification.new do |gem|
5
+ gem.authors = ["Carl Hörberg"]
6
+ gem.email = ["carl.hoerberg@gmail.com"]
7
+ gem.description = %q{Simple PG Migrator}
8
+ gem.summary = %q{Drop raw sql files in db/migrations and add require 'pg-migrator/tasks' in your Rakefile}
9
+ gem.homepage = ""
10
+
11
+ gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ gem.files = `git ls-files`.split("\n")
13
+ gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
14
+ gem.name = "pg-migrator"
15
+ gem.require_paths = ["lib"]
16
+ gem.version = PG::Migrator::VERSION
17
+
18
+ gem.add_dependency 'pg'
19
+ gem.add_development_dependency 'minitest'
20
+
21
+ end
@@ -0,0 +1,80 @@
1
+ require 'minitest/autorun'
2
+ require './lib/pg-migrator'
3
+
4
+ describe PG::Migrator do
5
+ before do
6
+ @pg = PGconn.connect :dbname => 'pg-migrator'
7
+ @migrator = PG::Migrator.new({:dbname => 'pg-migrator'}, '/tmp')
8
+ end
9
+
10
+ describe 'reset' do
11
+ it 'deletes previous tables' do
12
+ @pg.exec 'create table bar (id int)'
13
+ @migrator.reset
14
+ tables = @pg.exec "select tablename from pg_tables where schemaname = 'public'"
15
+ refute_includes tables.values.flatten, 'bar'
16
+ end
17
+ end
18
+
19
+ describe 'migrate' do
20
+ before do
21
+ @migrator.reset
22
+ up = "create table foo (id int)"
23
+ down = "drop table foo"
24
+ File.open('/tmp/201109102230_init.sql', 'w') {|f| f.write up }
25
+ File.open('/tmp/201109102230_init_down.sql', 'w') {|f| f.write down }
26
+ end
27
+
28
+ after do
29
+ File.delete '/tmp/201109102230_init.sql'
30
+ File.delete '/tmp/201109102230_init_down.sql'
31
+ end
32
+
33
+ describe 'up' do
34
+ it 'adds migration table' do
35
+ @migrator.migrate_up
36
+ tables = @pg.exec "select tablename from pg_tables where schemaname = 'public'"
37
+ assert_includes tables.values.flatten, 'migration'
38
+ end
39
+
40
+ it 'execute sql files' do
41
+ @migrator.migrate_up
42
+ tables = @pg.exec "select tablename from pg_tables where schemaname = 'public'"
43
+ assert_includes tables.values.flatten, 'foo'
44
+ end
45
+
46
+ it 'executes sql files in order' do
47
+ end
48
+
49
+ it 'adds records to the migration table' do
50
+ @migrator.migrate_up
51
+ migrations = @pg.exec "select * from migration"
52
+ assert_equal 1, migrations.count
53
+ assert_equal 201109102230, migrations.first['id'].to_i
54
+ assert_equal 'init', migrations.first['name']
55
+ end
56
+ end
57
+
58
+ describe 'down' do
59
+
60
+ it 'executes sql-down files' do
61
+ @migrator.migrate_up
62
+ @migrator.migrate_down
63
+ tables = @pg.exec "select tablename from pg_tables where schemaname = 'public'"
64
+ refute_includes tables.values.flatten, 'foo'
65
+ end
66
+
67
+ it 'removes records from the migration table' do
68
+ @migrator.migrate_up
69
+ @migrator.migrate_down
70
+ migrations = @pg.exec "select * from migration"
71
+ assert_equal 0, migrations.count
72
+ end
73
+ end
74
+
75
+ after do
76
+ @pg.finish
77
+ @migrator.close
78
+ end
79
+ end
80
+ end
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pg-migrator
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Carl Hörberg
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-10-24 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: pg
16
+ requirement: &70158261673600 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *70158261673600
25
+ - !ruby/object:Gem::Dependency
26
+ name: minitest
27
+ requirement: &70158261673180 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *70158261673180
36
+ description: Simple PG Migrator
37
+ email:
38
+ - carl.hoerberg@gmail.com
39
+ executables: []
40
+ extensions: []
41
+ extra_rdoc_files: []
42
+ files:
43
+ - .gitignore
44
+ - Gemfile
45
+ - README.md
46
+ - Rakefile
47
+ - lib/pg-migrator.rb
48
+ - lib/pg-migrator/version.rb
49
+ - lib/tasks/pg-migrator.rake
50
+ - pg-migrator.gemspec
51
+ - spec/migrator_spec.rb
52
+ homepage: ''
53
+ licenses: []
54
+ post_install_message:
55
+ rdoc_options: []
56
+ require_paths:
57
+ - lib
58
+ required_ruby_version: !ruby/object:Gem::Requirement
59
+ none: false
60
+ requirements:
61
+ - - ! '>='
62
+ - !ruby/object:Gem::Version
63
+ version: '0'
64
+ required_rubygems_version: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ requirements: []
71
+ rubyforge_project:
72
+ rubygems_version: 1.8.6
73
+ signing_key:
74
+ specification_version: 3
75
+ summary: Drop raw sql files in db/migrations and add require 'pg-migrator/tasks' in
76
+ your Rakefile
77
+ test_files:
78
+ - spec/migrator_spec.rb