Class: OpalORM::SchemaManager

Inherits:
Object
  • Object
show all
Defined in:
lib/opal_orm/schema_manager.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.comment_stringObject



42
43
44
45
46
47
48
49
50
51
# File 'lib/opal_orm/schema_manager.rb', line 42

def self.comment_string
  <<-RB
# Example usage:
# create_table 'table_name' do |t|
#   t.string column_name
# end
#
# The primary key will be created automatically, with name 'id'.
  RB
end

.create_table_from_name(table_name) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/opal_orm/schema_manager.rb', line 34

def self.create_table_from_name(table_name)
  <<-RB
create_table \'#{table_name}\' do |t|

end
  RB
end

.generate(file_name, *table_names) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/opal_orm/schema_manager.rb', line 8

def self.generate(file_name, *table_names)
  schema_path = File.join(Util.db_path, "#{file_name}.rb")
  if File.exist?(schema_path)
    raise FileExistsError, "#{file_name}.rb already exists. Please choose a different filename."
  else
    puts "Creating file 'db/#{file_name}.rb' ..."
    File.open(schema_path, "w+") do |f|
      contents = []
      if table_names.empty?
        contents << <<-RB
create_table("table_name") do |t|
end
        RB
      else
        contents += table_names.map do |table_name|
          puts "adding table #{table_name} ..."
          create_table_from_name(table_name)
      end
        contents.unshift(comment_string)
      end
      f.write(contents.join)
      puts "Done."
    end
  end
end

Instance Method Details

#create_table(table_name, &prc) ⇒ Object



53
54
55
56
57
58
# File 'lib/opal_orm/schema_manager.rb', line 53

def create_table(table_name, &prc)
  q = QueryBuilder.create_table_query(table_name, &prc)


  DBConnection.execute(q)
end