Skip to content

Commit d3782f4

Browse files
committed
Upgrade to SQLAlchemy 2.1 syntax and API
1 parent c2680b3 commit d3782f4

15 files changed

Lines changed: 414 additions & 213 deletions

library.sql

Lines changed: 78 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22
-- PostgreSQL database dump
33
--
44

5-
-- Dumped from database version 17.4 (Debian 17.4-1.pgdg120+2)
6-
-- Dumped by pg_dump version 17.0 (Ubuntu 17.0-1.pgdg24.04+1)
5+
\restrict 6N1gfbETXMlbXbp5vnhqyja28IXXRcJlY8xyIrdicRKNiHx7Nukx5LY6PHCRWxl
6+
7+
-- Dumped from database version 18.0 (Debian 18.0-1.pgdg13+3)
8+
-- Dumped by pg_dump version 18.1 (Ubuntu 18.1-1.pgdg25.10+2)
79

810
SET statement_timeout = 0;
911
SET lock_timeout = 0;
@@ -42,8 +44,8 @@ CREATE TABLE public.authors (
4244
lastname character varying(128) NOT NULL,
4345
email character varying(255) NOT NULL,
4446
phone character varying(15),
45-
created_at timestamp with time zone NOT NULL,
46-
modified_at timestamp with time zone NOT NULL
47+
created_at timestamp with time zone DEFAULT now() NOT NULL,
48+
modified_at timestamp with time zone DEFAULT now() NOT NULL
4749
);
4850

4951

@@ -53,14 +55,22 @@ ALTER TABLE public.authors OWNER TO guest;
5355
-- Name: authors_id_seq; Type: SEQUENCE; Schema: public; Owner: guest
5456
--
5557

56-
ALTER TABLE public.authors ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
57-
SEQUENCE NAME public.authors_id_seq
58+
CREATE SEQUENCE public.authors_id_seq
59+
AS integer
5860
START WITH 1
5961
INCREMENT BY 1
6062
NO MINVALUE
6163
NO MAXVALUE
62-
CACHE 1
63-
);
64+
CACHE 1;
65+
66+
67+
ALTER SEQUENCE public.authors_id_seq OWNER TO guest;
68+
69+
--
70+
-- Name: authors_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: guest
71+
--
72+
73+
ALTER SEQUENCE public.authors_id_seq OWNED BY public.authors.id;
6474

6575

6676
--
@@ -72,9 +82,9 @@ CREATE TABLE public.books (
7282
title character varying(128) NOT NULL,
7383
isbn character varying(255) NOT NULL,
7484
page_count integer NOT NULL,
75-
created_at timestamp with time zone NOT NULL,
76-
modified_at timestamp with time zone NOT NULL,
77-
author_id integer NOT NULL
85+
author_id integer NOT NULL,
86+
created_at timestamp with time zone DEFAULT now() NOT NULL,
87+
modified_at timestamp with time zone DEFAULT now() NOT NULL
7888
);
7989

8090

@@ -84,14 +94,22 @@ ALTER TABLE public.books OWNER TO guest;
8494
-- Name: books_id_seq; Type: SEQUENCE; Schema: public; Owner: guest
8595
--
8696

87-
ALTER TABLE public.books ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
88-
SEQUENCE NAME public.books_id_seq
97+
CREATE SEQUENCE public.books_id_seq
98+
AS integer
8999
START WITH 1
90100
INCREMENT BY 1
91101
NO MINVALUE
92102
NO MAXVALUE
93-
CACHE 1
94-
);
103+
CACHE 1;
104+
105+
106+
ALTER SEQUENCE public.books_id_seq OWNER TO guest;
107+
108+
--
109+
-- Name: books_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: guest
110+
--
111+
112+
ALTER SEQUENCE public.books_id_seq OWNED BY public.books.id;
95113

96114

97115
--
@@ -106,8 +124,8 @@ CREATE TABLE public.users (
106124
phone character varying(15),
107125
password character varying(128),
108126
lastlogin timestamp with time zone,
109-
created_at timestamp with time zone NOT NULL,
110-
modified_at timestamp with time zone NOT NULL
127+
created_at timestamp with time zone DEFAULT now() NOT NULL,
128+
modified_at timestamp with time zone DEFAULT now() NOT NULL
111129
);
112130

