Class: Turntables::Transaction

Inherits:
Object
  • Object
show all
Defined in:
lib/turntables/transaction.rb

Overview

This class takes care of a single transaction

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(sql_file_contents, filename) ⇒ Transaction

Initialize this object with the contents of the sql file

Parameters:

  • sql_file_contents

    are the contents of the given sql file

  • filename

    is the filename of the given sql file. We use the filenames for the version that it is supposed to upgrade to.



11
12
13
14
15
16
17
# File 'lib/turntables/transaction.rb', line 11

def initialize(sql_file_contents,filename)
  # Select only the lines that begin with '--$'
  @comment = sql_file_contents.lines.select{|el| el.match(/--\$/)}.join
  @comment.gsub!(/--\$/, '')
  @version = filename.to_i
  @data    = sql_file_contents
end

Instance Attribute Details

#commentObject

The comment should be parsed out from the respective sql file. We want to keep the comments in the sql file to keep things organized. We do not want to alter the sql language to fit our needs. Therefore we just require the user to comment lines as ‘–$ my comment here’ in order to parse them out

Examples:

How to write comments that are to be parsed

--$ author jon doe
--$ This sql file will update the schema to version 1.2
--$ You should note this and that
-- This commen line would be ignored

CREATE TABLE accounts ( ... ) 


34
35
36
# File 'lib/turntables/transaction.rb', line 34

def comment
  @comment
end

#dataObject

The sql information to be passed on to the db registry



37
38
39
# File 'lib/turntables/transaction.rb', line 37

def data
  @data
end

#versionObject

The version should be obtained from the file name of the respective sql file.



21
22
23
# File 'lib/turntables/transaction.rb', line 21

def version
  @version
end