From: Michael Tremer Date: Tue, 25 Oct 2022 10:38:38 +0000 (+0000) Subject: builds: Introduce groups X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=668846ac4d94943b11b6aafba26473df6e41be76;p=pbs.git builds: Introduce groups Signed-off-by: Michael Tremer --- diff --git a/src/buildservice/builds.py b/src/buildservice/builds.py index 60ab78f9..ecca71ed 100644 --- a/src/buildservice/builds.py +++ b/src/buildservice/builds.py @@ -168,7 +168,7 @@ class Builds(base.Object): return builds - def create(self, repo, package, owner=None): + def create(self, repo, package, owner=None, group=None): """ Creates a new build based on the given distribution and package """ @@ -182,20 +182,24 @@ class Builds(base.Object): ( build_repo_id, pkg_id, - owner_id + owner_id, + build_group_id ) VALUES ( - %s, - %s, - %s + %s, %s, %s, %s ) RETURNING *""", repo, package, owner, + group, ) + # Populate cache + if group: + build.group = group + # Create all jobs build._create_jobs() @@ -206,6 +210,15 @@ class Builds(base.Object): return build + # Groups + + @lazy_property + def groups(self): + """ + Build Groups + """ + return Groups(self.backend) + # Comments @lazy_property @@ -316,6 +329,13 @@ class Build(base.DataObject): def distro(self): return self.build_repo.distro + # Group + + @lazy_property + def group(self): + if self.data.build_group_id: + return self.backend.builds.groups.get_by_id(self.data.build_group_id) + @property def state(self): return self.data.state @@ -693,6 +713,81 @@ class Build(base.DataObject): self._update_bug(bug_id, status=status, resolution=resolution, comment=comment) +class Groups(base.Object): + """ + Build Groups are simple objects that group multiple builds together + """ + def _get_groups(self, query, *args): + res = self.db.query(query, *args) + + for row in res: + yield Group(self.backend, row.id, data=row) + + def _get_group(self, query, *args): + res = self.db.get(query, *args) + + if res: + return Group(self.backend, res.id, data=res) + + def get_by_id(self, id): + return self._get_group(""" + SELECT + * + FROM + build_groups + WHERE + id = %s + """, id, + ) + + def create(self): + """ + Creates a new Build Group + """ + return self._get_group(""" + INSERT INTO + build_groups + DEFAULT VALUES + RETURNING + * + """, + ) + + +class Group(base.DataObject): + table = "build_groups" + + # UUID + + @property + def uuid(self): + return self.data.uuid + + # Created At + + @property + def created_at(self): + return self.data.created_at + + # Builds + + @lazy_property + def builds(self): + builds = self.backend.builds._get_builds(""" + SELECT + * + FROM + builds + WHERE + deleted_at IS FALSE + AND + build_group_id = %s + """, self.id, + ) + + return list(builds) + + class Comments(base.Object): def _get_comments(self, query, *args): res = self.db.query(query, *args) diff --git a/src/database.sql b/src/database.sql index e986abd6..4c3fcdf3 100644 --- a/src/database.sql +++ b/src/database.sql @@ -49,6 +49,37 @@ CREATE TABLE public.build_comments ( ); +-- +-- Name: build_groups; Type: TABLE; Schema: public; Owner: - +-- + +CREATE TABLE public.build_groups ( + id integer NOT NULL, + uuid uuid DEFAULT gen_random_uuid() NOT NULL, + created_at timestamp without time zone DEFAULT CURRENT_TIMESTAMP NOT NULL +); + + +-- +-- Name: build_groups_id_seq; Type: SEQUENCE; Schema: public; Owner: - +-- + +CREATE SEQUENCE public.build_groups_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: build_groups_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: - +-- + +ALTER SEQUENCE public.build_groups_id_seq OWNED BY public.build_groups.id; + + -- -- Name: build_packages; Type: TABLE; Schema: public; Owner: - -- @@ -174,7 +205,8 @@ CREATE TABLE public.builds ( finished_at timestamp without time zone, failed boolean DEFAULT false NOT NULL, deleted_at timestamp without time zone, - deleted_by integer + deleted_by integer, + build_group_id integer ); @@ -1057,6 +1089,13 @@ ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id; ALTER TABLE ONLY public.build_comments ALTER COLUMN id SET DEFAULT nextval('public.builds_comments_id_seq'::regclass); +-- +-- Name: build_groups id; Type: DEFAULT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.build_groups ALTER COLUMN id SET DEFAULT nextval('public.build_groups_id_seq'::regclass); + + -- -- Name: builders id; Type: DEFAULT; Schema: public; Owner: - -- @@ -1205,6 +1244,14 @@ ALTER TABLE ONLY public.build_comments ADD CONSTRAINT build_comments_pkey PRIMARY KEY (id); +-- +-- Name: build_groups build_groups_pkey; Type: CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.build_groups + ADD CONSTRAINT build_groups_pkey PRIMARY KEY (id); + + -- -- Name: builds builds_pkey; Type: CONSTRAINT; Schema: public; Owner: - -- @@ -1410,6 +1457,13 @@ CREATE INDEX build_comments_created_at ON public.build_comments USING btree (cre CREATE INDEX build_comments_user_id ON public.build_comments USING btree (user_id) WHERE (deleted IS FALSE); +-- +-- Name: build_groups_uuid; Type: INDEX; Schema: public; Owner: - +-- + +CREATE UNIQUE INDEX build_groups_uuid ON public.build_groups USING btree (uuid); + + -- -- Name: build_packages_build_id; Type: INDEX; Schema: public; Owner: - -- @@ -1431,6 +1485,13 @@ CREATE UNIQUE INDEX build_watchers_unique ON public.build_watchers USING btree ( CREATE UNIQUE INDEX builders_name ON public.builders USING btree (name) WHERE (deleted IS FALSE); +-- +-- Name: builds_build_group_id; Type: INDEX; Schema: public; Owner: - +-- + +CREATE INDEX builds_build_group_id ON public.builds USING btree (build_group_id) WHERE (deleted_at IS NULL); + + -- -- Name: builds_created_at; Type: INDEX; Schema: public; Owner: - -- @@ -1714,6 +1775,14 @@ ALTER TABLE ONLY public.builder_stats ADD CONSTRAINT builder_stats_builder_id FOREIGN KEY (builder_id) REFERENCES public.builders(id); +-- +-- Name: builds builds_build_group_id; Type: FK CONSTRAINT; Schema: public; Owner: - +-- + +ALTER TABLE ONLY public.builds + ADD CONSTRAINT builds_build_group_id FOREIGN KEY (build_group_id) REFERENCES public.build_groups(id); + + -- -- Name: builds builds_build_repo_id; Type: FK CONSTRAINT; Schema: public; Owner: - --