diff --git a/tui_rsync/core/ports/orm/__init__.py b/tui_rsync/core/ports/orm/__init__.py new file mode 100644 index 0000000..3cb5d66 --- /dev/null +++ b/tui_rsync/core/ports/orm/__init__.py @@ -0,0 +1,3 @@ +from .database_manager_port import DatabaseManagerPort + +__all__ = ['DatabaseManagerPort'] diff --git a/tui_rsync/core/ports/orm/database_manager_port.py b/tui_rsync/core/ports/orm/database_manager_port.py new file mode 100644 index 0000000..b1f12cc --- /dev/null +++ b/tui_rsync/core/ports/orm/database_manager_port.py @@ -0,0 +1,11 @@ +from abc import ABC, abstractmethod + + +class DatabaseManagerPort(ABC): + @abstractmethod + def get_connection(self): + pass + + @abstractmethod + def create_tables(self): + pass diff --git a/tui_rsync/infrastructure/orm/__init__.py b/tui_rsync/infrastructure/orm/__init__.py new file mode 100644 index 0000000..2bd190d --- /dev/null +++ b/tui_rsync/infrastructure/orm/__init__.py @@ -0,0 +1,4 @@ +from .database_manager import DatabaseManager +from .in_memory_database_manager import InMemoryDatabaseManager + +__all__ = ['DatabaseManager', 'InMemoryDatabaseManager'] diff --git a/tui_rsync/infrastructure/orm/database_manager.py b/tui_rsync/infrastructure/orm/database_manager.py new file mode 100644 index 0000000..e5e7726 --- /dev/null +++ b/tui_rsync/infrastructure/orm/database_manager.py @@ -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) diff --git a/tui_rsync/infrastructure/orm/in_memory_database_manager.py b/tui_rsync/infrastructure/orm/in_memory_database_manager.py new file mode 100644 index 0000000..fc2cf6a --- /dev/null +++ b/tui_rsync/infrastructure/orm/in_memory_database_manager.py @@ -0,0 +1,7 @@ +from peewee import SqliteDatabase +from .database_manager import DatabaseManager + + +class InMemoryDatabaseManager(DatabaseManager): + def __init__(self): + super().__init__(SqliteDatabase(':memory:')) diff --git a/tui_rsync/infrastructure/orm/models.py b/tui_rsync/infrastructure/orm/models.py new file mode 100644 index 0000000..92e83f9 --- /dev/null +++ b/tui_rsync/infrastructure/orm/models.py @@ -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() diff --git a/tui_rsync/infrastructure/orm/sqlite_database_manager.py b/tui_rsync/infrastructure/orm/sqlite_database_manager.py new file mode 100644 index 0000000..0881000 --- /dev/null +++ b/tui_rsync/infrastructure/orm/sqlite_database_manager.py @@ -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))