summaryrefslogtreecommitdiffstats
path: root/alembic/versions/4a3773e332a0_use_utc_datetime_in_db.py.old
blob: e011a3a1d51ab1abeb21f94e57c58e999143b29f (plain) (blame)
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
"""use UTC DateTime in DB

Revision ID: 4a3773e332a0
Revises: 
Create Date: 2022-11-15 17:35:11.717714

"""
from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision = '4a3773e332a0'
down_revision = None
branch_labels = None
depends_on = None


def upgrade() -> None:
	# ### commands auto generated by Alembic - please adjust! ###
	op.add_column('borrows', sa.Column('purchase_utc', sa.DateTime(), nullable=True))
	op.add_column('borrows', sa.Column('expiration_utc', sa.DateTime(), nullable=True))
	op.add_column('borrows', sa.Column('purchase_timezone', sa.Integer(), nullable=True))
	op.add_column('borrows', sa.Column('expiration_timezone', sa.Integer(), nullable=True))
	borrows = sa.Table(
		"borrows",
		sa.MetaData(),
		sa.Column("id", sa.Integer, primary_key=True, nullable=False),
		sa.Column("purchase", sa.String, nullable=True),
		sa.Column("expiration", sa.String, nullable=True),
		sa.Column("purchase_utc", sa.DateTime, nullable=True),
		sa.Column("expiration_utc", sa.DateTime, nullable=True),
		sa.Column("purchase_timezone", sa.Integer, nullable=True),
		sa.Column("expiration_timezone", sa.Integer, nullable=True)
	)
	connection = op.get_bind()
	results = connection.execute(sa.select([
		borrows.c.id,
		borrows.c.purchase,
		borrows.c.expiration,
		borrows.c.purchase_utc,
		borrows.c.expiration_utc,
		borrows.c.purchase_timezone,
		borrows.c.expiration_timezone
	])).fetchall()
	from datetime import datetime, timezone
	for id, purchase, expiration, purchase_utc, expiration_utc, purchase_timezone, expiration_timezone in results:
		if id % 1000 == 0:
			print(f"... obdelujem id {id}", end="\r")
		if purchase == None:
			print(f"at id {id} purchase is None")
			continue
		purchase_utc = datetime.strptime(purchase, "%Y-%m-%dT%H:%M:%S%z")
		expiration_utc = datetime.strptime(expiration, "%Y-%m-%dT%H:%M:%S%z")
		purchase_timezone = purchase_utc.tzinfo.utcoffset(None).seconds
		expiration_timezone = expiration_utc.tzinfo.utcoffset(None).seconds
		purchase_utc = purchase_utc.astimezone(timezone.utc).replace(tzinfo=None)
		expiration_utc = expiration_utc.astimezone(timezone.utc).replace(tzinfo=None)
		connection.execute(borrows.update().where(borrows.c.id == id).values(
			purchase_utc = purchase_utc,
			expiration_utc = expiration_utc,
			purchase_timezone = purchase_timezone,
			expiration_timezone = expiration_timezone
		))
	op.drop_column('borrows', 'expiration')
	op.drop_column('borrows', 'purchase')
	# ### end Alembic commands ###


def downgrade() -> None:
	# ### commands auto generated by Alembic - please adjust! ###
	op.add_column('borrows', sa.Column('purchase', sa.VARCHAR(), nullable=True))
	op.add_column('borrows', sa.Column('expiration', sa.VARCHAR(), nullable=True))
	borrows = sa.Table(
		"borrows",
		sa.MetaData(),
		sa.Column("id", sa.Integer, primary_key=True, nullable=False),
		sa.Column("purchase", sa.String, nullable=True),
		sa.Column("expiration", sa.String, nullable=True),
		sa.Column("purchase_utc", sa.DateTime, nullable=True),
		sa.Column("expiration_utc", sa.DateTime, nullable=True),
		sa.Column("purchase_timezone", sa.Integer, nullable=True),
		sa.Column("expiration_timezone", sa.Integer, nullable=True)
	)
	connection = op.get_bind()
	results = connection.execute(sa.select([
		borrows.c.id,
		borrows.c.purchase,
		borrows.c.expiration,
		borrows.c.purchase_utc,
		borrows.c.expiration_utc,
		borrows.c.purchase_timezone,
		borrows.c.expiration_timezone
	])).fetchall()
	from datetime import datetime, timezone, timedelta
	for id, purchase, expiration, purchase_utc, expiration_utc, purchase_timezone, expiration_timezone in results:
		if id % 1000 == 0:
			print(f"... obdelujem id {id}", end="\r")
		if purchase_utc == None:
			print(f"at id {id} purchase_utc is None")
			continue
		connection.execute(borrows.update().where(borrows.c.id == id).values(
			purchase = purchase_utc.astimezone(timezone(timedelta(seconds=purchase_timezone))).isoformat(),
			expiration = expiration_utc.astimezone(timezone(timedelta(seconds=expiration_timezone))).isoformat()
		))
	op.drop_column('borrows', 'expiration_timezone')
	op.drop_column('borrows', 'purchase_timezone')
	op.drop_column('borrows', 'expiration_utc')
	op.drop_column('borrows', 'purchase_utc')
	# ### end Alembic commands ###