Class: Postspec::Environment
- Inherits:
-
Object
- Object
- Postspec::Environment
- Defined in:
- lib/postspec/environment.rb
Instance Attribute Summary collapse
-
#postspec ⇒ Object
readonly
Returns the value of attribute postspec.
Instance Method Summary collapse
-
#clean ⇒ Object
Removes excess data and meta data, and resets sequences.
- #create ⇒ Object
-
#dirty_tables ⇒ Object
Returns map from table UID to record ID of the last record to be kept so that ‘delete from table_uid where id is null or id > record_id’ does the right thing.
- #drop ⇒ Object
- #exist? ⇒ Boolean
-
#initialize(postspec) ⇒ Environment
constructor
A new instance of Environment.
- #reset ⇒ Object
- #setup(mode) ⇒ Object
-
#table_max_ids ⇒ Object
Returns an array from table name to maximum ID used in the table or in the root table if this is a subtable.
- #table_seed_ids ⇒ Object
- #table_sequence_ids(all: false) ⇒ Object
- #teardown(mode) ⇒ Object
-
#uids ⇒ Object
FIXME: What is this? And why not using @uids?.
Constructor Details
#initialize(postspec) ⇒ Environment
Returns a new instance of Environment.
9 10 11 |
# File 'lib/postspec/environment.rb', line 9 def initialize(postspec) @postspec = postspec end |
Instance Attribute Details
#postspec ⇒ Object (readonly)
Returns the value of attribute postspec.
4 5 6 |
# File 'lib/postspec/environment.rb', line 4 def postspec @postspec end |
Instance Method Details
#clean ⇒ Object
Removes excess data and meta data, and resets sequences. It doesn’t change any triggers
62 63 64 65 66 67 68 69 70 71 |
# File 'lib/postspec/environment.rb', line 62 def clean # puts "Environment#clean" @uids = {} user_tables = dirty_tables postspec_tables = CHANGE_TABLE_UIDS sql = render.delete_tables(user_tables) + render.delete_tables(postspec_tables) conn.execute render.execution_unit(user_tables.keys, sql) end |
#create ⇒ Object
46 47 48 49 50 |
# File 'lib/postspec/environment.rb', line 46 def create() # puts "Environment#create" conn.execute IO.read(Postspec::SHARE_DIR + "/postspec_schema.sql") conn.execute render.register_triggers(:create) end |
#dirty_tables ⇒ Object
Returns map from table UID to record ID of the last record to be kept so that ‘delete from table_uid where id is null or id > record_id’ does the right thing
Note: This assumes that the change triggers has been installed beforehand
39 40 41 42 43 44 |
# File 'lib/postspec/environment.rb', line 39 def dirty_tables seed_ids = table_seed_ids table_max_ids.map { |table_uid, record_id| (seed_id = seed_ids[table_uid]) ? [table_uid, seed_id] : [table_uid, record_id] }.compact.to_h end |
#drop ⇒ Object
52 53 54 55 |
# File 'lib/postspec/environment.rb', line 52 def drop() # puts "Environment#drop" conn.execute "drop schema if exists postspec cascade" end |
#exist? ⇒ Boolean
57 58 59 |
# File 'lib/postspec/environment.rb', line 57 def exist?() conn.exist? "select 1 from pg_namespace where nspname = 'postspec'" end |
#reset ⇒ Object
103 104 105 106 |
# File 'lib/postspec/environment.rb', line 103 def reset() # puts "Environment#reset" conn.execute render.execution_unit(*reset_data) end |
#setup(mode) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/postspec/environment.rb', line 73 def setup(mode) # puts "Environment#setup" sql = case mode when :seed @uids = table_max_ids render.readonly_triggers(:create, @uids) when :empty @uids = {} [] else raise ArgumentError end conn.execute sql end |
#table_max_ids ⇒ Object
Returns an array from table name to maximum ID used in the table or in the root table if this is a subtable
26 27 28 29 30 31 32 |
# File 'lib/postspec/environment.rb', line 26 def table_max_ids result = table_sequence_ids(all: true) postspec.type.tables.select(&:sub_table?).each { |table| result[table.uid] = result[table.root_table.uid] or raise "Oops: #{table.uid}" } result end |
#table_seed_ids ⇒ Object
20 21 22 |
# File 'lib/postspec/environment.rb', line 20 def table_seed_ids conn.map "select table_uid, record_id from postspec.seeds" end |
#table_sequence_ids(all: false) ⇒ Object
13 14 15 16 17 18 |
# File 'lib/postspec/environment.rb', line 13 def table_sequence_ids(all: false) ignore_list = "'" + (postspec.ignore + %w(prick)).join("', '") + "'" conn.map \ "select table_uid, record_id from postspec.table_sequence_ids(array[#{ignore_list}]::varchar[])" + (all ? "" : " where record_id is not null") end |
#teardown(mode) ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 |
# File 'lib/postspec/environment.rb', line 89 def teardown(mode) # puts "Environment#teardown" @uids = nil tables, reset_sql = reset_data sql = case mode when :seed; render.readonly_triggers(:drop) + render.delete_tables(SEED_TABLE_UIDS) when :empty; [] else raise ArgumentError end + reset_sql conn.execute render.execution_unit(tables, sql) end |
#uids ⇒ Object
FIXME: What is this? And why not using @uids?
7 |
# File 'lib/postspec/environment.rb', line 7 def uids() table_sequence_ids end |