-
Notifications
You must be signed in to change notification settings - Fork 1
Added sqlalchemy to the project #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ZapAnton
wants to merge
5
commits into
master
Choose a base branch
from
add_sqlalchemy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7e9fdd4
added sqlalchemy dependency
ZapAnton b2b1fcc
added SQLite bases to .gitignore
ZapAnton ddc1f5d
Added sqlite db initialization
ZapAnton d41ec31
passing db engine to the cli module
hartred 8514459
Integrated sqlalchemy into the all_styles function
hartred File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,4 +3,5 @@ venv/ | |
| .idea/ | ||
| *.pyc | ||
| *.swp | ||
| .mypy_cache/ | ||
| .mypy_cache/ | ||
| *.db | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| import os | ||
|
|
||
| from typing import List | ||
|
|
||
| from sqlalchemy import create_engine, MetaData, Table, Column, Integer, String | ||
| from sqlalchemy.sql import select | ||
| import sqlalchemy_utils | ||
|
|
||
| SQLITE_DB_PATH = 'styles.db' | ||
| SQLITE_URL = f'sqlite:///{SQLITE_DB_PATH}' | ||
|
|
||
|
|
||
| class DBConfig(): | ||
|
|
||
| STYLE_TABLE_NAME = 'styles' | ||
|
|
||
| def __init__(self, db_url: str): | ||
| self.__engine = None | ||
| self.meta = None | ||
| self.db_url = db_url | ||
| self.__styles = None | ||
|
|
||
| def create_styles_table(self): | ||
| self.meta = MetaData() | ||
| if not self.styles_table_exists(): | ||
| self.__styles = Table( | ||
| self.STYLE_TABLE_NAME, | ||
| self.meta, | ||
| Column('id', Integer, primary_key=True), | ||
| Column('name', String), | ||
| Column('body', String), | ||
| ) | ||
| self.meta.create_all(self.__engine) | ||
|
|
||
| conn = self.__engine.connect() | ||
| conn.execute(self.__styles.insert().values(name='hash_border', | ||
| body='')) | ||
| conn.execute(self.__styles.insert().values(name='plain_text', | ||
| body='')) | ||
| conn.execute(self.__styles.insert().values(name='at_sign', | ||
| body='')) | ||
| else: | ||
| self.__styles = Table( | ||
| self.STYLE_TABLE_NAME, | ||
| self.meta, | ||
| Column('id', Integer, primary_key=True), | ||
| Column('name', String), | ||
| Column('body', String), | ||
| ) | ||
|
|
||
| def connect(self): | ||
| self.__engine = create_engine(self.db_url, echo=True) | ||
|
|
||
| def styles_table_exists(self) -> bool: | ||
| return self.__engine.dialect.has_table(self.__engine, | ||
| self.STYLE_TABLE_NAME) | ||
|
|
||
| def list(self) -> List[str]: | ||
| conn = self.__engine.connect() | ||
| select_query = select([self.__styles]) | ||
| styles = conn.execute(select_query) | ||
| all_styles: List[str] = [style.name for style in styles] | ||
| return all_styles | ||
|
|
||
|
|
||
| def get_db_object(): | ||
| db_config = DBConfig(SQLITE_URL) | ||
| db_config.connect() | ||
| db_config.create_styles_table() | ||
| return db_config | ||
|
|
||
|
|
||
| def init_styles_db(): | ||
| return get_db_object() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| SQLAlchemy==1.3.12 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Better use combined
DATABASE_URLand make it possible to override fromos.environ(https://12factor.net/backing-services).