prick 0.18.0 → 0.20.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (112) hide show
  1. checksums.yaml +4 -4
  2. data/Gemfile +10 -4
  3. data/README.md +7 -7
  4. data/Rakefile +3 -1
  5. data/TODO +13 -11
  6. data/bin/console +2 -1
  7. data/doc/build-yml.txt +14 -0
  8. data/exe/prick +264 -28
  9. data/lib/builder/batch.rb +147 -0
  10. data/lib/builder/builder.rb +122 -0
  11. data/lib/builder/node.rb +189 -0
  12. data/lib/builder/node_pool.rb +105 -0
  13. data/lib/builder/parser.rb +120 -0
  14. data/lib/local/command.rb +193 -0
  15. data/lib/{prick → local}/git.rb +148 -22
  16. data/lib/local/timer.rb +98 -0
  17. data/lib/prick/constants.rb +54 -66
  18. data/lib/prick/diff.rb +28 -18
  19. data/lib/prick/prick_version.rb +161 -0
  20. data/lib/prick/state.rb +80 -165
  21. data/lib/prick/version.rb +2 -163
  22. data/lib/prick.rb +43 -27
  23. data/lib/share/init/.gitignore +10 -0
  24. data/lib/share/init/.prick-context +2 -0
  25. data/lib/share/init/.rspec +3 -0
  26. data/{share/schema/schema/public → lib/share/init/migration}/.keep +0 -0
  27. data/lib/share/init/prick.yml +6 -0
  28. data/lib/share/init/schema/.keep +0 -0
  29. data/lib/share/init/schema/build.yml +2 -0
  30. data/lib/share/init/schema/prick/.keep +0 -0
  31. data/lib/share/init/schema/prick/build.yml +5 -0
  32. data/lib/share/init/schema/prick/data.sql +6 -0
  33. data/{share/schema → lib/share/init}/schema/prick/tables.sql +2 -3
  34. data/lib/share/init/schema/public/.keep +0 -0
  35. data/lib/share/init/spec/prick_helper.rb +1 -0
  36. data/lib/share/init/spec/prick_spec.rb +6 -0
  37. data/lib/share/init/spec/spec_helper.rb +50 -0
  38. data/lib/share/migrate/migration/build.yml +4 -0
  39. data/lib/share/migrate/migration/diff.after-tables.sql +0 -0
  40. data/lib/share/migrate/migration/diff.before-tables.sql +0 -0
  41. data/lib/share/migrate/migration/diff.tables.sql +0 -0
  42. data/lib/subcommand/prick-build.rb +55 -0
  43. data/lib/subcommand/prick-create.rb +78 -0
  44. data/lib/subcommand/prick-drop.rb +25 -0
  45. data/lib/subcommand/prick-fox.rb +62 -0
  46. data/lib/subcommand/prick-init.rb +46 -0
  47. data/lib/subcommand/prick-make.rb +202 -0
  48. data/lib/subcommand/prick-migrate.rb +37 -0
  49. data/lib/subcommand/prick-release.rb +23 -0
  50. data/lib/subcommand/prick-setup.rb +20 -0
  51. data/lib/subcommand/prick-teardown.rb +18 -0
  52. data/prick.gemspec +43 -16
  53. metadata +161 -72
  54. data/.gitignore +0 -29
  55. data/.travis.yml +0 -7
  56. data/doc/create_release.txt +0 -17
  57. data/doc/flow.txt +0 -98
  58. data/doc/migra +0 -1
  59. data/doc/migrations.txt +0 -172
  60. data/doc/notes.txt +0 -116
  61. data/doc/prick.txt +0 -114
  62. data/doc/sh.prick +0 -316
  63. data/lib/ext/algorithm.rb +0 -14
  64. data/lib/ext/fileutils.rb +0 -26
  65. data/lib/ext/forward_method.rb +0 -18
  66. data/lib/ext/pg.rb +0 -18
  67. data/lib/ext/shortest_path.rb +0 -44
  68. data/lib/prick/archive.rb +0 -124
  69. data/lib/prick/branch.rb +0 -254
  70. data/lib/prick/builder.rb +0 -205
  71. data/lib/prick/cache.rb +0 -34
  72. data/lib/prick/command.rb +0 -102
  73. data/lib/prick/database.rb +0 -82
  74. data/lib/prick/dsort.rb +0 -151
  75. data/lib/prick/ensure.rb +0 -119
  76. data/lib/prick/exceptions.rb +0 -25
  77. data/lib/prick/head.rb +0 -183
  78. data/lib/prick/migration.rb +0 -70
  79. data/lib/prick/program.rb +0 -506
  80. data/lib/prick/project.rb +0 -626
  81. data/lib/prick/rdbms.rb +0 -137
  82. data/lib/prick/schema.rb +0 -27
  83. data/lib/prick/share.rb +0 -64
  84. data/libexec/strip-comments +0 -33
  85. data/make_releases +0 -72
  86. data/make_schema +0 -10
  87. data/share/diff/diff.after-tables.sql +0 -4
  88. data/share/diff/diff.before-tables.sql +0 -4
  89. data/share/diff/diff.tables.sql +0 -8
  90. data/share/features/diff.sql +0 -2
  91. data/share/features/feature/diff.sql +0 -2
  92. data/share/features/feature/migrate.sql +0 -2
  93. data/share/features/features.sql +0 -2
  94. data/share/features/features.yml +0 -2
  95. data/share/features/migrations.sql +0 -4
  96. data/share/gitignore +0 -2
  97. data/share/migration/diff.tables.sql +0 -8
  98. data/share/migration/features.yml +0 -6
  99. data/share/migration/migrate.sql +0 -3
  100. data/share/migration/migrate.yml +0 -8
  101. data/share/migration/tables.sql +0 -3
  102. data/share/schema/build.yml +0 -14
  103. data/share/schema/schema/build.yml +0 -3
  104. data/share/schema/schema/prick/build.yml +0 -14
  105. data/share/schema/schema/prick/data.sql +0 -7
  106. data/share/schema/schema/prick/schema.sql +0 -3
  107. data/share/schema/schema/public/build.yml +0 -13
  108. data/share/schema/schema.sql +0 -3
  109. data/test_assorted +0 -192
  110. data/test_feature +0 -112
  111. data/test_refactor +0 -34
  112. data/test_single_dev +0 -83
