-
-
Notifications
You must be signed in to change notification settings - Fork 170
/
schema.py
128 lines (104 loc) · 4.06 KB
/
schema.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
from datetime import datetime
import click
import sqlalchemy as sa
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
from sqlalchemy.schema import UniqueConstraint
from pgsync.base import create_database, pg_engine
from pgsync.helper import teardown
from pgsync.utils import config_loader, get_config
class Base(DeclarativeBase):
pass
class User(Base):
__tablename__ = "user"
__table_args__ = (UniqueConstraint("email"),)
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
email: Mapped[str] = mapped_column(sa.String, unique=True, nullable=False)
class Host(Base):
__tablename__ = "host"
__table_args__ = (UniqueConstraint("email"),)
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
email: Mapped[str] = mapped_column(sa.String, unique=True, nullable=False)
class Country(Base):
__tablename__ = "country"
__table_args__ = (UniqueConstraint("name", "country_code"),)
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
name: Mapped[str] = mapped_column(sa.String, nullable=False)
country_code: Mapped[str] = mapped_column(sa.String, nullable=False)
class City(Base):
__tablename__ = "city"
__table_args__ = (UniqueConstraint("name", "country_id"),)
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
name: Mapped[str] = mapped_column(sa.String, nullable=False)
country_id: Mapped[int] = mapped_column(
sa.Integer, sa.ForeignKey(Country.id)
)
country: Mapped[Country] = sa.orm.relationship(
Country,
backref=sa.orm.backref("country"),
)
class Place(Base):
__tablename__ = "place"
__table_args__ = (UniqueConstraint("host_id", "address", "city_id"),)
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
host_id: Mapped[int] = mapped_column(sa.Integer, sa.ForeignKey(Host.id))
address: Mapped[str] = mapped_column(sa.String, nullable=False)
city_id: Mapped[int] = mapped_column(sa.Integer, sa.ForeignKey(City.id))
host: Mapped[Host] = sa.orm.relationship(
Host,
backref=sa.orm.backref("host"),
)
city: Mapped[City] = sa.orm.relationship(
City,
backref=sa.orm.backref("city"),
)
class Booking(Base):
__tablename__ = "booking"
__table_args__ = (UniqueConstraint("user_id", "place_id", "start_date"),)
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
user_id: Mapped[int] = mapped_column(sa.Integer, sa.ForeignKey(User.id))
place_id: Mapped[int] = mapped_column(sa.Integer, sa.ForeignKey(Place.id))
start_date: Mapped[datetime] = mapped_column(
sa.DateTime, default=datetime.now()
)
end_date: Mapped[datetime] = mapped_column(
sa.DateTime, default=datetime.now()
)
price_per_night: Mapped[float] = mapped_column(sa.Float, default=0)
num_nights: Mapped[int] = mapped_column(
sa.Integer, nullable=False, default=1
)
user: Mapped[User] = sa.orm.relationship(User)
place: Mapped[Place] = sa.orm.relationship(Place)
class Review(Base):
__tablename__ = "review"
__table_args__ = (UniqueConstraint("booking_id"),)
id: Mapped[int] = mapped_column(sa.Integer, primary_key=True)
booking_id: Mapped[int] = mapped_column(
sa.Integer, sa.ForeignKey(Booking.id)
)
rating: Mapped[int] = mapped_column(sa.SmallInteger, nullable=True)
review_body: Mapped[str] = mapped_column(sa.Text, nullable=True)
booking: Mapped[Booking] = sa.orm.relationship(
Booking,
backref=sa.orm.backref("booking"),
)
def setup(config: str) -> None:
for doc in config_loader(config):
database: str = doc.get("database", doc["index"])
create_database(database)
with pg_engine(database) as engine:
Base.metadata.drop_all(engine)
Base.metadata.create_all(engine)
@click.command()
@click.option(
"--config",
"-c",
help="Schema config",
type=click.Path(exists=True),
)
def main(config):
config: str = get_config(config)
teardown(config=config)
setup(config)
if __name__ == "__main__":
main()