dbcode 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: 6921302d6cddedad8cd57765fe94e150adbe26ef
4
- data.tar.gz: e7ff40acb9567290c73f33bbede5208e4d4b72c5
3
+ metadata.gz: f21d02a2f9b86566f5b008c5399619466c8a10a9
4
+ data.tar.gz: 41685b4b0b1444f9edd6fcbae3a7a70cde544b6a
5
5
  SHA512:
6
- metadata.gz: 82c1d1eaf6bbbc9029819010f4d5d9b42609436299777997b6cd29dc2a95946f269c9e7a091a3da2405456afd8f67df580ce66ba7c9eb9159e583ad735cc6d12
7
- data.tar.gz: e33e20612fee946d2361b590748b985c1dfd3b7c5b798c0bb1631f9f5560b42cf38c2ff978dc19c7396a2c5695203df638cc1d3c88a43ba8030c0a6042cb477d
6
+ metadata.gz: 164a41e47a1627496298472e60acc00ea76e4252252ad2840d73d8dd166264d264f5af76c8529936a7a3a471a5122cf6cc89e20ff163bf841cd60d2377fd49ae
7
+ data.tar.gz: 5290e388c802eecdb868a33333458f1801183e32926edc0119607b18735a65d75e424ca4ae16f98e2315aef160aef696730735e40bf967611584afaed80d492a
data/README.md CHANGED
@@ -29,7 +29,7 @@ Writing drop statements or downward migrations is not necessary. All of your co
29
29
 
30
30
  #### Test
31
31
 
32
- dbcode ensures that the declarations in your test database are up to date. Any time you change a `db/code` file the changes will be available on your next test run. This happens automatically for tests that boot rails. If you have a test that integrates the database, but doesn't boot rails, call `DBCode.ensure_freshness!` in a before block.
32
+ dbcode ensures that the declarations in your test database are up to date. Any time you change a `db/code` file the changes will be available on your next test run. This happens automatically for tests that boot rails. If you have a test that integrates the database, but doesn't boot rails, call `DBCode.prepare` in a before block.
33
33
 
34
34
  #### Development
35
35
 
@@ -1,29 +1,73 @@
1
+ require 'logger'
2
+ require 'active_record'
3
+
1
4
  module DBCode
2
5
  autoload :SQLFile, 'dbcode/sql_file'
3
6
  autoload :Schema, 'dbcode/schema'
4
7
  autoload :Graph, 'dbcode/graph'
8
+ include ActiveRecord
9
+
5
10
  extend self
6
- attr_accessor :sql_file_path, :code_schema_name
7
- self.code_schema_name ||= 'code'
8
11
 
9
- def ensure_freshness!
10
- code = Schema.new connection: ActiveRecord::Base.connection, name: code_schema_name
12
+ def code_schema_name
13
+ @code_schema_name || 'code'
14
+ end
15
+
16
+ attr_writer :code_schema_name
17
+
18
+ def sql_file_path
19
+ @sql_file_path or raise "Configure sql file path. eg: #{self}.#{__method__} = Rails.root"
20
+ end
21
+
22
+ attr_writer :sql_file_path
23
+
24
+ def logger
25
+ @logger ||= Logger.new(STDOUT)
26
+ end
27
+
28
+ attr_writer :logger
29
+
30
+ def env
31
+ @env ||= 'development'
32
+ end
33
+
34
+ attr_writer :env
35
+
36
+ def prepare
37
+ code = Schema.new connection: Base.connection, name: code_schema_name
38
+ code.append_path!(Base.connection_config)
39
+
40
+ return if Migrator.needs_migration?
41
+
11
42
  code.within_schema do
12
- unless code.digest == graph.digest
43
+ graph = build_graph
44
+
45
+ if code.digest != graph.digest
46
+ if env == 'production'
47
+ return logger.error "[dbcode] out of date, but refusing to reset #{code.name} in production."
48
+ end
49
+ logger.warn "[dbcode] Resetting schema #{code.name}"
13
50
  code.reset!
14
51
  code.execute graph.to_sql
15
52
  code.digest = graph.digest
53
+ else
54
+ logger.info "[dbcode] Schema #{code.name} is up to date"
16
55
  end
17
56
  end
18
- code.append_path!(ActiveRecord::Base.connection_config)
19
57
  end
20
58
 
21
- def graph
22
- Graph.new file_names.sort.map &SQLFile.method(:new)
59
+ def build_graph
60
+ Graph.new files.map &SQLFile.method(:new)
23
61
  end
24
62
 
25
- def file_names
26
- Dir[sql_file_path.join('**/*.sql').expand_path]
63
+ def files
64
+ Dir[sql_file_path.join('**/*.sql').expand_path].sort.map do |file_name|
65
+ path = Pathname(file_name)
66
+ {
67
+ name: path.relative_path_from(sql_file_path).sub(/.sql$/,'').to_s,
68
+ contents: path.read
69
+ }
70
+ end
27
71
  end
28
72
  end
29
73
 
@@ -2,10 +2,12 @@ module DBCode
2
2
  class Railtie < Rails::Railtie
3
3
  initializer "dbcode.setup" do |app|
4
4
  DBCode.sql_file_path = app.root.join 'db/code'
5
+ DBCode.logger = Rails.logger
6
+ DBCode.env = Rails.env
5
7
  config.watchable_dirs[DBCode.sql_file_path.to_s] = ['sql']
6
8
  end
7
9
 
8
- config.to_prepare &DBCode.method(:ensure_freshness!)
10
+ config.to_prepare &DBCode.method(:prepare)
9
11
 
10
12
  rake_tasks do
11
13
  load 'dbcode/tasks/db_code.rake'
@@ -1,13 +1,9 @@
1
1
  module DBCode
2
2
  class SQLFile
3
- attr_reader :path
3
+ attr_reader :name, :contents
4
4
 
5
- def initialize(path)
6
- @path = Pathname(path)
7
- end
8
-
9
- def name
10
- path.basename('.sql').to_s
5
+ def initialize(name:, contents:)
6
+ @name, @contents = name, contents
11
7
  end
12
8
 
13
9
  def dependency_names
@@ -15,7 +11,7 @@ module DBCode
15
11
  end
16
12
 
17
13
  def to_sql
18
- @sql ||= path.read
14
+ @contents
19
15
  end
20
16
  end
21
17
  end
@@ -2,11 +2,11 @@ namespace :db do
2
2
  namespace :code do
3
3
  desc "sync the database code schema with the declaration files"
4
4
  task sync: :environment do
5
- DBCode.ensure_freshness!
5
+ DBCode.prepare
6
6
  end
7
7
  end
8
8
 
9
9
  task :migrate do
10
- DBCode.ensure_freshness!
10
+ DBCode.prepare
11
11
  end
12
12
  end
@@ -1,3 +1,3 @@
1
1
  module DBCode
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: dbcode
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
  - Brian Dunn