Statistics
| Branch: | Tag: | Revision:

one / src / onedb / database_schema.rb @ 8305bf63

History | View | Annotate | Download (5.44 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
            else
80
                version = LATEST_DB_VERSION
81
            end
82
        end
83

    
84
        gver = Gem::Version.new(version)
85

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

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

    
96
        schema = nil
97

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

    
104
        schema = SCHEMA[type] if !schema
105

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

    
111
        schema
112
    end
113

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

    
121
        schema = get_schema(type, version)
122

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

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

    
127
        @db.run sql
128
    end
129
end
130