-
Notifications
You must be signed in to change notification settings - Fork 265
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: postgres integration - create database (#932)
1. Add support for creating database ``` CREATE DATABASE db WITH ENGINE = "postgres", PARAMETERS = { "user": "user", "password": "password", "host": "127.0.0.1", "port": "5432", "database": "demo" }; ``` 2. Persist the info in the catalog 3. Todo: Verify if the provided info is valid - next PR
- Loading branch information
Showing
18 changed files
with
459 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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,47 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from sqlalchemy import Column, String | ||
|
||
from evadb.catalog.models.base_model import BaseModel | ||
from evadb.catalog.models.utils import DatabaseCatalogEntry, TextPickleType | ||
|
||
|
||
class DatabaseCatalog(BaseModel): | ||
"""The `DatabaseCatalog` catalog stores information about all databases. | ||
`_row_id:` an autogenerated unique identifier. | ||
`_name:` the database name. | ||
`_engine:` database engine | ||
`_param:` parameters specific to the database engine | ||
""" | ||
|
||
__tablename__ = "database_catalog" | ||
|
||
_name = Column("name", String(100), unique=True) | ||
_engine = Column("engine", String(100)) | ||
_params = Column("params", TextPickleType()) | ||
|
||
def __init__(self, name: str, engine: str, params: dict): | ||
self._name = name | ||
self._engine = engine | ||
self._params = params | ||
|
||
def as_dataclass(self) -> "DatabaseCatalogEntry": | ||
return DatabaseCatalogEntry( | ||
row_id=self._row_id, | ||
name=self._name, | ||
engine=self._engine, | ||
params=self._params, | ||
) |
This file contains 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 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,83 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from sqlalchemy.orm import Session | ||
from sqlalchemy.sql.expression import select | ||
|
||
from evadb.catalog.models.database_catalog import DatabaseCatalog | ||
from evadb.catalog.models.utils import DatabaseCatalogEntry | ||
from evadb.catalog.services.base_service import BaseService | ||
from evadb.utils.errors import CatalogError | ||
from evadb.utils.logging_manager import logger | ||
|
||
|
||
class DatabaseCatalogService(BaseService): | ||
def __init__(self, db_session: Session): | ||
super().__init__(DatabaseCatalog, db_session) | ||
|
||
def insert_entry( | ||
self, | ||
name: str, | ||
engine: str, | ||
params: dict, | ||
): | ||
try: | ||
db_catalog_obj = self.model( | ||
name=name, | ||
engine=engine, | ||
params=params, | ||
) | ||
db_catalog_obj = db_catalog_obj.save(self.session) | ||
|
||
except Exception as e: | ||
logger.exception( | ||
f"Failed to insert entry into database catalog with exception {str(e)}" | ||
) | ||
raise CatalogError(e) | ||
|
||
def get_entry_by_name(self, database_name: str) -> DatabaseCatalogEntry: | ||
""" | ||
Get the table catalog entry with given table name. | ||
Arguments: | ||
database_name (str): Database name | ||
Returns: | ||
DatabaseCatalogEntry - catalog entry for given database name | ||
""" | ||
entry = self.session.execute( | ||
select(self.model).filter(self.model._name == database_name) | ||
).scalar_one_or_none() | ||
if entry: | ||
return entry.as_dataclass() | ||
return entry | ||
|
||
def delete_entry(self, database_entry: DatabaseCatalogEntry): | ||
"""Delete database from the catalog | ||
Arguments: | ||
database (DatabaseCatalogEntry): database to delete | ||
Returns: | ||
True if successfully removed else false | ||
""" | ||
try: | ||
db_catalog_obj = self.session.execute( | ||
select(self.model).filter(self.model._row_id == database_entry.row_id) | ||
).scalar_one_or_none() | ||
db_catalog_obj.delete(self.session) | ||
return True | ||
except Exception as e: | ||
err_msg = ( | ||
f"Delete database failed for {database_entry} with error {str(e)}." | ||
) | ||
logger.exception(err_msg) | ||
raise CatalogError(err_msg) |
This file contains 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 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,48 @@ | ||
# coding=utf-8 | ||
# Copyright 2018-2023 EvaDB | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
import pandas as pd | ||
|
||
from evadb.database import EvaDBDatabase | ||
from evadb.executor.abstract_executor import AbstractExecutor | ||
from evadb.models.storage.batch import Batch | ||
from evadb.parser.create_statement import CreateDatabaseStatement | ||
from evadb.utils.logging_manager import logger | ||
|
||
|
||
class CreateDatabaseExecutor(AbstractExecutor): | ||
def __init__(self, db: EvaDBDatabase, node: CreateDatabaseStatement): | ||
super().__init__(db, node) | ||
|
||
def exec(self, *args, **kwargs): | ||
# todo handle if_not_exists | ||
|
||
logger.debug( | ||
f"Trying to connect to the provided engine {self.node.engine} with params {self.node.param_dict}" | ||
) | ||
# todo handle if the provided database params are valid | ||
|
||
logger.debug(f"Creating database {self.node}") | ||
|
||
self.catalog().insert_database_catalog_entry( | ||
self.node.database_name, self.node.engine, self.node.param_dict | ||
) | ||
|
||
yield Batch( | ||
pd.DataFrame( | ||
[ | ||
f"The database {self.node.database_name} has been successfully created." | ||
] | ||
) | ||
) |
This file contains 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
Oops, something went wrong.