data/lib/prick.rb CHANGED
@@ -1,31 +1,47 @@
1
+ require 'prick/version.rb'
1
2
 
2
- #$LOAD_PATH.unshift("/home/clr/prj/shellopts/lib")
3
-
4
- # 'semantic' is required here instead of in prick/version.rb to avoid having Gem depend on it
5
- require "semantic"
6
-
7
- # Needs to go first because it contains class-level methods that is used by other modules
8
- require "ext/forward_method.rb"
9
-
10
- require "prick/builder.rb"
11
- require "prick/migration.rb"
12
- require "prick/cache.rb"
13
- require "prick/command.rb"
14
- require "prick/constants.rb"
15
- require "prick/database.rb"
16
- require "prick/diff.rb"
17
- require "prick/exceptions.rb"
18
- require "prick/git.rb"
19
- require "prick/head.rb"
20
- require "prick/project.rb"
21
- require "prick/rdbms.rb"
22
- require "prick/schema.rb"
23
- require "prick/share.rb"
24
- require "prick/state.rb"
25
- require "prick/version.rb"
26
-
27
- require "ext/fileutils.rb"
28
- require "ext/shortest_path.rb"
3
+ require 'pg_conn'
4
+
5
+ require 'fixture_fox'
6
+ require 'indented_io'
7
+ require 'constrain'
8
+
9
+ module Prick
10
+ class Error < RuntimeError; end
11
+ class Fail < RuntimeError; end
12
+ end
13
+
14
+ require 'prick/constants.rb'
15
+
16
+ require 'local/command.rb'
17
+ require 'local/git.rb'
18
+ require 'local/timer.rb'
19
+
20
+ require 'prick/state.rb'
21
+ require 'prick/prick_version.rb'
22
+ require 'prick/diff.rb'
23
+
24
+ module Prick
25
+ module SubCommand
26
+ extend Timer
27
+ def self.mesg(*msgs) puts msgs.join(" ") if !$quiet end
28
+ def self.verb(*msgs) puts msgs.join(" ") if $verbose && !$quiet end
29
+ end
30
+ end
31
+
32
+ require 'subcommand/prick-init.rb'
33
+ require 'subcommand/prick-setup.rb'
34
+ require 'subcommand/prick-teardown.rb'
35
+ require 'subcommand/prick-create.rb'
36
+ require 'subcommand/prick-build.rb'
37
+ require 'subcommand/prick-make.rb'
38
+ require 'subcommand/prick-fox.rb'
39
+ require 'subcommand/prick-drop.rb'
40
+ require 'subcommand/prick-release.rb'
41
+ require 'subcommand/prick-migrate.rb'
29
42
 
