Skip to content

Commit ca8f67c

Browse files
author
lx-dev
committed
feat(database): moving db migration scripts in lib src
1 parent 8563ee5 commit ca8f67c

File tree

5 files changed

+113
-5
lines changed

5 files changed

+113
-5
lines changed

src/astraeus_common/io/database.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from psycopg2.extras import DictCursor
88
from yoyo import read_migrations, get_backend
99

10+
PING_QUERY = 'SELECT 1'
1011

1112
class Database:
1213
_db_host: str
@@ -22,20 +23,20 @@ def __init__(self, db_host: str, db_port: str, db_user: str, db_name: str, db_pa
2223
self._db_name = db_name
2324
self._db_password = db_password
2425
self._log = structlog.get_logger()
25-
self.__try_connection()
26+
self.__try_connection(PING_QUERY)
2627
self.__apply_migration()
28+
self.__try_connection('SELECT * FROM ASTRAEUS.BODY')
2729

2830
def __apply_migration(self):
2931
backend = get_backend(f'postgresql://{self._db_user}:{self._db_password}@'
3032
f'{self._db_host}:{self._db_port}/{self._db_name}')
31-
migrations = read_migrations('../../../db')
33+
migrations = read_migrations('./db')
3234
backend.apply_migrations(backend.to_apply(migrations))
3335

34-
def __try_connection(self):
36+
def __try_connection(self, query: str):
3537
with self.__db_connection() as connection:
3638
with connection.cursor(cursor_factory=DictCursor) as cursor:
37-
cursor.execute('SELECT 1')
38-
cursor.execute('SELECT * FROM ASTRAEUS.BODY')
39+
cursor.execute(query)
3940

4041
def __db_connection(self):
4142
try:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
CREATE SCHEMA IF NOT EXISTS "astraeus";
2+
3+
CREATE TABLE IF NOT EXISTS "astraeus"."system"
4+
(
5+
"key" jsonb PRIMARY KEY,
6+
"name" text,
7+
"coordinates" jsonb,
8+
"require_permit" boolean,
9+
"information" jsonb,
10+
"update_time" timestamp,
11+
"primary_star" jsonb
12+
);
13+
14+
CREATE TABLE IF NOT EXISTS "astraeus"."body"
15+
(
16+
"key" jsonb PRIMARY KEY,
17+
"system_key" jsonb,
18+
"name" text,
19+
"type" text,
20+
"sub_type" text,
21+
"discovery" jsonb,
22+
"update_time" timestamp,
23+
"materials" jsonb,
24+
"solid_composition" jsonb,
25+
"atmosphere_composition" jsonb,
26+
"parents" jsonb,
27+
"belts" jsonb,
28+
"rings" jsonb,
29+
"properties" jsonb
30+
);
31+
32+
ALTER TABLE "astraeus"."body"
33+
ADD FOREIGN KEY (system_key) REFERENCES "astraeus"."system" (key);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- depends: 001.initial-db-creation
2+
3+
CREATE TABLE IF NOT EXISTS "astraeus"."sync_state"
4+
(
5+
"key" jsonb PRIMARY KEY,
6+
"sync_date" timestamp not null,
7+
"type" text not null,
8+
"sync_hash" text,
9+
"previous_state" jsonb
10+
);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- depends: 001.initial-db-creation
2+
3+
CREATE TABLE IF NOT EXISTS "astraeus"."eddn_message"
4+
(
5+
"id" uuid PRIMARY KEY default uuid_generate_v4(),
6+
"schema" text not null,
7+
"header" jsonb not null,
8+
"message" jsonb not null,
9+
"recv_date" timestamp not null,
10+
"sync_date" timestamp
11+
);

src/astraeus_common/models/body.py

+53
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,59 @@ class Body:
1818
_rings: dict
1919
_properties: dict
2020

21+
BODY_SELECT_BY_KEY = '''
22+
select key, system_key, name, type,
23+
sub_type, discovery, update_time, materials,
24+
solid_composition, atmosphere_composition, parents, belts,
25+
rings, properties
26+
from body
27+
where key = %(key)s
28+
'''
29+
30+
BODY_SELECT_BY_SYSTEM_KEY = '''
31+
select key, system_key, name, type,
32+
sub_type, discovery, update_time, materials,
33+
solid_composition, atmosphere_composition, parents, belts,
34+
rings, properties
35+
from body
36+
where system_key = %(system_key)s
37+
'''
38+
39+
BODY_INSERT = '''
40+
insert into body
41+
(key, system_key, name, type,
42+
sub_type, discovery, update_time, materials,
43+
solid_composition, atmosphere_composition, parents,
44+
belts, rings, properties)
45+
values
46+
(%(key)s, %(system_key)s, %(name)s, %(type)s,
47+
%(sub_type)s, %(discovery)s, %(update_time)s, %(materials)s,
48+
%(solid_composition)s, %(atmosphere_composition)s, %(parents)s,
49+
%(belts)s, %(rings)s, %(properties)s)
50+
'''
51+
52+
BODY_UPDATE_BY_KEY = '''
53+
update body
54+
set system_key = %(system_key)s,
55+
name = %(name)s,
56+
type = %(type)s,
57+
sub_type = %(sub_type)s,
58+
discovery = %(discovery)s,
59+
update_time = %(update_time)s,
60+
materials = %(materials)s,
61+
solid_composition = %(solid_composition)s,
62+
atmosphere_composition = %(atmosphere_composition)s,
63+
parents = %(parents)s,
64+
belts = %(belts)s,
65+
rings = %(rings)s,
66+
properties = %(properties)s
67+
where key = %(key)s
68+
'''
69+
70+
BODY_DELETE_BY_KEY = '''
71+
delete from body where key = %(key)s
72+
'''
73+
2174
@property
2275
def key(self) -> dict:
2376
return self._key

0 commit comments

Comments
 (0)