class Database: """Инициализатор базы данных""" __database_proxy = DatabaseProxy() @classmethod def init(cls): """Инициализация базы данных""" cfg = Config.get_database_config() db_name = cfg.pop('db_name', "mu") psql_db = PostgresqlDatabase(db_name, **cfg) cls.__database_proxy.initialize(psql_db) @classmethod def get(cls): """Получение прокси базы данных""" return cls.__database_proxy
class Database: """SQL System""" __db = DatabaseProxy() __migrator = None @classmethod def setup(cls): """sets up the SQL System""" mode = CONFIG["database"]["mode"].value.lower() if mode == "sqlite3": database = SqliteExtDatabase( PROGRAMCONFIGLOCATION + "/Tackem.db", pragmas={ "journal_mode": "wal", "foreign_keys": 0 }, ) elif mode == "mysql": database = MySQLDatabase( CONFIG["database"]["database"].value, user=CONFIG["database"]["username"].value, password=CONFIG["database"]["password"].value, host=CONFIG["database"]["host"].value, port=CONFIG["database"]["port"].value, ) else: print(mode) cls.__db.initialize(database) if mode == "sqlite3": cls.__migrator = SqliteMigrator(cls.__db) elif mode == "mysql": cls.__migrator = MySQLMigrator(cls.__db) TableVersion.create_table() @classproperty def db(cls) -> DatabaseProxy: """returns the DB""" return cls.__db @classproperty def migrator(cls) -> SchemaMigrator: """returns the migrator""" return cls.__migrator
from datetime import datetime from peewee import Model, CharField, DatabaseProxy, ForeignKeyField, BooleanField, DateTimeField, IntegerField, \ CompositeKey, DateField from config import config db_proxy = DatabaseProxy() class Migration(Model): version = IntegerField() class Meta: database = db_proxy class TimeSlot(Model): start_date_time = DateTimeField() length_min = IntegerField() class Meta: database = db_proxy class Appointment(Model): claim_token = CharField(null=True) claimed_at = DateTimeField(null=True) booked = BooleanField() time_slot = ForeignKeyField(TimeSlot, backref='appointments')
from peewee import DatabaseProxy, Model, CharField, DateTimeField from playhouse.db_url import connect database_proxy = DatabaseProxy() # Create a proxy for our db. class BaseModel(Model): class Meta: database = database_proxy # Use proxy for our DB. class Forecast(BaseModel): weather_type = CharField() temperature = CharField() date = DateTimeField() def init_db(url): """ The function to dynamic initialization of the database depending on the transmitted url ex.: - sqlite:///my_database.db will create a SqliteDatabase - postgresql://postgres:my_password@localhost:5432/my_database will create a PostgresqlDatabase instance. - mysql://user:passwd@ip:port/my_db will create a MySQLDatabase @param url: str @return:None """ database = connect(url) database_proxy.initialize(database) database_proxy.create_tables([Forecast])
import pymysql from peewee import ( CharField, DatabaseProxy, DateTimeField, Model, MySQLDatabase, SqliteDatabase, ) # Create a proxy for the DB. This is because we don't know what kind of DB # we are connecting to until runtime. We can create either a Sqlite DB or # an RDS DB. The Sqlite DB is a local DB stored in a file and can be very # useful for testing. database_proxy = DatabaseProxy() db = None class BaseModel(Model): class Meta: database = database_proxy class Schedule(BaseModel): instance_id = CharField(max_length=20, unique=True, null=False) start_at = DateTimeField(null=False) stop_at = DateTimeField(null=False)
CompositeKey, DatabaseProxy, DateTimeField, FloatField, ForeignKeyField, IntegerField, Model, PostgresqlDatabase, TextField, ) from scripts.pipelines import entity_pipelines DATABASE_CONFIG = "config.ini" input_database = DatabaseProxy() output_database = DatabaseProxy() class InputModel(Model): # type: ignore """ Input models are read but never written. These specifications of input models in this file only need to define the fields we want to read, and they may not be a complete specification of the table (i.e. they might leave out indexes or columns). """ class Meta: database = input_database class Metadata(InputModel): metadata_id = TextField()
class Meta: database = DatabaseProxy()
import os import logging import time from peewee import (DatabaseProxy, SqliteDatabase, MySQLDatabase, Model) from playhouse.shortcuts import ReconnectMixin from ..config import get_db_config LOGGER = logging.getLogger(__name__) db = DatabaseProxy() class ReconnectMySQLDatabase(ReconnectMixin, MySQLDatabase): def __init__(self, *args, **kwargs): self.max_attempts = kwargs.pop("max_attempts", None) if self.max_attempts: self.max_attempts = int(self.max_attempts) super().__init__(*args, **kwargs) def connect(self, *args, **kwargs): delay = 1 attempt_n = 1 while True: try: super().connect(*args, **kwargs)
MySQLMigrator, PostgresqlMigrator ) from peewee import ( fn, DatabaseProxy, Model, CharField, TextField, IntegerField, ForeignKeyField, BooleanField, DateTimeField ) database = DatabaseProxy() def set_up_logging(): import logging logger = logging.getLogger('peewee') logger.addHandler(logging.StreamHandler()) logger.setLevel(logging.DEBUG) def init_app(app): def open_db(): database.connect() def close_db(exception): if not database.is_closed():
from peewee import Model, CharField, DateTimeField, DatabaseProxy from datetime import datetime # NOTE: It may be more conventional to name this file ORM.py db = DatabaseProxy() # Set to DatabaseProxy() to enable setting db at runtime # There are other ways to do this # http://docs.peewee-orm.com/en/latest/peewee/database.html#setting-the-database-at-run-time # Base Database model, actual models should inherit this class BaseModel(Model): class Meta: database = db class User(BaseModel): def __init__(self, *args: str, **kwargs: str): # PLEASE CHECK super(User, self).__init__(*args, **kwargs) now = datetime.now() self.join_date = now self.last_changed = now # Should we have an numerical ID field or just use username as key? username = CharField(unique=True) password = CharField email = CharField join_date = DateTimeField last_changed = DateTimeField # Last time user was updated
from peewee import ForeignKeyField, CharField, IntegerField, DateTimeField, TextField # Local imports from rewardify._util import rarity_filter from rewardify.password import PasswordField from rewardify.rarity import Rarity from rewardify.rarity import RarityField from rewardify.probability import ProbabilityField # GENERAL SETUP # ############# DATABASE_PROXY = DatabaseProxy() class BaseModel(Model): """ This is the abstract base class for all the models of this program. It defines the connection to a database proxy (a placeholder for a database), which can be bound to an actual instance of a database at runtime (when the login info to the database has been read from a config file possibly) CHANGELOG Added 09.06.2019 """ class Meta: database = DATABASE_PROXY
from peewee import fn, DoesNotExist import time from elasticsearch import Elasticsearch from playhouse.shortcuts import model_to_dict from playhouse.postgres_ext import Match import six import math import zerorpc logger = get_logger('search project learning') app = Flask(__name__, static_folder='statics', static_url_path='/static') api = Api(app) app.config['MAX_CONTENT_LENGTH'] = 256 * 1024 * 1024 es = Elasticsearch(['es01:9200']) psql_db = DatabaseProxy() def json_encoder_default(obj): datetime_format = "%Y/%m/%d %H:%M:%S" date_format = "%Y/%m/%d" time_format = "%H:%M:%S" # if isinstance(obj, Decimal): # return str(obj) if isinstance(obj, datetime.datetime): return obj.strftime(datetime_format) if isinstance(obj, datetime.date): return obj.strftime(date_format) if isinstance(obj, datetime.time): return obj.strftime(time_format) if isinstance(obj, memoryview):
def __init__(self, config): self.cfg = config self.instances = {} self.defaut_instance = self.cfg.get('db', 'database') self.sqlite_db_path = self.cfg.get('sqlite', 'path') self.database_proxy = DatabaseProxy()
from typing import List from uuid import uuid4 from peewee import DatabaseProxy, Model, PostgresqlDatabase, SqliteDatabase, TextField, UUIDField from psycopg2 import connect from psycopg2.errors import DuplicateDatabase from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT from .configuration import Configuration, EntityStruct, _IN_USER_DIR, _try_dir DB = DatabaseProxy() class BaseModel(Model): class Meta: database = DB class Entity(BaseModel): """Some stub entity""" uuid = UUIDField() data = TextField() def _create_psql_database(db_name, user, password, host, port): conn = connect(dbname="postgres", user=user, password=password, host=host, port=port)