30
43
  module Prick
44
+ @state = nil
45
+ def self.state() @state end
46
+ def self.state=(state) @state = state end
31
47
  end
@@ -0,0 +1,10 @@
1
+
2
+ # Local database and environment
3
+ /.prick-context
4
+
5
+ # Fox state file
6
+ /.fox-state.yml
7
+
8
+ # rspec failure tracking
9
+ .rspec_status
10
+
@@ -0,0 +1,2 @@
1
+ database:
2
+ username:
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,6 @@
1
+ name:
2
+ title:
3
+ database:
4
+ username:
5
+ version:
6
+ prick:
File without changes
@@ -0,0 +1,2 @@
1
+ ---
2
+ - prick/
File without changes
@@ -0,0 +1,5 @@
1
+ ---
2
+ - schema: prick
3
+ - tables.sql
4
+ - seed:
5
+ - data.sql
@@ -0,0 +1,6 @@
1
+ --
2
+ -- This file is auto-generated by prick(1). Please don't touch
3
+ --
4
+ delete from prick.versions;
5
+ insert into prick.versions (fork, major, minor, patch, pre, feature, version)
6
+ values (null, 0, 0, 0, null, null, '0.0.0');
@@ -1,4 +1,3 @@
1
-
2
1
  -- Controlled by prick(1). Don't touch
3
2
  --
4
3
 
@@ -12,6 +11,6 @@ create table versions (
12
11
  patch integer not null,
13
12
  pre integer,
14
13
  feature varchar,
15
- version varchar not null
14
+ version varchar not null,
15
+ built_at timestamp without time zone not null default (now() at time zone 'UTC')
16
16
  );
