From fadb41922aec6901255946bb13c4f0a9ccf70fa5 Mon Sep 17 00:00:00 2001 From: matv864 Date: Fri, 14 Mar 2025 13:29:46 +1000 Subject: [PATCH] debug and create new table --- .../create-multiple-postgresql-databases.sh | 2 +- database/dumps/db1.sql | 83 +++++++++++++++++++ src/adapters/database/models/__init__.py | 1 + src/adapters/database/models/db1/comment.py | 16 ++++ 4 files changed, 101 insertions(+), 1 deletion(-) create mode 100644 src/adapters/database/models/db1/comment.py diff --git a/database/create-multiple-postgresql-databases.sh b/database/create-multiple-postgresql-databases.sh index 2bb825c..1ae8eee 100644 --- a/database/create-multiple-postgresql-databases.sh +++ b/database/create-multiple-postgresql-databases.sh @@ -14,7 +14,7 @@ EOSQL function import_dump() { local database=$1 - psql -U "$POSTGRES_USER" -d "$database" -a -f "docker-entrypoint-initdb.d/$database.sql" + psql -U "$POSTGRES_USER" -d "$database" -a -f "docker-entrypoint-initdb.d/dumps/$database.sql" } if [ -n "$POSTGRES_MULTIPLE_DATABASES" ]; then diff --git a/database/dumps/db1.sql b/database/dumps/db1.sql index bb26439..7acb107 100644 --- a/database/dumps/db1.sql +++ b/database/dumps/db1.sql @@ -57,6 +57,42 @@ ALTER SEQUENCE public.blog_id_seq OWNER TO postgres; ALTER SEQUENCE public.blog_id_seq OWNED BY public.blog.id; +-- +-- Name: comment; Type: TABLE; Schema: public; Owner: postgres +-- + +CREATE TABLE public.comment ( + id integer NOT NULL, + text character varying NOT NULL, + author_id integer NOT NULL, + post_id integer NOT NULL +); + + +ALTER TABLE public.comment OWNER TO postgres; + +-- +-- Name: comment_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres +-- + +CREATE SEQUENCE public.comment_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +ALTER SEQUENCE public.comment_id_seq OWNER TO postgres; + +-- +-- Name: comment_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres +-- + +ALTER SEQUENCE public.comment_id_seq OWNED BY public.comment.id; + + -- -- Name: post; Type: TABLE; Schema: public; Owner: postgres -- @@ -136,6 +172,13 @@ ALTER SEQUENCE public.user_id_seq OWNED BY public."user".id; ALTER TABLE ONLY public.blog ALTER COLUMN id SET DEFAULT nextval('public.blog_id_seq'::regclass); +-- +-- Name: comment id; Type: DEFAULT; Schema: public; Owner: postgres +-- + +ALTER TABLE ONLY public.comment ALTER COLUMN id SET DEFAULT nextval('public.comment_id_seq'::regclass); + + -- -- Name: post id; Type: DEFAULT; Schema: public; Owner: postgres -- @@ -158,6 +201,15 @@ INSERT INTO public.blog (id, owner_id, name, description) VALUES (2, 1, 'мой INSERT INTO public.blog (id, owner_id, name, description) VALUES (3, 2, 'мой старый блог', 'такое же описание'); +-- +-- Data for Name: comment; Type: TABLE DATA; Schema: public; Owner: postgres +-- + +INSERT INTO public.comment (id, text, author_id, post_id) VALUES (1, 'хороший пост', 1, 1); +INSERT INTO public.comment (id, text, author_id, post_id) VALUES (2, 'тоже норм', 1, 2); +INSERT INTO public.comment (id, text, author_id, post_id) VALUES (3, 'другой комент', 2, 2); + + -- -- Data for Name: post; Type: TABLE DATA; Schema: public; Owner: postgres -- @@ -181,6 +233,13 @@ INSERT INTO public."user" (id, email, login) VALUES (2, 'another@gmail.com', 'an SELECT pg_catalog.setval('public.blog_id_seq', 3, true); +-- +-- Name: comment_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres +-- + +SELECT pg_catalog.setval('public.comment_id_seq', 3, true); + + -- -- Name: post_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres -- @@ -203,6 +262,14 @@ ALTER TABLE ONLY public.blog ADD CONSTRAINT blog_pkey PRIMARY KEY (id); +-- +-- Name: comment comment_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres +-- + +ALTER TABLE ONLY public.comment + ADD CONSTRAINT comment_pkey PRIMARY KEY (id); + + -- -- Name: post post_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres -- @@ -227,6 +294,22 @@ ALTER TABLE ONLY public.blog ADD CONSTRAINT blog_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES public."user"(id); +-- +-- Name: comment comment_author_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres +-- + +ALTER TABLE ONLY public.comment + ADD CONSTRAINT comment_author_id_fkey FOREIGN KEY (author_id) REFERENCES public."user"(id); + + +-- +-- Name: comment comment_post_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres +-- + +ALTER TABLE ONLY public.comment + ADD CONSTRAINT comment_post_id_fkey FOREIGN KEY (post_id) REFERENCES public.post(id); + + -- -- Name: post post_author_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres -- diff --git a/src/adapters/database/models/__init__.py b/src/adapters/database/models/__init__.py index f823457..f994a93 100644 --- a/src/adapters/database/models/__init__.py +++ b/src/adapters/database/models/__init__.py @@ -1,5 +1,6 @@ from .db1.user import User as User from .db1.blog import Blog as Blog from .db1.post import Post as Post +from .db1.comment import Comment as Comment from .db2.log import Log as Log diff --git a/src/adapters/database/models/db1/comment.py b/src/adapters/database/models/db1/comment.py new file mode 100644 index 0000000..21af1df --- /dev/null +++ b/src/adapters/database/models/db1/comment.py @@ -0,0 +1,16 @@ +from sqlalchemy import INTEGER, ForeignKey +from sqlalchemy.orm import Mapped, mapped_column + +from .base import Base +from .user import User +from .post import Post + + +class Comment(Base): + __tablename__ = "comment" + id: Mapped[int] = mapped_column(INTEGER, primary_key=True, autoincrement=True) + text: Mapped[str] + author_id: Mapped[int] = mapped_column(ForeignKey(User.id)) + # author: Mapped[User] = relationship(lazy="selectin") + post_id: Mapped[int] = mapped_column(ForeignKey(Post.id)) + # post: Mapped[Post] = relationship(lazy="selectin") \ No newline at end of file