communard 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 93fe86a1450877912e94bf0d8d6c61d6994d0731
4
- data.tar.gz: 8b41d0725025b0901e1c574f3be1196c7441219c
3
+ metadata.gz: 254deb5e41f164f82d78f8e790607c3043914a54
4
+ data.tar.gz: b06a55879156c64e6118a3d02f068719d0c94770
5
5
  SHA512:
6
- metadata.gz: 3725a5b59c63946c9532e4f189719590404faad6e2df3584f66c6d5673ae9d8e1f75fa895dc4c39f5c970f9628f3cb72af0fecae98284ade648f6ba965b1dd87
7
- data.tar.gz: 300f1d6b8033c35ab87101581d499ba93102e090fcda8774c7817e01b4e3908b9d186332a054009bd0b9c49a0a71f9a2aa20b170392d1fb34592d27da447d6d7
6
+ metadata.gz: 18d3919f1171c3285de8f6b93e7c2c07659ac99bec515c093dd4b93513c28292407b39573bff10baae2a9951817a238a1ce5923b26dd6e091a5030b8ad409773
7
+ data.tar.gz: 9e466e1fba88680672d88289dc5493aaa5bd8caf7ced131582025b9ec2f3eaa266f6767d2b185214cb68bd39a1a6cffc8dbe2f9639ffdde13e77ed9573e7db4a
@@ -4,15 +4,20 @@ require "pathname"
4
4
  module Communard
5
5
  class Configuration
6
6
 
7
- attr_accessor :environment, :root, :logger
7
+ attr_accessor :environment, :root, :logger, :dump_same_db
8
8
 
9
9
  def initialize
10
- self.environment = ENV["RACK_ENV"] || ENV["RUBY_ENV"] || ENV["RACK_ENV"] || "development"
11
- self.root = Pathname(Dir.pwd)
12
- self.logger = default_logger
10
+ self.environment = ENV["RACK_ENV"] || ENV["RUBY_ENV"] || ENV["RACK_ENV"] || "development"
11
+ self.root = Pathname(Dir.pwd)
12
+ self.logger = default_logger
13
+ self.dump_same_db = false
13
14
  yield self if block_given?
14
15
  end
15
16
 
17
+ def dump_same_db!
18
+ self.dump_same_db = true
19
+ end
20
+
16
21
  private
17
22
 
18
23
  def default_logger
@@ -30,7 +30,13 @@ module Communard
30
30
  end
31
31
 
32
32
  def create_database(env: environment)
33
- run_without_database("CREATE DATABASE IF NOT EXISTS %{database_name}", env: env)
33
+ run_without_database("CREATE DATABASE %{database_name}", env: env)
34
+ rescue Sequel::DatabaseError => error
35
+ if /database (.*) already exists/ === error.message
36
+ logger.info "Database #{$1} already exists, which is fine."
37
+ else
38
+ raise
39
+ end
34
40
  end
35
41
 
36
42
  def drop_database(env: environment)
@@ -39,7 +45,7 @@ module Communard
39
45
  end
40
46
 
41
47
  def migrate(target: nil, env: environment)
42
- maintenance(env: env).migrate(target: target)
48
+ maintenance(env: env).migrate(target: target, dump_same_db: configuration.dump_same_db)
43
49
  end
44
50
 
45
51
  def seed(env: environment)
@@ -47,7 +53,7 @@ module Communard
47
53
  end
48
54
 
49
55
  def rollback(step: 1, env: environment)
50
- maintenance(env: env).rollback(step: step)
56
+ maintenance(env: env).rollback(step: step, dump_same_db: configuration.dump_same_db)
51
57
  end
52
58
 
53
59
  def load_schema(env: environment)
@@ -55,7 +61,7 @@ module Communard
55
61
  end
56
62
 
57
63
  def dump_schema(env: environment)
58
- maintenance(env: env).dump_schema
64
+ maintenance(env: env).dump_schema(dump_same_db: configuration.dump_same_db)
59
65
  end
60
66
 
61
67
  def status(env: environment)
@@ -65,6 +71,10 @@ module Communard
65
71
  def run_without_database(cmd, env: environment)
66
72
  opts = options(env: env).dup
67
73
  database_name = opts.delete("database")
74
+ if opts.fetch("adapter") == "postgres"
75
+ opts["database"] = "postgres"
76
+ opts["schema_search_path"] = "public"
77
+ end
68
78
  connection = connect(opts)
69
79
  connection.run(cmd % { database_name: database_name })
70
80
  end
@@ -9,20 +9,20 @@ module Communard
9
9
  @root = root
10
10
  end
11
11
 
12
- def migrate(target: nil)
12
+ def migrate(target: nil, dump_same_db: false)
13
13
  target = Integer(target) if target