17
-
File without changes
@@ -0,0 +1 @@
1
+ require 'prick/constants.rb'
@@ -0,0 +1,6 @@
1
+
2
+ describe "PRICK" do
3
+ it "requires that project version is equal to schema version"
4
+ it "requires that project version is equal to database version"
5
+ end
6
+
@@ -0,0 +1,50 @@
1
+
2
+ # Set up paths to fox-project libraries
3
+ libdirs = `fox-project-libdirs 2>/dev/null`.chomp.split(":")
4
+ if !libdirs.empty?
5
+ $LOAD_PATH.unshift(*libdirs)
6
+ end
7
+
8
+ require "bundler/setup"
9
+
10
+ RSpec.configure do |config|
11
+ # Enable flags like --only-failures and --next-failure
12
+ config.example_status_persistence_file_path = ".rspec_status"
13
+
14
+ # Disable RSpec exposing methods globally on `Module` and `main`
15
+ # config.disable_monkey_patching!
16
+
17
+ config.expect_with :rspec do |c|
18
+ c.syntax = :expect
19
+ end
20
+ end
21
+
22
+ # Silence most of the output from pending tests. See https://github.com/rspec/rspec-core/issues/2377
23
+ module FormatterOverrides
24
+ def example_pending(n)
25
+ colorizer=::RSpec::Core::Formatters::ConsoleCodes
26
+ output << current_indentation \
27
+ << colorizer.wrap(n.example.description, :pending) \
28
+ << "\n"
29
+ end
30
+
31
+ def dump_pending(_)
32
+ end
33
+ end
34
+
35
+ RSpec::Core::Formatters::DocumentationFormatter.prepend FormatterOverrides
36
+
37
+ # Postspec
38
+ require "postspec"
39
+ require 'postspec_helper'
40
+ require 'prick_helper'
41
+
42
+ Postspec.configure do |config|
43
+ config.database = "mikras"
44
+ # config.anchors = "..."
45
+ config.mode = :seed
46
+ config.reflections = Prick::REFLECTIONS_PATH if File.exist?(Prick::REFLECTIONS_PATH)
47
+ end
48
+
49
+ # Local modifications
50
+
@@ -0,0 +1,4 @@
1
+ ---
2
+ - diff.before-tables.sql
3
+ - diff.tables.sql
4
+ - diff.after-tables.sql
File without changes
File without changes
@@ -0,0 +1,55 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'builder/builder.rb'
4
+
5
+ module Prick::SubCommand
6
+ def self.build(database, username, schema, builddir: "schema", timer: nil, dump: nil)
7
+ Timer.on! if timer
8
+ time "Prick::Command#build" do
9
+ begin
10
+ super_conn = PgConn.new
11
+ conn = nil
12
+ builder = nil
13
+
14
+ time "Load build object" do
15
+ if super_conn.rdbms.exist? database
16
+ conn = PgConn.new(database, username)
17
+ super_conn.rdbms.empty!(database)
18
+ else
19
+ super_conn.rdbms.create database, owner: username
20
+ conn = PgConn.new(database, username)
21
+ end
22
+
23
+ builder = Prick::Build::Builder.new(conn, builddir)
24
+ builder.pool.delete_schema(builder.pool.after_schema(schema)) if schema
25
+ end
26
+
27
+ case dump
28
+ when :nodes; builder.nodes.reject { |node| node.is_a?(Build::BuildNode) }.map &:dump
29
+ when :allnodes; builder.nodes.map &:dump
30
+ when :batches; builder.dump
31
+ when nil;
32
+ else
33
+ raise Prick::Error, "Illegal dump type: #{dump.inspect}"
34
+ end && exit
35
+
36
+ time "Execute build object" do
37
+ builder.execute conn
38
+ end
39
+
40
+ rescue Prick::Error => ex
41
+ $stderr.puts ex.message
42
+ exit 1
43
+
44
+ rescue ::Command::Error => ex
45
+ $stderr.puts ex.message
46
+ exit 1
47
+
48
+ ensure
49
+ super_conn&.terminate
50
+ conn&.terminate
51
+ end
52
+ end
53
+ end
54
+ end
55
+
@@ -0,0 +1,78 @@
1
+
2
+ module Prick
3
+ module SubCommand
4
+ def self.create_migration(username, from_version, force: false, file: nil)
5
+ constrain from_version, PrickVersion
6
+ Git.clean? or raise "Won't migrate: Repository is dirty"
7
+ Git.synchronized? or raise "Won't migrate: Repository is not synchronized"
8
+
9
+ from_version != PrickVersion.zero or raise Prick::Error, "Can't migrate from release 0.0.0"
10
+ to_version = Prick.state.version
11
+ migration_dir = "#{MIGRATION_DIR}/#{from_version}"
12
+ migration_exist = File.directory? migration_dir
13
+ force || file || !migration_exist or raise Prick::Fail, "Migration #{from_version} exists"
14
+
15
+ $quiet = true if file
16
+
17
+ if file && migration_exist
18
+ File.open(file, "w") { |file|
19
+ for diff_file in DIFF_FILES
20
+ file.write File.read("#{migration_dir}/#{diff_file}")
21
+ end
22
+ }
23
+ else
24
+ from_version != to_version or raise Prick::Fail, "Can't migrate to same release"
25
+ from_version < to_version or raise Prick::Fail, "Can't migrate backwards (why not?)"
26
+
27
+ diff = nil
28
+ if force || !migration_exist
29
+ from_db = "#{Prick.state.name}-#{from_version}"
30
+ to_db = "#{Prick.state.name}-#{to_version}"
31
+
32
+ mesg "Migrating from #{from_version} to #{to_version}"
33
+ begin
34
+ origin = Git.origin
35
+
36
+ # Local repos are supported to ease testing
37
+ if File.directory?(origin)
38
+ origin = File.expand_path(origin)
39
+ end
40
+
41
+ diff = Dir.mktmpdir { |tmpdir|
42
+ Dir.chdir(tmpdir) {
43
+ mesg " Building #{from_db}"
44
+ Git.clone(origin, from_version, branch: from_version)
45
+ Dir.chdir(from_version.to_s) { build(from_db, username, nil) }
46
+ }
47
+
48
+ mesg " Building #{to_db}"
49
+ build(to_db, username, nil)
50
+
51
+ mesg " Creating diff"
52
+ Diff.new(from_db, to_db)
53
+ }
54
+ !diff.same? or raise Prick::Fail, "No changes"
55
+ ensure
56
+ drop_all(from_db)
57
+ drop_all(to_db)
58
+ end
59
+ end
60
+
61
+ diff.write(file) if file
62
+ end
63
+
64
+ if file
65
+ File.open(file, "a") { |f|
66
+ f.puts "-- UPDATE VERSION"
67
+ f.puts File.readlines(SCHEMA_VERSION_PATH).grep_v(/^--/)
68
+ }
69
+ else
70
+ FileUtils.rm_rf migration_dir if force
71
+ FileUtils.mkdir_p migration_dir
72
+ Command.command "cp -a #{SHARE_PATH}/migrate/migration/. #{migration_dir}"
73
+ Dir.chdir(migration_dir) { diff.write(*DIFF_FILES, mark: true) }
74
+ end
75
+ end
76
+ end
77
+ end
78
+
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'builder/builder.rb'
4
+
5
+ module Prick::SubCommand
6
+ def self.drop_users(database)
7
+ PgConn.new(database) { |conn|
8
+ users = conn.role.list(database: database)
9
+ conn.role.drop(users, cascade: true)
10
+ }
11
+ end
12
+
13
+ def self.drop_database(database)
14
+ PgConn.new { |conn| conn.rdbms.drop database }
15
+ end
16
+
17
+ def self.drop_all(database)
18
+ PgConn.new { |conn|
19
+ users = conn.role.list(database: database)
20
+ PgConn.new(database) { |db| db.role.drop(users, cascade: true) }
21
+ conn.rdbms.drop database
22
+ }
23
+ end
24
+ end
25
+
@@ -0,0 +1,62 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'builder/builder.rb'
4
+
5
+ module Prick::SubCommand
6
+ def self.fox(database, username, files)
7
+ Command.command "fox --state=#{FOX_STATE_PATH} --exec #{database} #{files.join(" ")}"
8
+ end
9
+ end
10
+
11
+ __END__
12
+
13
+
14
+ Timer.on! if timer
15
+ time "Prick::Command#build" do
16
+ begin
17
+ super_conn = PgConn.new
18
+ conn = nil
19
+ builder = nil
20
+
21
+ time "Load build object" do
22
+ if super_conn.rdbms.exist? database
23
+ conn = PgConn.new(database, username)
24
+ super_conn.rdbms.empty!(database)
25
+ else
26
+ super_conn.rdbms.create database, owner: username
27
+ conn = PgConn.new(database, username)
28
+ end
29
+
30
+ builder = Prick::Build::Builder.new(conn, builddir)
31
+ builder.pool.delete_schema(builder.pool.after_schema(schema)) if schema
32
+ end
33
+
34
+ case dump
35
+ when :nodes; builder.nodes.reject { |node| node.is_a?(Build::BuildNode) }.map &:dump
36
+ when :allnodes; builder.nodes.map &:dump
37
+ when :batches; builder.dump
38
+ when nil;
39
+ else
40
+ raise Prick::Error, "Illegal dump type: #{dump.inspect}"
41
+ end && exit
42
+
43
+ time "Execute build object" do
44
+ builder.execute conn
45
+ end
46
+
47
+ rescue Prick::Error => ex
48
+ $stderr.puts ex.message
49
+ exit 1
50
+
51
+ rescue ::Command::Error => ex
52
+ $stderr.puts ex.message
53
+ exit 1
54
+
55
+ ensure
56
+ super_conn&.terminate
57
+ conn&.terminate
58
+ end
59
+ end
60
+ end
61
+ end
62
+
@@ -0,0 +1,46 @@
1
+ require 'fileutils'
2
+
3
+ module Prick::SubCommand
4
+ def self.init(dir, name, title, database, username)
5
+ if dir
6
+ !File.exist?(dir) or raise Prick::Error, "Directory #{dir} exists"
7
+ FileUtils.mkdir_p(dir)
8
+ Dir.chdir(dir)
9
+ else
10
+ dir = "."
11
+ end
12
+ name ||= File.basename(Dir.getwd)
13
+ title ||= name
14
+ database ||= name
15
+ username ||= name
16
+
17
+ Command.command %(
18
+ git init .
19
+ cp -a #{SHARE_PATH}/init/. .
20
+ git add .
21
+ git commit -am "Initial import"
22
+ ), fail: false
23
+
24
+ Command.status == 0 or raise Prick::Fail, "Init script failed"
25
+
26
+ state = State.new
27
+ state.name = name
28
+ state.title = title
29
+ state.prick_version = PrickVersion.new VERSION
30
+ state.version = PrickVersion.new("0.0.0")
31
+ state.environment = :development
32
+ state.database = database
33
+ state.username = username
34
+ state.save
35
+
36
+ Command.command %(
37
+ set -e
38
+ git commit -am "Release 0.0.0"
39
+ git tag v0.0.0
40
+ ), fail: false
41
+
42
+ Command.status == 0 or raise Prick::Fail, "Init script failed"
43
+
44
+ [dir, state]
45
+ end
46
+ end