prepare migration script to add ulid column
This commit is contained in:
parent
ccf0b2b688
commit
865472af51
3 changed files with 52 additions and 1 deletions
38
dbmigration/migrate_from_2.1_to_3.0.py
Normal file
38
dbmigration/migrate_from_2.1_to_3.0.py
Normal file
|
@ -0,0 +1,38 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: UTF-8 -*-
|
||||
|
||||
import sqlite3
|
||||
import datetime
|
||||
from ulid import ULID
|
||||
|
||||
# add column ulid
|
||||
connection = sqlite3.connect("db.sqlite")
|
||||
cursor = connection.cursor()
|
||||
script = """
|
||||
PRAGMA foreign_keys = OFF;
|
||||
BEGIN TRANSACTION;
|
||||
ALTER TABLE comment ADD ulid INTEGER;
|
||||
COMMIT;
|
||||
PRAGMA foreign_keys = ON;
|
||||
"""
|
||||
cursor.executescript(script)
|
||||
connection.close()
|
||||
|
||||
# fill in ulid column
|
||||
connection = sqlite3.connect("db.sqlite")
|
||||
cursor = connection.cursor()
|
||||
updates = []
|
||||
for row in cursor.execute('SELECT * FROM comment'):
|
||||
row_id = row[0]
|
||||
string_created = row[2]
|
||||
date_created = datetime.datetime.strptime(string_created, "%Y-%m-%d %H:%M:%S")
|
||||
ulid = ULID.from_datetime(date_created)
|
||||
update = "UPDATE comment SET ulid = " + str(int(ulid)) + " WHERE id = " + str(row_id)
|
||||
print(update)
|
||||
updates.append(update)
|
||||
|
||||
for update in updates:
|
||||
pass
|
||||
connection.execute(update)
|
||||
connection.commit()
|
||||
connection.close()
|
14
poetry.lock
generated
14
poetry.lock
generated
|
@ -495,6 +495,14 @@ python-versions = "!=3.0.*,!=3.1.*,!=3.2.*,>=2.7"
|
|||
[package.dependencies]
|
||||
six = ">=1.5"
|
||||
|
||||
[[package]]
|
||||
name = "python-ulid"
|
||||
version = "1.0.3"
|
||||
description = "Universally Unique Lexicographically Sortable Identifier"
|
||||
category = "main"
|
||||
optional = false
|
||||
python-versions = "*"
|
||||
|
||||
[[package]]
|
||||
name = "pytz"
|
||||
version = "2021.3"
|
||||
|
@ -688,7 +696,7 @@ testing = ["pytest (>=6)", "pytest-checkdocs (>=2.4)", "pytest-flake8", "pytest-
|
|||
[metadata]
|
||||
lock-version = "1.1"
|
||||
python-versions = "^3.8"
|
||||
content-hash = "79d96a01febb3d55127734be5b86aefdaab09d1f400efd8cbbcd55faf00030cb"
|
||||
content-hash = "24b77862cfbece0c68447f4d026bed51431e3a655a92dde2697368e758bfac89"
|
||||
|
||||
[metadata.files]
|
||||
appdirs = [
|
||||
|
@ -1000,6 +1008,10 @@ python-dateutil = [
|
|||
{file = "python-dateutil-2.8.2.tar.gz", hash = "sha256:0123cacc1627ae19ddf3c27a5de5bd67ee4586fbdd6440d9748f8abb483d3e86"},
|
||||
{file = "python_dateutil-2.8.2-py2.py3-none-any.whl", hash = "sha256:961d03dc3453ebbc59dbdea9e4e11c5651520a876d0f4db161e8674aae935da9"},
|
||||
]
|
||||
python-ulid = [
|
||||
{file = "python-ulid-1.0.3.tar.gz", hash = "sha256:5dd8b969312a40e2212cec9c1ad63f25d4b6eafd92ee3195883e0287b6e9d19e"},
|
||||
{file = "python_ulid-1.0.3-py3-none-any.whl", hash = "sha256:8704dc20f547f531fe3a41d4369842d737a0f275403b909d0872e7ea0fe8d6f2"},
|
||||
]
|
||||
pytz = [
|
||||
{file = "pytz-2021.3-py2.py3-none-any.whl", hash = "sha256:3672058bc3453457b622aab7a1c3bfd5ab0bdae451512f6cf25f64ed37f5b87c"},
|
||||
{file = "pytz-2021.3.tar.gz", hash = "sha256:acad2d8b20a1af07d4e4c9d2e9285c5ed9104354062f275f3fcd88dcef4f1326"},
|
||||
|
|
|
@ -18,6 +18,7 @@ requests = "^2.25.1"
|
|||
coverage = "^5.5"
|
||||
peewee = "^3.14.8"
|
||||
tox = "^3.24.5"
|
||||
python-ulid = "^1.0.3"
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
rope = "^0.16.0"
|
||||
|
|
Loading…
Add table
Reference in a new issue