113131

@@ -117,22 +135,51 @@ ALTER TABLE public.users OWNER TO guest;
117135
-- Name: users_id_seq; Type: SEQUENCE; Schema: public; Owner: guest
118136
--
119137

120-
ALTER TABLE public.users ALTER COLUMN id ADD GENERATED BY DEFAULT AS IDENTITY (
121-
SEQUENCE NAME public.users_id_seq
138+
CREATE SEQUENCE public.users_id_seq
139+
AS integer
122140
START WITH 1
123141
INCREMENT BY 1
124142
NO MINVALUE
125143
NO MAXVALUE
126-
CACHE 1
127-
);
144+
CACHE 1;
145+
146+
147+
ALTER SEQUENCE public.users_id_seq OWNER TO guest;
148+
149+
--
150+
-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: public; Owner: guest
151+
--
152+
153+
ALTER SEQUENCE public.users_id_seq OWNED BY public.users.id;
154+
155+
156+
--
157+
-- Name: authors id; Type: DEFAULT; Schema: public; Owner: guest
158+
--
159+
160+
ALTER TABLE ONLY public.authors ALTER COLUMN id SET DEFAULT nextval('public.authors_id_seq'::regclass);
161+
162+
163+
--
164+
-- Name: books id; Type: DEFAULT; Schema: public; Owner: guest
165+
--
166+
167+
ALTER TABLE ONLY public.books ALTER COLUMN id SET DEFAULT nextval('public.books_id_seq'::regclass);
168+
169+
170+
--
171+
-- Name: users id; Type: DEFAULT; Schema: public; Owner: guest
172+
--
173+
174+
ALTER TABLE ONLY public.users ALTER COLUMN id SET DEFAULT nextval('public.users_id_seq'::regclass);
128175

129176

130177
--
131178
-- Data for Name: alembic_version; Type: TABLE DATA; Schema: public; Owner: guest
132179
--
133180

134181
COPY public.alembic_version (version_num) FROM stdin;
135-
8813faeb5825
182+
84f8ba08e237
136183
\.
137184

138185

@@ -141,14 +188,16 @@ COPY public.alembic_version (version_num) FROM stdin;
141188
--
142189

143190
COPY public.authors (id, firstname, lastname, email, phone, created_at, modified_at) FROM stdin;
191+
1 JK Rowing jk@rowing.com +49123456789 2026-01-30 14:45:21.230749+00 2026-01-30 14:45:21.230749+00
144192
\.
145193

146194

147195
--
148196
-- Data for Name: books; Type: TABLE DATA; Schema: public; Owner: guest
149197
--
150198

151-
COPY public.books (id, title, isbn, page_count, created_at, modified_at, author_id) FROM stdin;
199+
COPY public.books (id, title, isbn, page_count, author_id, created_at, modified_at) FROM stdin;
200+
1 Harry Porter 978-0-596-52068-7 123 1 2026-01-30 14:46:48.081686+00 2026-01-30 14:46:48.081686+00
152201
\.
153202

154203

