Skip to content

Commit b4d9c27

Browse files
authored
feat: SQL Database support (#143)
1 parent bb77c58 commit b4d9c27

11 files changed

Lines changed: 261 additions & 4 deletions

File tree

.github/workflows/test.yaml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ jobs:
3333
# Run tox using the version of Python in `PATH`
3434
run: tox -e py
3535
- name: Upload coverage to Codecov
36-
uses: codecov/codecov-action@v1
36+
continue-on-error: true
37+
uses: codecov/codecov-action@v4.0.1
3738
with:
3839
fail_ci_if_error: true
3940
token: ${{ secrets.CODECOV_TOKEN }}

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
.nitric/
33
nitric.yaml
44
nitric.*.yaml
5-
proto/
5+
/proto/
6+
/nitric/proto/KeyValue
67

78
# Byte-compiled / optimized / DLL files
89
__pycache__/

makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ test:
2121
@echo Running Tox tests
2222
@tox -e py
2323

24-
NITRIC_VERSION := 1.1.0
24+
NITRIC_VERSION := 1.6.0
2525

2626
download-local:
2727
@rm -r ./proto/nitric

nitric/application.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ class Nitric:
3939
"websocket": {},
4040
"keyvaluestore": {},
4141
"oidcsecuritydefinition": {},
42+
"sql": {},
4243
}
4344

4445
@classmethod

nitric/proto/deployments/v1/__init__.py

Lines changed: 11 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitric/proto/resources/v1/__init__.py

Lines changed: 19 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitric/proto/sql/__init__.py

Whitespace-only changes.

nitric/proto/sql/v1/__init__.py

Lines changed: 77 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

nitric/resources/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
from nitric.resources.topics import Topic, topic
2727
from nitric.resources.websockets import Websocket, websocket
2828
from nitric.resources.queues import Queue, queue
29+
from nitric.resources.sql import Sql, sql
2930

3031
__all__ = [
3132
"api",
@@ -44,6 +45,8 @@
4445
"schedule",
4546
"secret",
4647
"Secret",
48+
"sql",
49+
"Sql",
4750
"topic",
4851
"Topic",
4952
"websocket",

nitric/resources/sql.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#
2+
# Copyright (c) 2021 Nitric Technologies Pty Ltd.
3+
#
4+
# This file is part of Nitric Python 3 SDK.
5+
# See https://github.com/nitrictech/python-sdk for further info.
6+
#
7+
# Licensed under the Apache License, Version 2.0 (the "License");
8+
# you may not use this file except in compliance with the License.
9+
# You may obtain a copy of the License at
10+
#
11+
# http://www.apache.org/licenses/LICENSE-2.0
12+
#
13+
# Unless required by applicable law or agreed to in writing, software
14+
# distributed under the License is distributed on an "AS IS" BASIS,
15+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
# See the License for the specific language governing permissions and
17+
# limitations under the License.
18+
#
19+
from __future__ import annotations
20+
21+
from typing import Union
22+
23+
from grpclib import GRPCError
24+
from grpclib.client import Channel
25+
26+
from nitric.exception import exception_from_grpc_error
27+
from nitric.proto.resources.v1 import (
28+
SqlDatabaseResource,
29+
SqlDatabaseMigrations,
30+
ResourceDeclareRequest,
31+
ResourceIdentifier,
32+
ResourceType,
33+
)
34+
from nitric.resources.resource import Resource as BaseResource
35+
from nitric.utils import new_default_channel
36+
from nitric.application import Nitric
37+
38+
from nitric.proto.sql.v1 import SqlStub, SqlConnectionStringRequest
39+
40+
41+
class Sql(BaseResource):
42+
"""A SQL Database."""
43+
44+
_channel: Channel
45+
_sql_stub: SqlStub
46+
name: str
47+
migrations: Union[str, None]
48+
49+
def __init__(self, name: str, migrations: Union[str, None] = None):
50+
"""Construct a new SQL Database."""
51+
super().__init__(name)
52+
53+
self._channel: Union[Channel, None] = new_default_channel()
54+
self._sql_stub = SqlStub(channel=self._channel)
55+
self.name = name
56+
self.migrations = migrations
57+
58+
async def _register(self) -> None:
59+
try:
60+
await self._resources_stub.declare(
61+
resource_declare_request=ResourceDeclareRequest(
62+
id=ResourceIdentifier(name=self.name, type=ResourceType.SqlDatabase),
63+
sql_database=SqlDatabaseResource(
64+
migrations=SqlDatabaseMigrations(migrations_path=self.migrations if self.migrations else "")
65+
),
66+
),
67+
)
68+
except GRPCError as grpc_err:
69+
raise exception_from_grpc_error(grpc_err) from grpc_err
70+
71+
async def connection_string(self) -> str:
72+
"""Return the connection string for this SQL Database."""
73+
response = await self._sql_stub.connection_string(SqlConnectionStringRequest(database_name=self.name))
74+
75+
return response.connection_string
76+
77+
78+
def sql(name: str, migrations: Union[str, None] = None) -> Sql:
79+
"""
80+
Create and register a sql database.
81+
82+
If a sql databse has already been registered with the same name, the original reference will be reused.
83+
"""
84+
return Nitric._create_resource(Sql, name, migrations)

0 commit comments

Comments
 (0)