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 +4 -4
- data/README.md +1 -1
- data/lib/dbcode.rb +54 -10
- data/lib/dbcode/railtie.rb +3 -1
- data/lib/dbcode/sql_file.rb +4 -8
- data/lib/dbcode/tasks/db_code.rake +2 -2
- data/lib/dbcode/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f21d02a2f9b86566f5b008c5399619466c8a10a9
|
4
|
+
data.tar.gz: 41685b4b0b1444f9edd6fcbae3a7a70cde544b6a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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.
|
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
|
|
data/lib/dbcode.rb
CHANGED
@@ -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
|
10
|
-
|
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
|
-
|
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
|
22
|
-
Graph.new
|
59
|
+
def build_graph
|
60
|
+
Graph.new files.map &SQLFile.method(:new)
|
23
61
|
end
|
24
62
|
|
25
|
-
def
|
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
|
|
data/lib/dbcode/railtie.rb
CHANGED
@@ -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(:
|
10
|
+
config.to_prepare &DBCode.method(:prepare)
|
9
11
|
|
10
12
|
rake_tasks do
|
11
13
|
load 'dbcode/tasks/db_code.rake'
|
data/lib/dbcode/sql_file.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
1
|
module DBCode
|
2
2
|
class SQLFile
|
3
|
-
attr_reader :
|
3
|
+
attr_reader :name, :contents
|
4
4
|
|
5
|
-
def initialize(
|
6
|
-
@
|
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
|
-
@
|
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.
|
5
|
+
DBCode.prepare
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
9
|
task :migrate do
|
10
|
-
DBCode.
|
10
|
+
DBCode.prepare
|
11
11
|
end
|
12
12
|
end
|
data/lib/dbcode/version.rb
CHANGED