@@ -157,28 +206,29 @@ COPY public.books (id, title, isbn, page_count, created_at, modified_at, author_
157206
--
158207

159208
COPY public.users (id, firstname, lastname, email, phone, password, lastlogin, created_at, modified_at) FROM stdin;
209+
1 Kok How Teh khteh@email.com \N $2b$10$3zixeio4.1.OU.5OgdXLvuCbwk40ZyyWxD7xtlL1Joil04dSC/lHK \N 2026-01-30 14:44:45.219441+00 2026-01-30 14:44:45.219441+00
160210
\.
161211

162212

163213
--
164214
-- Name: authors_id_seq; Type: SEQUENCE SET; Schema: public; Owner: guest
165215
--
166216

167-
SELECT pg_catalog.setval('public.authors_id_seq', 1, false);
217+
SELECT pg_catalog.setval('public.authors_id_seq', 1, true);
168218

169219

170220
--
171221
-- Name: books_id_seq; Type: SEQUENCE SET; Schema: public; Owner: guest
172222
--
173223

174-
SELECT pg_catalog.setval('public.books_id_seq', 1, false);
224+
SELECT pg_catalog.setval('public.books_id_seq', 1, true);
175225

176226

177227
--
178228
-- Name: users_id_seq; Type: SEQUENCE SET; Schema: public; Owner: guest
179229
--
180230

181-
SELECT pg_catalog.setval('public.users_id_seq', 1, false);
231+
SELECT pg_catalog.setval('public.users_id_seq', 1, true);
182232

183233

184234
--
@@ -253,3 +303,5 @@ ALTER TABLE ONLY public.books
253303
-- PostgreSQL database dump complete
254304
--
255305

306+
\unrestrict 6N1gfbETXMlbXbp5vnhqyja28IXXRcJlY8xyIrdicRKNiHx7Nukx5LY6PHCRWxl
307+

migrations/env.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,13 @@
2121
# for 'autogenerate' support
2222
# from myapp import mymodel
2323
# target_metadata = mymodel.Base.metadata
24-
from src.models.base import Base
2524
from src.models.AuthorModel import AuthorModel
2625
from src.models.BookModel import BookModel
2726
from src.models.UserModel import UserModel
27+
from src.models.base import Base
2828

2929
target_metadata = Base.metadata
3030

31-
#from src.models.Database import LibraryMetadata
32-
#target_metadata = LibraryMetadata
33-
3431
# other values from the config, defined by the needs of env.py,
3532
# can be acquired:
3633
# my_important_option = config.get_main_option("my_important_option")
@@ -41,6 +38,7 @@
4138
config.set_section_option(section, "DB_PASSWORD", urllib.parse.quote_plus(os.environ.get('DB_PASSWORD')).replace("%", "%%"))
4239
config.set_section_option(section, "DB_HOST", json_config["DB_HOST"])
4340
config.set_section_option(section, "DB_DATABASE", json_config["DB_DATABASE"])
41+
4442
def run_migrations_offline() -> None:
4543
"""Run migrations in 'offline' mode.
4644
@@ -51,7 +49,6 @@ def run_migrations_offline() -> None:
5149
5250
Calls to context.execute() here emit the given string to the
5351
script output.
54-
5552
"""
5653
url = config.get_main_option("sqlalchemy.url")
5754
context.configure(
@@ -60,17 +57,14 @@ def run_migrations_offline() -> None:
6057
literal_binds=True,
6158
dialect_opts={"paramstyle": "named"},
6259
)
63-
6460
with context.begin_transaction():
6561
context.run_migrations()
6662

67-
6863
def run_migrations_online() -> None:
6964
"""Run migrations in 'online' mode.
7065
7166
In this scenario we need to create an Engine
7267
and associate a connection with the context.
73-
7468
"""
7569
connectable = engine_from_config(
7670
config.get_section(config.config_ini_section, {}),
@@ -82,11 +76,9 @@ def run_migrations_online() -> None:
8276
context.configure(
8377
connection=connection, target_metadata=target_metadata
8478
)
85-
8679
with context.begin_transaction():
8780
context.run_migrations()
8881

89-
9082
if context.is_offline_mode():
9183
run_migrations_offline()
9284
else:

migrations/script.py.mako

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ ${imports if imports else ""}
1313

1414
# revision identifiers, used by Alembic.
1515
revision: str = ${repr(up_revision)}
16-
down_revision: Union[str, None] = ${repr(down_revision)}
16+
down_revision: Union[str, Sequence[str], None] = ${repr(down_revision)}
1717
branch_labels: Union[str, Sequence[str], None] = ${repr(branch_labels)}
1818
depends_on: Union[str, Sequence[str], None] = ${repr(depends_on)}
1919

migrations/versions/8813faeb5825_initial_migration.py renamed to migrations/versions/84f8ba08e237_initial_migration.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
"""Initial migration
22
3-
Revision ID: 8813faeb5825
3+
Revision ID: 84f8ba08e237
44
Revises:
5-
Create Date: 2025-03-27 11:45:48.556713
5+
Create Date: 2026-01-30 22:43:03.290447
66
77
"""
88
from typing import Sequence, Union
99

1010
from alembic import op
1111
import sqlalchemy as sa
12-
from sqlalchemy import FetchedValue
12+
1313

1414
# revision identifiers, used by Alembic.
15-
revision: str = '8813faeb5825'
16-
down_revision: Union[str, None] = None
15+
revision: str = '84f8ba08e237'
16+
down_revision: Union[str, Sequence[str], None] = None
1717
branch_labels: Union[str, Sequence[str], None] = None
1818
depends_on: Union[str, Sequence[str], None] = None
1919

@@ -22,39 +22,39 @@ def upgrade() -> None:
2222
"""Upgrade schema."""
2323
# ### commands auto generated by Alembic - please adjust! ###
2424
op.create_table('authors',
25-
sa.Column('id', sa.Integer(), sa.Identity(always=False), autoincrement=True, nullable=False),
25+
sa.Column('id', sa.Integer(), nullable=False),
2626
sa.Column('firstname', sa.String(length=128), nullable=False),
2727
sa.Column('lastname', sa.String(length=128), nullable=False),
2828
sa.Column('email', sa.String(length=255), nullable=False),
2929
sa.Column('phone', sa.String(length=15), nullable=True),
30-
sa.Column('created_at', sa.DateTime(timezone=True), server_default=FetchedValue(), nullable=False),
31-
sa.Column('modified_at', sa.DateTime(timezone=True), server_default=FetchedValue(), nullable=False),
30+
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
31+
sa.Column('modified_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
3232
sa.PrimaryKeyConstraint('id')
3333
)
3434
op.create_index(op.f('ix_authors_email'), 'authors', ['email'], unique=True)
3535
op.create_index(op.f('ix_authors_phone'), 'authors', ['phone'], unique=True)
3636
op.create_table('users',
37-
sa.Column('id', sa.Integer(), sa.Identity(always=False), autoincrement=True, nullable=False),
37+
sa.Column('id', sa.Integer(), nullable=False),
3838
sa.Column('firstname', sa.String(length=128), nullable=False),
3939
sa.Column('lastname', sa.String(length=128), nullable=False),
4040
sa.Column('email', sa.String(length=255), nullable=False),
4141
sa.Column('phone', sa.String(length=15), nullable=True),
4242
sa.Column('password', sa.String(length=128), nullable=True),
43-
sa.Column('lastlogin', sa.DateTime(timezone=True), server_default=FetchedValue(), nullable=True),
44-
sa.Column('created_at', sa.DateTime(timezone=True), server_default=FetchedValue(), nullable=False),
45-
sa.Column('modified_at', sa.DateTime(timezone=True), server_default=FetchedValue(), nullable=False),
43+
sa.Column('lastlogin', sa.DateTime(timezone=True), nullable=True),
44+
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
45+
sa.Column('modified_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
4646
sa.PrimaryKeyConstraint('id')
4747
)
4848
op.create_index(op.f('ix_users_email'), 'users', ['email'], unique=True)
4949
op.create_index(op.f('ix_users_phone'), 'users', ['phone'], unique=True)
5050
op.create_table('books',
51-
sa.Column('id', sa.Integer(), sa.Identity(always=False), autoincrement=True, nullable=False),
51+
sa.Column('id', sa.Integer(), nullable=False),
5252
sa.Column('title', sa.String(length=128), nullable=False),
5353
sa.Column('isbn', sa.String(length=255), nullable=False),
5454
sa.Column('page_count', sa.Integer(), nullable=False),
55-
sa.Column('created_at', sa.DateTime(timezone=True), server_default=FetchedValue(), nullable=False),
56-
sa.Column('modified_at', sa.DateTime(timezone=True), server_default=FetchedValue(), nullable=False),
5755
sa.Column('author_id', sa.Integer(), nullable=False),
56+
sa.Column('created_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
57+
sa.Column('modified_at', sa.DateTime(timezone=True), server_default=sa.text('now()'), nullable=False),
5858
sa.ForeignKeyConstraint(['author_id'], ['authors.id'], ),
5959
sa.PrimaryKeyConstraint('id')
6060
)

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ dependencies = [
2525
"flask-sqlalchemy",
2626
"quart-flask-patch",
2727
"psycopg[binary,pool]",
28+
"marshmallow-sqlalchemy>=1.4.2",
2829
]
2930

3031
[dependency-groups]

0 commit comments

Comments
 (0)