1 from buildbot.db.schema import base
2 from buildbot.db.exceptions import DatabaseNotReadyError
3
14
16 if self.dbapiName == 'MySQLdb':
17 return "`%s` INTEGER PRIMARY KEY AUTO_INCREMENT" % name
18 elif self.dbapiName in ('sqlite3', 'pysqlite2.dbapi2'):
19 return "`%s` INTEGER PRIMARY KEY AUTOINCREMENT" % name
20 raise ValueError("Unsupported dbapi: %s" % self.dbapiName)
21
23 old_name = "%s_old" % table_name
24 cursor = self.conn.cursor()
25
26 cursor.execute("""
27 ALTER TABLE %(table_name)s
28 RENAME TO %(old_name)s
29 """ % locals())
30
31 try:
32 cursor.execute(schema)
33 except:
34
35 cursor.execute("""
36 ALTER TABLE %(old_name)s
37 RENAME TO %(table_name)s
38 """ % locals())
39 raise
40
41 try:
42 cursor.execute("""
43 INSERT INTO %(table_name)s
44 SELECT * FROM %(old_name)s
45 """ % locals())
46 cursor.execute("""
47 DROP TABLE %(old_name)s
48 """ % locals())
49 except:
50
51 cursor.execute("""
52 DROP TABLE %(table_name)s
53 """ % locals())
54 cursor.execute("""
55 ALTER TABLE %(old_name)s
56 RENAME TO %(table_name)s
57 """ % locals())
58 raise
59
61 c = self.conn.cursor()
62 c.execute("""UPDATE version set version = 4 where version = 3""")
63
65 schedulerid_col = self.makeAutoincColumn('schedulerid')
66 schema = """
67 CREATE TABLE schedulers (
68 %(schedulerid_col)s, -- joins to other tables
69 `name` VARCHAR(100) NOT NULL, -- the scheduler's name according to master.cfg
70 `class_name` VARCHAR(100) NOT NULL, -- the scheduler's class
71 `state` VARCHAR(1024) NOT NULL -- JSON-encoded state dictionary
72 );
73 """ % locals()
74 self.migrate_table('schedulers', schema)
75
76
77 cursor = self.conn.cursor()
78 cursor.execute("""
79 CREATE UNIQUE INDEX `name_and_class` ON
80 schedulers (`name`, `class_name`)
81 """)
82
84 buildid_col = self.makeAutoincColumn('id')
85 schema = """
86 CREATE TABLE builds (
87 %(buildid_col)s,
88 `number` INTEGER NOT NULL, -- BuilderStatus.getBuild(number)
89 -- 'number' is scoped to both the local buildmaster and the buildername
90 `brid` INTEGER NOT NULL, -- matches buildrequests.id
91 `start_time` INTEGER NOT NULL,
92 `finish_time` INTEGER
93 );
94 """ % locals()
95 self.migrate_table('builds', schema)
96
98 changeid_col = self.makeAutoincColumn('changeid')
99 schema = """
100 CREATE TABLE changes (
101 %(changeid_col)s, -- also serves as 'change number'
102 `author` VARCHAR(1024) NOT NULL,
103 `comments` VARCHAR(1024) NOT NULL, -- too short?
104 `is_dir` SMALLINT NOT NULL, -- old, for CVS
105 `branch` VARCHAR(1024) NULL,
106 `revision` VARCHAR(256), -- CVS uses NULL. too short for darcs?
107 `revlink` VARCHAR(256) NULL,
108 `when_timestamp` INTEGER NOT NULL, -- copied from incoming Change
109 `category` VARCHAR(256) NULL,
110
111 -- repository specifies, along with revision and branch, the
112 -- source tree in which this change was detected.
113 `repository` TEXT NOT NULL default '',
114
115 -- project names the project this source code represents. It is used
116 -- later to filter changes
117 `project` TEXT NOT NULL default ''
118 );
119 """ % locals()
120 self.migrate_table('changes', schema)
121
122
123 cursor = self.conn.cursor()
124 cursor.execute("DROP TABLE changes_nextid")
125
127 buildrequestid_col = self.makeAutoincColumn('id')
128 schema = """
129 CREATE TABLE buildrequests (
130 %(buildrequestid_col)s,
131
132 -- every BuildRequest has a BuildSet
133 -- the sourcestampid and reason live in the BuildSet
134 `buildsetid` INTEGER NOT NULL,
135
136 `buildername` VARCHAR(256) NOT NULL,
137
138 `priority` INTEGER NOT NULL default 0,
139
140 -- claimed_at is the time at which a master most recently asserted that
141 -- it is responsible for running the build: this will be updated
142 -- periodically to maintain the claim
143 `claimed_at` INTEGER default 0,
144
145 -- claimed_by indicates which buildmaster has claimed this request. The
146 -- 'name' contains hostname/basedir, and will be the same for subsequent
147 -- runs of any given buildmaster. The 'incarnation' contains bootime/pid,
148 -- and will be different for subsequent runs. This allows each buildmaster
149 -- to distinguish their current claims, their old claims, and the claims
150 -- of other buildmasters, to treat them each appropriately.
151 `claimed_by_name` VARCHAR(256) default NULL,
152 `claimed_by_incarnation` VARCHAR(256) default NULL,
153
154 `complete` INTEGER default 0, -- complete=0 means 'pending'
155
156 -- results is only valid when complete==1
157 `results` SMALLINT, -- 0=SUCCESS,1=WARNINGS,etc, from status/builder.py
158
159 `submitted_at` INTEGER NOT NULL,
160
161 `complete_at` INTEGER
162 );
163 """ % locals()
164 self.migrate_table('buildrequests', schema)
165
167 buildsetsid_col = self.makeAutoincColumn('id')
168 schema = """
169 CREATE TABLE buildsets (
170 %(buildsetsid_col)s,
171 `external_idstring` VARCHAR(256),
172 `reason` VARCHAR(256),
173 `sourcestampid` INTEGER NOT NULL,
174 `submitted_at` INTEGER NOT NULL,
175 `complete` SMALLINT NOT NULL default 0,
176 `complete_at` INTEGER,
177 `results` SMALLINT -- 0=SUCCESS,2=FAILURE, from status/builder.py
178 -- results is NULL until complete==1
179 );
180 """ % locals()
181 self.migrate_table("buildsets", schema)
182
184 patchesid_col = self.makeAutoincColumn('id')
185 schema = """
186 CREATE TABLE patches (
187 %(patchesid_col)s,
188 `patchlevel` INTEGER NOT NULL,
189 `patch_base64` TEXT NOT NULL, -- encoded bytestring
190 `subdir` TEXT -- usually NULL
191 );
192 """ % locals()
193 self.migrate_table("patches", schema)
194
196 sourcestampsid_col = self.makeAutoincColumn('id')
197 schema = """
198 CREATE TABLE sourcestamps (
199 %(sourcestampsid_col)s,
200 `branch` VARCHAR(256) default NULL,
201 `revision` VARCHAR(256) default NULL,
202 `patchid` INTEGER default NULL,
203 `repository` TEXT not null default '',
204 `project` TEXT not null default ''
205 );
206 """ % locals()
207 self.migrate_table("sourcestamps", schema)
208