Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

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

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

# This file is part of daf_butler. 

# 

# Developed for the LSST Data Management System. 

# This product includes software developed by the LSST Project 

# (http://www.lsst.org). 

# See the COPYRIGHT file at the top-level directory of this distribution 

# for details of code ownership. 

# 

# This program is free software: you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program. If not, see <http://www.gnu.org/licenses/>. 

 

import itertools 

 

from .utils import iterable 

from .views import makeView 

from .config import ConfigSubset 

from sqlalchemy import Column, String, Integer, Boolean, LargeBinary, DateTime,\ 

Float, ForeignKeyConstraint, Table, MetaData 

 

metadata = None # Needed to make disabled test_hsc not fail on import 

 

__all__ = ("SchemaConfig", "Schema", "SchemaBuilder") 

 

 

class SchemaConfig(ConfigSubset): 

component = "schema" 

requiredKeys = ("version", "tables") 

defaultConfigFile = "schema.yaml" 

 

 

class Schema: 

"""The SQL schema for a Butler Registry. 

 

Parameters 

---------- 

dataUnits : `DataUnitsRegistry` 

Registry of all possible data units. 

config : `SchemaConfig` or `str`, optional 

Load configuration. Defaults will be read if ``config`` is not 

a `SchemaConfig`. 

 

Attributes 

---------- 

metadata : `sqlalchemy.MetaData` 

The sqlalchemy schema description. 

""" 

def __init__(self, dataUnits, config=None): 

if config is None or not isinstance(config, SchemaConfig): 

config = SchemaConfig(config) 

self.config = config 

self.builder = SchemaBuilder() 

self.dataUnits = dataUnits 

self.buildFromConfig(config) 

 

def buildFromConfig(self, config): 

for tableName, tableDescription in self.config["tables"].items(): 

self.builder.addTable(tableName, tableDescription) 

self.datasetTable = self.builder.metadata.tables["Dataset"] 

self._metadata = self.builder.metadata 

self._tables = self.builder.tables 

self._views = self.builder.views 

self.tables = {k: v for k, v in itertools.chain(self._tables.items(), 

self._views.items())} 

 

 

class SchemaBuilder: 

"""Builds a Schema step-by-step. 

 

Attributes 

---------- 

metadata : `sqlalchemy.MetaData` 

The sqlalchemy schema description. 

tables : `dict` 

All created tables. 

views : `dict` 

All created views. 

""" 

VALID_COLUMN_TYPES = {"string": String, "int": Integer, "float": Float, "region": LargeBinary, 

"bool": Boolean, "blob": LargeBinary, "datetime": DateTime} 

 

def __init__(self): 

self.metadata = MetaData() 

self.tables = {} 

self.views = {} 

 

def addTable(self, tableName, tableDescription): 

"""Add a table to the schema metadata. 

 

Parameters 

---------- 

tableName : `str` 

Key of the table. 

tableDescription : `dict` 

Table description. 

 

Requires: 

- columns, a list of column descriptions 

- foreignKeys, a list of foreign-key constraint descriptions 

 

Raises 

------ 

ValueError 

If a table with the given name already exists. 

""" 

115 ↛ 116line 115 didn't jump to line 116, because the condition on line 115 was never true if tableName in self.metadata.tables: 

raise ValueError("Table with name {} already exists".format(tableName)) 

# Create a Table object (attaches itself to metadata) 

if "sql" in tableDescription: 

# This table can be materialized as a view 

table = makeView(tableName, self.metadata, selectable=tableDescription["sql"]) 

self.views[tableName] = table 

view = True 

else: 

table = Table(tableName, self.metadata) 

self.tables[tableName] = table 

view = False 

127 ↛ 128line 127 didn't jump to line 128, because the condition on line 127 was never true if "columns" not in tableDescription: 

raise ValueError("No columns in table: {}".format(tableName)) 

for columnDescription in tableDescription["columns"]: 

self.addColumn(table, columnDescription) 

if not view and "foreignKeys" in tableDescription: 

for constraintDescription in tableDescription["foreignKeys"]: 

self.addForeignKeyConstraint(table, constraintDescription) 

return table 

 

def addColumn(self, table, columnDescription): 

"""Add a column to a table. 

 

Parameters 

---------- 

table : `sqlalchemy.Table`, `sqlalchemy.expression.TableClause` or `str` 

The table. 

columnDescription : `dict` 

Description of the column to be created. 

Should always contain: 

- name, descriptive name 

- type, valid column type 

May contain: 

- nullable, entry can be null 

- primary_key, mark this column as primary key 

- foreign_key, link to other table 

- doc, docstring 

""" 

154 ↛ 155line 154 didn't jump to line 155, because the condition on line 154 was never true if isinstance(table, str): 

table = self.metadata.tables[table] 

table.append_column(self.makeColumn(columnDescription)) 

 

def addForeignKeyConstraint(self, table, constraintDescription): 

"""Add a ForeignKeyConstraint to a table. 

 

Parameters 

---------- 

table : `sqlalchemy.Table` or `str` 

The table. 

constraintDescription : `dict` 

Description of the ForeignKeyConstraint to be created. 

Should always contain: 

- src, list of source column names 

- tgt, list of target column names 

""" 

171 ↛ 172line 171 didn't jump to line 172, because the condition on line 171 was never true if isinstance(table, str): 

table = self.metadata.tables[table] 

table.append_constraint(self.makeForeignKeyConstraint(constraintDescription)) 

 

def makeColumn(self, columnDescription): 

"""Make a Column entry for addition to a Table. 

 

Parameters 

---------- 

columnDescription : `dict` 

Description of the column to be created. 

Should always contain: 

- name, descriptive name 

- type, valid column type 

May contain: 

- nullable, entry can be null 

- primary_key, mark this column as primary key 

- doc, docstring 

 

Returns 

------- 

c : `sqlalchemy.Column` 

The created `Column` entry. 

 

Raises 

------ 

ValueError 

If the column description contains unsupported arguments 

""" 

description = columnDescription.copy() 

# required 

columnName = description.pop("name") 

args = (columnName, self.VALID_COLUMN_TYPES[description.pop("type")]) 

# additional optional arguments can be passed through directly 

kwargs = {} 

for opt in ("nullable", "primary_key", "doc"): 

if opt in description: 

value = description.pop(opt) 

kwargs[opt] = value 

210 ↛ 211line 210 didn't jump to line 211, because the condition on line 210 was never true if description: 

raise ValueError("Unhandled extra kwargs: {} for column: {}".format(description, columnName)) 

return Column(*args, **kwargs) 

 

def makeForeignKeyConstraint(self, constraintDescription): 

"""Make a ForeignKeyConstraint for addition to a Table. 

 

Parameters 

---------- 

constraintDescription : `dict` 

Description of the ForeignKeyConstraint to be created. 

Should always contain: 

- src, list of source column names 

- tgt, list of target column names 

""" 

src = tuple(iterable(constraintDescription["src"])) 

tgt = tuple(iterable(constraintDescription["tgt"])) 

return ForeignKeyConstraint(src, tgt)