mirror of https://gitlab.com/KKlochko/tui-rsync
parent
2834ad8bee
commit
690cc7de9c
@ -0,0 +1,3 @@
|
||||
from .database_manager_port import DatabaseManagerPort
|
||||
|
||||
__all__ = ['DatabaseManagerPort']
|
@ -0,0 +1,11 @@
|
||||
from abc import ABC, abstractmethod
|
||||
|
||||
|
||||
class DatabaseManagerPort(ABC):
|
||||
@abstractmethod
|
||||
def get_connection(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def create_tables(self):
|
||||
pass
|
@ -0,0 +1,4 @@
|
||||
from .database_manager import DatabaseManager
|
||||
from .in_memory_database_manager import InMemoryDatabaseManager
|
||||
|
||||
__all__ = ['DatabaseManager', 'InMemoryDatabaseManager']
|
@ -0,0 +1,27 @@
|
||||
from typing import List
|
||||
from peewee import Model, Database
|
||||
from .models import BackupPlan, Destination
|
||||
from tui_rsync.core.ports.orm import DatabaseManagerPort
|
||||
|
||||
|
||||
class DatabaseManager(DatabaseManagerPort):
|
||||
models: List[Model] = [
|
||||
BackupPlan,
|
||||
Destination,
|
||||
]
|
||||
|
||||
def __init__(self, db: Database):
|
||||
self.db = db
|
||||
self._update_model_meta()
|
||||
self.create_tables()
|
||||
|
||||
def _update_model_meta(self):
|
||||
for model in self.models:
|
||||
model._meta.database = self.db
|
||||
|
||||
def get_connection(self):
|
||||
return self.db
|
||||
|
||||
def create_tables(self):
|
||||
with self.db:
|
||||
self.db.create_tables(self.models, safe=True)
|
@ -0,0 +1,7 @@
|
||||
from peewee import SqliteDatabase
|
||||
from .database_manager import DatabaseManager
|
||||
|
||||
|
||||
class InMemoryDatabaseManager(DatabaseManager):
|
||||
def __init__(self):
|
||||
super().__init__(SqliteDatabase(':memory:'))
|
@ -0,0 +1,16 @@
|
||||
from peewee import *
|
||||
import uuid
|
||||
|
||||
|
||||
class BackupPlan(Model):
|
||||
id = UUIDField(primary_key=True, default=uuid.uuid4)
|
||||
label = CharField()
|
||||
source = CharField()
|
||||
|
||||
class Meta:
|
||||
database = DatabaseProxy()
|
||||
|
||||
|
||||
class Destination(Model):
|
||||
source = ForeignKeyField(BackupPlan, backref='destinations')
|
||||
path = CharField()
|
@ -0,0 +1,7 @@
|
||||
from peewee import SqliteDatabase
|
||||
from .database_manager import DatabaseManager
|
||||
|
||||
|
||||
class SqliteDatabaseManager(DatabaseManager):
|
||||
def __init__(self, filepath='sync.db'):
|
||||
super().__init__(SqliteDatabase(filepath))
|
Loading…
Reference in new issue