Class: SqlServer

Inherits:
Object
  • Object
show all
Defined in:
lib/migsql/sqlserver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, address, database, username, password) ⇒ SqlServer

Returns a new instance of SqlServer.



7
8
9
10
11
12
13
# File 'lib/migsql/sqlserver.rb', line 7

def initialize(name, address, database, username, password)
  @name     = name
  @address  = address
  @database = database
  @username = username
  @password = password
end

Instance Attribute Details

#addressObject (readonly)

Returns the value of attribute address.



15
16
17
# File 'lib/migsql/sqlserver.rb', line 15

def address
  @address
end

#databaseObject (readonly)

Returns the value of attribute database.



15
16
17
# File 'lib/migsql/sqlserver.rb', line 15

def database
  @database
end

#nameObject (readonly)

Returns the value of attribute name.



15
16
17
# File 'lib/migsql/sqlserver.rb', line 15

def name
  @name
end

#passwordObject (readonly)

Returns the value of attribute password.



15
16
17
# File 'lib/migsql/sqlserver.rb', line 15

def password
  @password
end

#usernameObject (readonly)

Returns the value of attribute username.



15
16
17
# File 'lib/migsql/sqlserver.rb', line 15

def username
  @username
end

Instance Method Details

#apply_migration(path) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/migsql/sqlserver.rb', line 74

def apply_migration(path)
  client = get_client
  sql    = File.read(path)
  begin
    sql = parse_sql(sql)
    sql.each do |statement|
      client.execute(statement).each
    end
  rescue Exception => e
    puts "Failed to apply migration from #{path}!".red
    raise e
  end
end

#get_clientObject



33
34
35
36
37
38
39
40
41
# File 'lib/migsql/sqlserver.rb', line 33

def get_client
  require 'tiny_tds'
  TinyTds::Client.new(
    :username => username,
    :password => password,
    :host     => address,
    :database => database
  )
end

#get_migration_statusObject



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/migsql/sqlserver.rb', line 43

def get_migration_status
  client = get_client
  begin
    results = client.execute('SELECT migration FROM _migration')
    result = results.each(:first => true)[0]['migration']
    result = '0' if result.length == 0
  rescue
    result = '0'
  end
  result
end

#get_sql(name) ⇒ Object



17
18
19
# File 'lib/migsql/sqlserver.rb', line 17

def get_sql(name)
  File.read("#{File.dirname(__FILE__)}/../../sql/#{name}.sql")
end

#parse_sql(sql) ⇒ Object

This method will parse some SSMS stuff, such as GO



70
71
72
# File 'lib/migsql/sqlserver.rb', line 70

def parse_sql(sql)
  sql.split(/go\n/i)
end

#remove_migrationObject



64
65
66
67
# File 'lib/migsql/sqlserver.rb', line 64

def remove_migration
  client = get_client
  client.execute('DROP TABLE _migration')
end

#set_migration_status(to) ⇒ Object



55
56
57
58
59
60
61
62
# File 'lib/migsql/sqlserver.rb', line 55

def set_migration_status(to)
  client = get_client
  sql = [
    get_sql('create_migration_table'),
    "UPDATE _migration SET migration = '#{to}'"
  ].join(' ')
  client.execute(sql).each
end