Statistics
| Branch: | Tag: | Revision:

one / src / onedb / database_schema.rb @ 591e21d7

History | View | Annotate | Download (5.52 KB)

1
# -------------------------------------------------------------------------- #
2
# Copyright 2002-2017, OpenNebula Project, OpenNebula Systems                #
3
#                                                                            #
4
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
5
# not use this file except in compliance with the License. You may obtain    #
6
# a copy of the License at                                                   #
7
#                                                                            #
8
# http://www.apache.org/licenses/LICENSE-2.0                                 #
9
#                                                                            #
10
# Unless required by applicable law or agreed to in writing, software        #
11
# distributed under the License is distributed on an "AS IS" BASIS,          #
12
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
13
# See the License for the specific language governing permissions and        #
14
# limitations under the License.                                             #
15
#--------------------------------------------------------------------------- #
16

    
17
require "rubygems"
18

    
19
class OneDBBacKEnd
20
    SCHEMA = {
21
        cluster_pool: "oid INTEGER PRIMARY KEY, name VARCHAR(128), " <<
22
            "body MEDIUMTEXT, uid INTEGER, gid INTEGER, owner_u INTEGER, " <<
23
            "group_u INTEGER, other_u INTEGER, UNIQUE(name)",
24
        cluster_datastore_relation: "cid INTEGER, oid INTEGER, " <<
25
            "PRIMARY KEY(cid, oid)",
26
        cluster_network_relation: "cid INTEGER, oid INTEGER, " <<
27
            "PRIMARY KEY(cid, oid)",
28
        datastore_pool: "oid INTEGER PRIMARY KEY, name VARCHAR(128), " <<
29
            "body MEDIUMTEXT, uid INTEGER, gid INTEGER, owner_u INTEGER, " <<
30
            "group_u INTEGER, other_u INTEGER",
31
        cluster_vnc_bitmap: "id INTEGER, map LONGTEXT, PRIMARY KEY(id)",
32
        host_pool: "oid INTEGER PRIMARY KEY, " <<
33
            "name VARCHAR(128), body MEDIUMTEXT, state INTEGER, " <<
34
            "last_mon_time INTEGER, uid INTEGER, gid INTEGER, " <<
35
            "owner_u INTEGER, group_u INTEGER, other_u INTEGER, " <<
36
            "cid INTEGER",
37
        image_pool: "oid INTEGER PRIMARY KEY, name VARCHAR(128), " <<
38
            "body MEDIUMTEXT, uid INTEGER, gid INTEGER, owner_u INTEGER, " <<
39
            "group_u INTEGER, other_u INTEGER, UNIQUE(name,uid)",
40
        network_pool: "oid INTEGER PRIMARY KEY, name VARCHAR(128), " <<
41
            "body MEDIUMTEXT, uid INTEGER, gid INTEGER, owner_u INTEGER, " <<
42
            "group_u INTEGER, other_u INTEGER, pid INTEGER, UNIQUE(name,uid)",
43
        user_quotas: "user_oid INTEGER PRIMARY KEY, body MEDIUMTEXT",
44
        group_quotas: "group_oid INTEGER PRIMARY KEY, body MEDIUMTEXT"
45
    }
46

    
47
    VERSION_SCHEMA = {
48
        "5.3.80" => {
49
            vmgroup_pool: "oid INTEGER PRIMARY KEY, name VARCHAR(128), " <<
50
                "body MEDIUMTEXT, uid INTEGER, gid INTEGER, " <<
51
                "owner_u INTEGER, group_u INTEGER, other_u INTEGER, " <<
52
                "UNIQUE(name,uid)",
53
            host_pool: "oid INTEGER PRIMARY KEY, name VARCHAR(128), " <<
54
                "body MEDIUMTEXT, state INTEGER, last_mon_time INTEGER, " <<
55
                "uid INTEGER, gid INTEGER, owner_u INTEGER, " <<
56
                "group_u INTEGER, other_u INTEGER, cid INTEGER",
57
            vm_pool: "oid INTEGER PRIMARY KEY, name VARCHAR(128), " <<
58
                "body MEDIUMTEXT, uid INTEGER, gid INTEGER, " <<
59
                "last_poll INTEGER, state INTEGER, lcm_state INTEGER, " <<
60
                "owner_u INTEGER, group_u INTEGER, other_u INTEGER",
61
            logdb: "log_index INTEGER PRIMARY KEY, term INTEGER, " <<
62
                "sqlcmd MEDIUMTEXT, timestamp INTEGER, fed_index INTEGER",
63
            history: "vid INTEGER, seq INTEGER, body MEDIUMTEXT, " <<
64
                     "stime INTEGER, etime INTEGER, PRIMARY KEY(vid,seq)",
65
            zone_pool: "oid INTEGER PRIMARY KEY, name VARCHAR(128), " <<
66
                       "body MEDIUMTEXT, uid INTEGER, gid INTEGER, " <<
67
                       "owner_u INTEGER, group_u INTEGER, other_u INTEGER, " <<
68
                       "UNIQUE(name)"
69
        },
70
        "5.4.0" => {}
71
    }
72

    
73
    LATEST_DB_VERSION = "5.4.0"
74

    
75
    def get_schema(type, version = nil)
76
        if !version
77
            if self.respond_to?(:db_version)
78
                version = db_version
79
                version = version[:local_version] if Hash === version
80
            else
81
                version = LATEST_DB_VERSION
82
            end
83
        end
84

    
85
        gver = Gem::Version.new(version.dup)
86

    
87
        # Discard versions greater than the one we are searching for
88
        versions = VERSION_SCHEMA.keys.reject do |v|
89
            Gem::Version.new(v.dup) > gver
90
        end
91

    
92
        # Order versions in decreasing order
93
        versions.sort! do |a, b|
94
            Gem::Version.new(b.dup) <=> Gem::Version.new(a.dup)
95
        end
96

    
97
        schema = nil
98

    
99
        # Find latest type definition
100
        versions.each do |v|
101
            schema = VERSION_SCHEMA[v][type]
102
            next if schema
103
        end
104

    
105
        schema = SCHEMA[type] if !schema
106

    
107
        if !schema
108
            STDERR.puts "Schema not found (#{type}) for version #{version}"
109
            exit(-1)
110
        end
111

    
112
        schema
113
    end
114

    
115
    def create_table(type, name = nil, version = nil)
116
        if name
117
            n = name.to_s
118
        else
119
            n = type.to_s
120
        end
121

    
122
        schema = get_schema(type, version)
123

    
124
        @db.run "DROP TABLE IF EXISTS #{n};"
125

    
126
        sql = "CREATE TABLE #{n} (#{schema});"
127

    
128
        @db.run sql
129
    end
130
end
131