Class: Prick::Schema
- Inherits:
-
Object
- Object
- Prick::Schema
- Defined in:
- lib/prick/schema.rb
Overview
TODO: Resolve dependencies by extracting i includes and then execute them in topological order. This means the must be no ‘if’ constructs in the files because otherwise dependencies are not static. The rule is that if a file i includes another file, then the included file can always be execute before the current file
Alternatively add some special tags:
– require prick (meaning include the prick schema) – require public/user-schema (include this file)
Instance Attribute Summary collapse
-
#project ⇒ Object
readonly
Enclosing project.
Class Method Summary collapse
-
.data_file ⇒ Object
Path to data file.
Instance Method Summary collapse
-
#build(database = project.database) ⇒ Object
Build a release from the files in schemas/.
- #built?(database = project.database) ⇒ Boolean
-
#collect(filename) ⇒ Object
Collects instances of ‘filename` in sub-directories of schemas/.
- #data_file ⇒ Object
-
#initialize(project) ⇒ Schema
constructor
A new instance of Schema.
-
#path ⇒ Object
Path to the schemas directory.
-
#version ⇒ Object
Version read from schemas/prick/data.sql.
-
#version=(version) ⇒ Object
Write version number into schemas/prick/data.sql.
Constructor Details
#initialize(project) ⇒ Schema
Returns a new instance of Schema.
63 64 65 |
# File 'lib/prick/schema.rb', line 63 def initialize(project) @project = project end |
Instance Attribute Details
#project ⇒ Object (readonly)
Enclosing project
18 19 20 |
# File 'lib/prick/schema.rb', line 18 def project @project end |
Class Method Details
.data_file ⇒ Object
Path to data file
21 |
# File 'lib/prick/schema.rb', line 21 def Schema.data_file() DATA_SQL_PATH end |
Instance Method Details
#build(database = project.database) ⇒ Object
Build a release from the files in schemas/
75 76 77 78 79 80 |
# File 'lib/prick/schema.rb', line 75 def build(database = project.database) files = collect("schema.sql") + collect("data.sql") Dir.chdir(path) { files.each { |file| Rdbms.exec_file(database.name, file, user: project.user) } } end |
#built?(database = project.database) ⇒ Boolean
70 71 72 |
# File 'lib/prick/schema.rb', line 70 def built?(database = project.database) database.exist? && database.version == version end |
#collect(filename) ⇒ Object
Collects instances of ‘filename` in sub-directories of schemas/
83 84 85 86 87 88 89 90 91 |
# File 'lib/prick/schema.rb', line 83 def collect(filename) Dir.chdir(path) { if File.exist?(filename) [filename] else Dir["*/#{filename}"] end } end |
#data_file ⇒ Object
22 |
# File 'lib/prick/schema.rb', line 22 def data_file() Schema.data_file end |
#path ⇒ Object
Path to the schemas directory
68 |
# File 'lib/prick/schema.rb', line 68 def path() "#{project.path}/#{SCHEMA_DIR}" end |
#version ⇒ Object
Version read from schemas/prick/data.sql
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/prick/schema.rb', line 25 def version() @version ||= begin File.open(data_file, "r") { |file| while !file.eof? && file.gets.chomp != COPY_STMT ; end !file.eof? or raise Fail, "No COPY statement in #{data_file}" l = file.gets.chomp a = l.split("\t")[1..].map { |val| val == '\N' ? nil : val } a.size == FIELDS.size or raise Fail, "Illegal data format in #{data_file}" custom, major, minor, patch, pre = a[0], *a[1..-2].map { |val| val && val.to_i } v = Version.new("0.0.0", custom: (custom == '\N' ? nil : custom)) v.major = major v.minor = minor v.patch = patch v.pre = (pre == "null" ? nil : pre) v } end end |
#version=(version) ⇒ Object
Write version number into schemas/prick/data.sql
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/prick/schema.rb', line 47 def version=(version) @version = Version.new(version) File.open(data_file, "w") { |f| f.puts "--" f.puts "-- This file is auto-generated by prick(1). Please don't touch" f.puts COPY_STMT f.print \ "1\t", FIELDS[..-2].map { |f| @version.send(f.to_sym) || '\N' }.join("\t"), "\t#{@version}\n" f.puts "\\." } Git.add(data_file) @version end |