create dump and models

This commit is contained in:
Ivanov Matvey 2025-03-13 18:53:07 +10:00
parent 4b0673c467
commit 344ac77fc7
10 changed files with 509 additions and 5 deletions

View File

@ -8,8 +8,8 @@ from sqlalchemy.ext.asyncio import async_engine_from_config
from alembic import context
from src.settings import settings
from src.adapters.database.models import BaseDB1
from src.adapters.database.models.db1.post import * # noqa: F401
from src.adapters.database import BaseDB1
from src.adapters.database.models.db1.user import User # noqa: F401
# this is the Alembic Config object, which provides
# access to the values within the .ini file in use.

View File

@ -0,0 +1,53 @@
"""empty message
Revision ID: 9975c56b36b8
Revises:
Create Date: 2025-03-13 18:49:11.155773
"""
from typing import Sequence, Union
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '9975c56b36b8'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.create_table('user',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('email', sa.String(), nullable=False),
sa.Column('login', sa.String(), nullable=False),
sa.PrimaryKeyConstraint('id')
)
op.create_table('blog',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('owner_id', sa.INTEGER(), nullable=False),
sa.Column('name', sa.String(), nullable=False),
sa.Column('description', sa.String(), nullable=False),
sa.ForeignKeyConstraint(['owner_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
op.create_table('post',
sa.Column('id', sa.INTEGER(), autoincrement=True, nullable=False),
sa.Column('header', sa.String(), nullable=False),
sa.Column('text', sa.String(), nullable=False),
sa.Column('author_id', sa.INTEGER(), nullable=False),
sa.ForeignKeyConstraint(['author_id'], ['user.id'], ),
sa.PrimaryKeyConstraint('id')
)
# ### end Alembic commands ###
def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_table('post')
op.drop_table('blog')
op.drop_table('user')
# ### end Alembic commands ###

View File

@ -8,7 +8,7 @@ from sqlalchemy.ext.asyncio import async_engine_from_config
from alembic import context
from src.settings import settings
from src.adapters.database.models import BaseDB2
from src.adapters.database import BaseDB2
from src.adapters.database.models.db2.log import Log # noqa: F401

View File

@ -0,0 +1,260 @@
--
-- PostgreSQL database dump
--
-- Dumped from database version 17.3 (Debian 17.3-3.pgdg120+1)
-- Dumped by pg_dump version 17.3 (Debian 17.3-3.pgdg120+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: alembic_version; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.alembic_version (
version_num character varying(32) NOT NULL
);
ALTER TABLE public.alembic_version OWNER TO postgres;
--
-- Name: blog; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.blog (
id integer NOT NULL,
owner_id integer NOT NULL,
name character varying NOT NULL,
description character varying NOT NULL
);
ALTER TABLE public.blog OWNER TO postgres;
--
-- Name: blog_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE public.blog_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.blog_id_seq OWNER TO postgres;
--
-- Name: blog_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE public.blog_id_seq OWNED BY public.blog.id;
--
-- Name: post; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.post (
id integer NOT NULL,
header character varying NOT NULL,
text character varying NOT NULL,
author_id integer NOT NULL
);
ALTER TABLE public.post OWNER TO postgres;
--
-- Name: post_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE public.post_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.post_id_seq OWNER TO postgres;
--
-- Name: post_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE public.post_id_seq OWNED BY public.post.id;
--
-- Name: user; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public."user" (
id integer NOT NULL,
email character varying NOT NULL,
login character varying NOT NULL
);
ALTER TABLE public."user" OWNER TO postgres;
--
-- Name: user_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE public.user_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.user_id_seq OWNER TO postgres;
--
-- Name: user_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE public.user_id_seq OWNED BY public."user".id;
--
-- Name: blog id; Type: DEFAULT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.blog ALTER COLUMN id SET DEFAULT nextval('public.blog_id_seq'::regclass);
--
-- Name: post id; Type: DEFAULT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.post ALTER COLUMN id SET DEFAULT nextval('public.post_id_seq'::regclass);
--
-- Name: user id; Type: DEFAULT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public."user" ALTER COLUMN id SET DEFAULT nextval('public.user_id_seq'::regclass);
--
-- Data for Name: alembic_version; Type: TABLE DATA; Schema: public; Owner: postgres
--
INSERT INTO public.alembic_version (version_num) VALUES ('9975c56b36b8');
--
-- Data for Name: blog; Type: TABLE DATA; Schema: public; Owner: postgres
--
--
-- Data for Name: post; Type: TABLE DATA; Schema: public; Owner: postgres
--
--
-- Data for Name: user; Type: TABLE DATA; Schema: public; Owner: postgres
--
--
-- Name: blog_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--
SELECT pg_catalog.setval('public.blog_id_seq', 1, false);
--
-- Name: post_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--
SELECT pg_catalog.setval('public.post_id_seq', 1, false);
--
-- Name: user_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--
SELECT pg_catalog.setval('public.user_id_seq', 1, false);
--
-- Name: alembic_version alembic_version_pkc; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.alembic_version
ADD CONSTRAINT alembic_version_pkc PRIMARY KEY (version_num);
--
-- Name: blog blog_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.blog
ADD CONSTRAINT blog_pkey PRIMARY KEY (id);
--
-- Name: post post_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.post
ADD CONSTRAINT post_pkey PRIMARY KEY (id);
--
-- Name: user user_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public."user"
ADD CONSTRAINT user_pkey PRIMARY KEY (id);
--
-- Name: blog blog_owner_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.blog
ADD CONSTRAINT blog_owner_id_fkey FOREIGN KEY (owner_id) REFERENCES public."user"(id);
--
-- Name: post post_author_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.post
ADD CONSTRAINT post_author_id_fkey FOREIGN KEY (author_id) REFERENCES public."user"(id);
--
-- PostgreSQL database dump complete
--

View File

@ -0,0 +1,146 @@
--
-- PostgreSQL database dump
--
-- Dumped from database version 17.3 (Debian 17.3-3.pgdg120+1)
-- Dumped by pg_dump version 17.3 (Debian 17.3-3.pgdg120+1)
SET statement_timeout = 0;
SET lock_timeout = 0;
SET idle_in_transaction_session_timeout = 0;
SET transaction_timeout = 0;
SET client_encoding = 'UTF8';
SET standard_conforming_strings = on;
SELECT pg_catalog.set_config('search_path', '', false);
SET check_function_bodies = false;
SET xmloption = content;
SET client_min_messages = warning;
SET row_security = off;
--
-- Name: eventtype; Type: TYPE; Schema: public; Owner: postgres
--
CREATE TYPE public.eventtype AS ENUM (
'LOGIN',
'COMMENT',
'CREATE_POST',
'DELETE_POST',
'LOGOUT'
);
ALTER TYPE public.eventtype OWNER TO postgres;
--
-- Name: spacetype; Type: TYPE; Schema: public; Owner: postgres
--
CREATE TYPE public.spacetype AS ENUM (
'GLOBAL',
'BLOG',
'POST'
);
ALTER TYPE public.spacetype OWNER TO postgres;
SET default_tablespace = '';
SET default_table_access_method = heap;
--
-- Name: alembic_version; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.alembic_version (
version_num character varying(32) NOT NULL
);
ALTER TABLE public.alembic_version OWNER TO postgres;
--
-- Name: log; Type: TABLE; Schema: public; Owner: postgres
--
CREATE TABLE public.log (
id integer NOT NULL,
datetime timestamp with time zone NOT NULL,
user_id integer NOT NULL,
space_type public.spacetype NOT NULL,
event_type public.eventtype NOT NULL
);
ALTER TABLE public.log OWNER TO postgres;
--
-- Name: log_id_seq; Type: SEQUENCE; Schema: public; Owner: postgres
--
CREATE SEQUENCE public.log_id_seq
AS integer
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;
ALTER SEQUENCE public.log_id_seq OWNER TO postgres;
--
-- Name: log_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: postgres
--
ALTER SEQUENCE public.log_id_seq OWNED BY public.log.id;
--
-- Name: log id; Type: DEFAULT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.log ALTER COLUMN id SET DEFAULT nextval('public.log_id_seq'::regclass);
--
-- Data for Name: alembic_version; Type: TABLE DATA; Schema: public; Owner: postgres
--
INSERT INTO public.alembic_version (version_num) VALUES ('581af68b8c68');
--
-- Data for Name: log; Type: TABLE DATA; Schema: public; Owner: postgres
--
--
-- Name: log_id_seq; Type: SEQUENCE SET; Schema: public; Owner: postgres
--
SELECT pg_catalog.setval('public.log_id_seq', 1, false);
--
-- Name: alembic_version alembic_version_pkc; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.alembic_version
ADD CONSTRAINT alembic_version_pkc PRIMARY KEY (version_num);
--
-- Name: log log_pkey; Type: CONSTRAINT; Schema: public; Owner: postgres
--
ALTER TABLE ONLY public.log
ADD CONSTRAINT log_pkey PRIMARY KEY (id);
--
-- PostgreSQL database dump complete
--

View File

@ -0,0 +1,2 @@
from .models.db1.base import Base as BaseDB1 # noqa: F401
from .models.db2.base import Base as BaseDB2 # noqa: F401

View File

@ -1,2 +1,5 @@
from .db1.base import Base as BaseDB1
from .db2.base import Base as BaseDB2
from .db1.user import User as User
from .db1.blog import Blog as Blog
from .db1.post import Post as Post
from .db2.log import Log as Log

View File

@ -0,0 +1,14 @@
from sqlalchemy import INTEGER, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base
from .user import User
class Blog(Base):
__tablename__ = "blog"
id: Mapped[int] = mapped_column(INTEGER, primary_key=True, autoincrement=True)
owner_id: Mapped[int] = mapped_column(ForeignKey(User.id))
# owner: Mapped[User] = relationship(lazy="selectin")
name: Mapped[str]
description: Mapped[str] = mapped_column(default="")

View File

@ -0,0 +1,15 @@
from sqlalchemy import INTEGER, ForeignKey
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base
from .user import User
class Post(Base):
__tablename__ = "post"
id: Mapped[int] = mapped_column(INTEGER, primary_key=True, autoincrement=True)
header: Mapped[str]
text: Mapped[str] = mapped_column(default="")
author_id: Mapped[int] = mapped_column(ForeignKey(User.id))
# author: Mapped[User] = relationship(lazy="selectin")

View File

@ -0,0 +1,11 @@
from sqlalchemy import INTEGER
from sqlalchemy.orm import Mapped, mapped_column
from .base import Base
class User(Base):
__tablename__ = "user"
id: Mapped[int] = mapped_column(INTEGER, primary_key=True, autoincrement=True)
email: Mapped[str]
login: Mapped[str]