14
14
  ::Sequel::Migrator.run(connection, migrations, target: target, allow_missing_migration_files: true)
15
- dump_schema
15
+ dump_schema(dump_same_db: dump_same_db)
16
16
  end
17
17
 
18
18
  def seed
19
19
  load seeds_file if seeds_file.exist?
20
20
  end
21
21
 
22
- def rollback(step: 1)
22
+ def rollback(step: 1, dump_same_db: false)
23
23
  target = applied_migrations[-step - 1]
24
24
  if target
25
- migrate(target: target.split(/_/, 2).first)
25
+ migrate(target: target.split(/_/, 2).first, dump_same_db: dump_same_db)
26
26
  else
27
27
  fail ArgumentError, "Cannot roll back that far"
28
28
  end
@@ -33,9 +33,9 @@ module Communard
33
33
  migration.apply(connection, :up)
34
34
  end
35
35
 
36
- def dump_schema
36
+ def dump_schema(dump_same_db: false)
37
37
  connection.extension :schema_dumper
38
- schema = connection.dump_schema_migration(same_db: false)
38
+ schema = connection.dump_schema_migration(same_db: dump_same_db)
39
39
  schema_file.open("w") { |f| f << schema.gsub(/\s+$/m, "") }
40
40
  end
41
41
 
@@ -8,43 +8,47 @@ module Communard
8
8
  def self.add_tasks(ns = :db, &block)
9
9
 
10
10
  namespace ns do
11
+ task :_load_communard do
12
+ @_communard_context = Communard.context(&block)
13
+ end
14
+
11
15
  desc "Creates the database, migrate the schema, and loads seed data"
12
16
  task :setup => [:create, :migrate, :seed, "db:test:prepare"]
13
17
 
14
18
  desc "Creates the database"
15
- task :create do
16
- Communard.context(&block).create_database
19
+ task :create => :_load_communard do
20
+ @_communard_context.create_database
17
21
  end
18
22
 
19
23
  desc "Drops the database"
20
- task :drop do
21
- Communard.context(&block).drop_database
24
+ task :drop => :_load_communard do
25
+ @_communard_context.drop_database
22
26
  end
23
27
 
24
28
  desc "Drops and creates the database"
25
29
  task :reset => [:drop, :setup]
26
30
 
27
31
  desc "Migrate the database"
28
- task :migrate do
32
+ task :migrate => :_load_communard do
29
33
  target = ENV["VERSION"] || ENV["TARGET"]
30
- Communard.context(&block).migrate(target: target)
34
+ @_communard_context.migrate(target: target)
31
35
  end
32
36
 
33
37
  desc "Load the seed data from db/seeds.rb"
34
- task :seed do
35
- Communard.context(&block).seed
38
+ task :seed => :_load_communard do
39
+ @_communard_context.seed
36
40
  end
37
41
 
38
42
  desc "Rolls the schema back to the previous version"
39
- task :rollback do
43
+ task :rollback => :_load_communard do
40
44
  step = Integer(ENV["STEP"] || 1)
41
- Communard.context(&block).rollback(step: step)
45
+ @_communard_context.rollback(step: step)
42
46
  end
43
47
 
44
48
  namespace :test do
45
49
  desc "Cleans the test database"
46
- task :prepare do
47
- context = Communard.context(&block)
50
+ task :prepare => :_load_communard do
51
+ context = @_communard_context
48
52
  context.drop_database(env: "test")
49
53
  context.create_database(env: "test")
50
54
  context.migrate(env: "test")
@@ -54,28 +58,28 @@ module Communard
54
58
  namespace :migrate do
55
59
 
56
60
  desc "Redo the last migration"
57
- task :redo do
58
- context = Communard.context(&block)
61
+ task :redo => :_load_communard do
62
+ context = @_communard_context
59
63
  context.rollback
60
64
  context.migrate
61
65
  end
62
66
 
63
67
  desc "Display status of migrations"
64
- task :status do
65
- Communard.context(&block).status
68
+ task :status, :_load_communard do
69
+ @_communard_context.status
66
70
  end
67
71
  end
68
72
 
69
73
  namespace :schema do
70
74
 
71
75
  desc "Load the schema from db/schema.rb"
72
- task :load do
73
- Communard.context(&block).load_schema
76
+ task :load => :_load_communard do
77
+ @_communard_context.load_schema
74
78
  end
75
79
 
76
80
  desc "Dumps the schema to db/schema.rb"
77
- task :dump do
78
- Communard.context(&block).dump_schema
81
+ task :dump => :_load_communard do
82
+ @_communard_context.dump_schema
79
83
  end
80
84
 
81
85
  end
@@ -1,3 +1,3 @@
1
1
  module Communard
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: communard
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - iain
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-20 00:00:00.000000000 Z
11
+ date: 2014-10-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: sequel