Statistics
| Branch: | Tag: | Revision:

one / src / onedb / database_schema.rb @ 1448e1df

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

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

    
70
    LATEST_DB_VERSION = "5.3.80"
71

    
72
    def get_schema(type, version = nil)
73
        if !version
74
            if self.respond_to?(:db_version)
75
                version = db_version
76
            else
77
                version = LATEST_DB_VERSION
78
            end
79
        end
80

    
81
        version_schema = VERSION_SCHEMA[version] || {}
82
        schema = SCHEMA.merge(version_schema)[type]
83

    
84
        if !schema
85
            STDERR.puts "Schema not found (#{type}) for version #{version}"
86
            exit(-1)
87
        end
88

    
89
        schema
90
    end
91

    
92
    def create_table(type, name = nil, version = nil)
93
        if name
94
            n = name.to_s
95
        else
96
            n = type.to_s
97
        end
98

    
99
        schema = get_schema(type, version)
100

    
101
        sql = "CREATE TABLE #{n} (#{schema});"
102

    
103
        @db.run sql
104
    end
105
end
106