Skip to content
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

113 Read manifest from database #495

Merged
merged 69 commits into from
Jan 24, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
69 commits
Select commit Hold shift + click to select a range
3b64f86
113 added internal manifest, added write
adp-atea Jul 24, 2023
66b8d84
113 added sql to tabular converter
adp-atea Jul 26, 2023
396dda3
113 Fixed prepare json null
adp-atea Jul 26, 2023
3629e0b
113 Added tests, added ids to tabular
adp-atea Aug 1, 2023
56a6b32
Merge branch 'master' into 113-read-manifest-from-database
adp-atea Aug 1, 2023
a1de5e5
Testing notes
sirex Nov 13, 2023
6623d06
Merge branch 'master' into 113-read-manifest-from-database
adp-atea Nov 21, 2023
5663392
113 updated tests
adp-atea Nov 21, 2023
c961641
113 merge fix
adp-atea Nov 22, 2023
ce9f7d6
113 refactored manifest.objects, removed Project, Owner and endpoints
adp-atea Nov 23, 2023
23ceeb6
113 fixed typo
adp-atea Nov 23, 2023
6099e0b
113 fixed model naming errors
adp-atea Nov 24, 2023
6ad0d47
113 fixed test_api tests
adp-atea Nov 24, 2023
a45000f
113 fixed test_auth tests
adp-atea Nov 24, 2023
0686077
113 fixed test_exceptions tests
adp-atea Nov 24, 2023
7b8729c
113 fixed test_freeze tests
adp-atea Nov 24, 2023
d3ae479
113 fixed wrong manifest in inspect
adp-atea Nov 24, 2023
2bb1166
113 fixed inspect
adp-atea Nov 24, 2023
afc6108
113 fixed wrong check
adp-atea Nov 24, 2023
ee70cde
113 fixed test_manifests tests
adp-atea Nov 24, 2023
057c236
113 fixed test_all for linux
adp-atea Nov 24, 2023
3799f4c
113 fixed more tests
adp-atea Nov 27, 2023
d758058
Merge branch 'master' into 113-read-manifest-from-database
adp-atea Nov 27, 2023
6d6bc9f
113 skipped one test temp
adp-atea Nov 27, 2023
ed91c47
113 fixed bug
adp-atea Nov 27, 2023
724768b
113 added url none check
adp-atea Nov 27, 2023
7b3e765
113 added context to new commands
adp-atea Nov 27, 2023
cc7ce01
113 added more context
adp-atea Nov 27, 2023
5cc5670
113 fixed context scope
adp-atea Nov 27, 2023
c4c5d27
113 fixed test_inspect tests
adp-atea Nov 27, 2023
c13e282
113 fixed test_inspect tests
adp-atea Nov 27, 2023
b36e288
113 added missing field
adp-atea Nov 28, 2023
27e470c
113 refactored namespace requests
adp-atea Dec 1, 2023
697629a
113 added on demand model load
adp-atea Dec 5, 2023
b07c6bb
113 added index, refactored more
adp-atea Dec 6, 2023
d3df1f5
113 fixed command
adp-atea Dec 6, 2023
25e91f2
113 added missing variables to load
adp-atea Dec 6, 2023
1b387e0
113 changed compare_manifest
adp-atea Dec 6, 2023
0efda62
113 added nested prop support
adp-atea Dec 7, 2023
9eaf990
113 fixed bug
adp-atea Dec 7, 2023
31bbb4a
113 added level to sql base
adp-atea Dec 8, 2023
0389247
113 temp disabled one test
adp-atea Dec 8, 2023
9ffb474
113 fixed old tests
adp-atea Dec 8, 2023
e3d5288
113 refactored traverse_ns_models
adp-atea Dec 8, 2023
773d3a1
113 fixed incorrect calls
adp-atea Dec 8, 2023
1e1e298
113 fixed missing call
adp-atea Dec 8, 2023
cce7304
113 added missing changes
adp-atea Dec 8, 2023
2f27588
113 fixed tests
adp-atea Dec 8, 2023
016193a
113 missing test changes
adp-atea Dec 8, 2023
5498acf
113 refactored manifest testing
adp-atea Dec 11, 2023
231c3ac
113 refactored manifest_load for tests
adp-atea Dec 11, 2023
d2bbf52
113 fixed refactor
adp-atea Dec 11, 2023
dafd6ec
113 refactoring and test changes
adp-atea Dec 12, 2023
5a93540
113 bug fix, updated sqlalchemy-utils version
adp-atea Dec 13, 2023
0f0a3ce
113 refactor, poetry.lock file
adp-atea Dec 13, 2023
8e3f7de
113 fixed check tests
adp-atea Dec 13, 2023
c036aa0
113 fixed more tests
adp-atea Dec 13, 2023
0b08da8
113 optimizations and bug fixes
adp-atea Dec 13, 2023
9708184
113 added missing id
adp-atea Dec 13, 2023
089be2c
113 fixed test_postgresql.py tests
adp-atea Dec 13, 2023
f7cd225
113 clean up
adp-atea Dec 14, 2023
4b98dbb
Merge branch 'master' into 113-read-manifest-from-database
adp-atea Jan 23, 2024
37d74dc
113 merge changes
adp-atea Jan 23, 2024
4b4b0fe
113 missing indent
adp-atea Jan 23, 2024
2e6d4dd
113 missing context from renderer
adp-atea Jan 23, 2024
0a492f4
113 added missing context to xlsx render
adp-atea Jan 23, 2024
7857b8a
113 fixed test_migrations.py tests
adp-atea Jan 24, 2024
1a95846
113 fixed config checks
adp-atea Jan 24, 2024
7d99d9a
113 test fixes
adp-atea Jan 24, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 86 additions & 0 deletions notes/manifests/internal.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# notes/docker.sh Start docker compose
# notes/postgres.sh Reset database

INSTANCE=manifests/internal
DATASET=$INSTANCE
BASEDIR=var/instances/$INSTANCE

mkdir -p $BASEDIR

export SPINTA_CONFIG=$BASEDIR/config.yml

cat $BASEDIR/config.yml
cat > $BASEDIR/config.yml <<EOF
env: production
data_path: $PWD/$BASEDIR
default_auth_client: default

keymaps:
default:
type: sqlalchemy
dsn: sqlite:///$PWD/$BASEDIR/keymap.db

backends:
default:
type: postgresql
dsn: postgresql://admin:admin123@localhost:54321/spinta

manifest: default
manifests:
default:
type: internal
path: postgresql://admin:admin123@localhost:54321/spinta
backend: default
keymap: default
mode: internal

accesslog:
type: file
file: $PWD/$BASEDIR/accesslog.json
EOF

poetry run spinta --tb=native bootstrap
#| Traceback (most recent call last):
#| File "spinta/cli/migrate.py", line 27, in bootstrap
#| store = prepare_manifest(context, ensure_config_dir=True)
#| File "spinta/cli/helpers/store.py", line 132, in prepare_manifest
#| store = load_manifest(
#| File "spinta/cli/helpers/store.py", line 116, in load_manifest
#| commands.load(
#| File "spinta/manifests/internal_sql/commands/load.py", line 49, in load
#| load_manifest_nodes(context, manifest, schemas)
#| File "spinta/manifests/helpers.py", line 134, in load_manifest_nodes
#| for eid, schema in schemas:
#| File "spinta/manifests/internal_sql/helpers.py", line 36, in read_schema
#| yield from _read_all_sql_manifest_rows(path, conn)
#| File "spinta/manifests/internal_sql/helpers.py", line 75, in _read_all_sql_manifest_rows
#| rows = conn.execute(stmt)
#| ProgrammingError: (psycopg2.errors.UndefinedTable) relation "_manifest" does not exist
# FIXME: `spinta bootstrap` should create _manifest table.
adp-atea marked this conversation as resolved.
Show resolved Hide resolved

psql -h localhost -p 54321 -U admin spinta -c '\dt public.*'

cat > $BASEDIR/manifest.txt <<EOF
d | r | b | m | property | type | ref | access
$DATASET | | |
| | | Country | | id |
| | | | id | integer | | open
| | | | name | string | | open
| | | | | | |
| | | City | | id |
| | | | id | integer | | open
| | | | name | string | | open
| | | | country | ref | Country | open
EOF
poetry run spinta copy $BASEDIR/manifest.txt -o postgresql://admin:admin123@localhost:54321/spinta
poetry run spinta show

psql -h localhost -p 54321 -U admin spinta -c 'select * from _manifest;'

poetry run spinta bootstrap

psql -h localhost -p 54321 -U admin spinta -c '\dt public.*'

# notes/spinta/server.sh Run migrations
# notes/spinta/server.sh Run server
# notes/spinta/client.sh Configure client
10 changes: 7 additions & 3 deletions spinta/cli/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@
from spinta.manifests.components import Manifest
from spinta.manifests.components import ManifestPath
from spinta.manifests.helpers import get_manifest_from_type, init_manifest
from spinta.manifests.tabular.helpers import render_tabular_manifest
from spinta.manifests.tabular.helpers import write_tabular_manifest
from spinta.manifests.internal_sql.components import InternalSQLManifest
from spinta.manifests.internal_sql.helpers import write_internal_sql_manifest
from spinta.manifests.tabular.helpers import write_tabular_manifest, render_tabular_manifest
from spinta.types.datatype import Ref, DataType, Array, Object, Denorm
from spinta.utils.naming import Deduplicator
from spinta.utils.schema import NA
Expand Down Expand Up @@ -107,7 +108,10 @@ def inspect(
manifest.objects['model'] = sorted_models

if output:
write_tabular_manifest(output, manifest)
if InternalSQLManifest.detect_from_path(output):
write_internal_sql_manifest(output, manifest)
else:
write_tabular_manifest(output, manifest)
else:
echo(render_tabular_manifest(manifest))

Expand Down
64 changes: 53 additions & 11 deletions spinta/cli/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
from spinta.components import Context
from spinta.core.context import configure_context
from spinta.core.enums import Access
from spinta.manifests.internal_sql.components import InternalSQLManifest
from spinta.manifests.internal_sql.helpers import write_internal_sql_manifest
from spinta.manifests.tabular.components import ManifestColumn
from spinta.manifests.tabular.components import ManifestRow
from spinta.manifests.tabular.helpers import datasets_to_tabular
Expand Down Expand Up @@ -62,25 +64,65 @@ def copy(
verbose = True
if not output:
verbose = False
internal = InternalSQLManifest.detect_from_path(output)
if output and internal:
rows = _read_and_return_manifest(
context,
manifests,
external=source,
access=access,
format_names=format_names,
order_by=order_by,
rename_duplicates=rename_duplicates,
verbose=verbose,
)
else:
rows = _read_and_return_rows(
context,
manifests,
external=source,
access=access,
format_names=format_names,
order_by=order_by,
rename_duplicates=rename_duplicates,
verbose=verbose,
)

rows = _read_csv_files(
if output:
if internal:
write_internal_sql_manifest(output, rows)
else:
write_tabular_manifest(output, rows)
else:
echo(render_tabular_manifest_rows(rows, cols))


def _read_and_return_manifest(
context: Context,
manifests: List[str],
*,
external: bool = True,
access: Access = Access.private,
format_names: bool = False,
order_by: ManifestColumn = None,
rename_duplicates: bool = False,
verbose: bool = True,
) -> Iterator[ManifestRow]:
context = configure_context(context, manifests)
store = load_manifest(
context,
manifests,
external=source,
access=access,
format_names=format_names,
order_by=order_by,
rename_duplicates=rename_duplicates,
load_internal=False,
verbose=verbose,
)

if output:
write_tabular_manifest(output, rows)
else:
echo(render_tabular_manifest_rows(rows, cols))
if format_names:
reformat_names(context, store.manifest)

return store.manifest


def _read_csv_files(
def _read_and_return_rows(
context: Context,
manifests: List[str],
*,
Expand Down
11 changes: 9 additions & 2 deletions spinta/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,13 @@ def get_eid_for_error_context(self):
return str(self.eid)


class ExtraMetaData(Node):
id: str = None
schema = {
'id': {'type': 'string'}
}


class NamespaceGiven:
access: str = None

Expand Down Expand Up @@ -452,7 +459,7 @@ def is_root(self) -> bool:
return isinstance(self.parent, Manifest)


class Base(Node):
class Base(ExtraMetaData):
model: Model # a.base.b - here `model` is `b`
parent: Model # a.base.b - here `parent` is `a`
pk: List[Property] # a.base.b - list of properties of `a` model
Expand Down Expand Up @@ -549,7 +556,7 @@ class PropertyGiven:
unit: str = None


class Property(Node):
class Property(ExtraMetaData):
place: str = None # Dotted property path
title: str = None
description: str = None
Expand Down
3 changes: 2 additions & 1 deletion spinta/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@
'sql': 'spinta.manifests.sql.components:SqlManifest',
'memory': 'spinta.manifests.memory.components:MemoryManifest',
'json': 'spinta.manifests.dict.components:JsonManifest',
'xml': 'spinta.manifests.dict.components:XmlManifest'
'xml': 'spinta.manifests.dict.components:XmlManifest',
'internal': 'spinta.manifests.internal_sql.components:InternalSQLManifest',
},
'backends': {
# In memory backends mostly usable in tests
Expand Down
17 changes: 9 additions & 8 deletions spinta/datasets/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
from sqlalchemy.engine.base import Engine

from spinta.backends.components import Backend
from spinta.components import EntryId
from spinta.components import EntryId, ExtraMetaData
from spinta.components import Namespace
from spinta.dimensions.lang.components import LangData
from spinta.components import MetaData
from spinta.components import Model
from spinta.components import Node
from spinta.components import Property
from spinta.core.enums import Access
from spinta.core.ufuncs import Expr
Expand All @@ -22,6 +21,7 @@
from spinta.manifests.components import Manifest
from spinta.types.owner import Owner
from spinta.types.project import Project
from spinta.utils.schema import NA


class DatasetGiven:
Expand Down Expand Up @@ -96,7 +96,7 @@ class ExternalBackend(Backend):
schema: sa.MetaData = None


class External(Node):
class External(ExtraMetaData):
pass


Expand All @@ -113,7 +113,7 @@ class Resource(External):
level: Level
access: Access
external: str
prepare: str
prepare: Expr
models: Dict[str, Model]
given: ResourceGiven
lang: LangData = None
Expand All @@ -122,7 +122,7 @@ class Resource(External):
schema = {
'type': {'type': 'string'},
'dataset': {'parent': True},
'prepare': {'type': 'spyna'},
'prepare': {'type': 'spyna', 'default': NA},

# Backend name specified in `ref` column, points to previously defined
# backend or to a configured stored backend.
Expand Down Expand Up @@ -151,6 +151,7 @@ class Resource(External):
'title': {'type': 'string'},
'description': {'type': 'string'},
'comments': {},
'lang': {'type': 'object'},
}

def __init__(self):
Expand Down Expand Up @@ -186,7 +187,7 @@ class Entity(External):
'dataset': {'type': 'ref', 'ref': 'context.nodes.dataset'},
'resource': {'type': 'ref', 'ref': 'dataset.resources'},
'name': {'type': 'string', 'default': None},
'prepare': {'type': 'spyna', 'default': None},
'prepare': {'type': 'spyna', 'default': NA},
'params': {
'type': 'array',
'items': {'type': 'object'},
Expand All @@ -206,10 +207,10 @@ class Entity(External):
class Attribute(External):
prop: Property # property
name: str # property.source
prepare: Expr = None # property.prepare
prepare: Expr = NA # property.prepare

schema = {
'prop': {'parent': True},
'name': {'default': None},
'prepare': {'type': 'spyna', 'default': None},
'prepare': {'type': 'spyna', 'default': NA},
}
5 changes: 2 additions & 3 deletions spinta/dimensions/enum/components.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

from typing import Dict

from spinta.components import Model
from spinta.components import Node
from spinta.components import Model, ExtraMetaData
from spinta.core.enums import Access
from spinta.core.ufuncs import Env
from spinta.core.ufuncs import Expr
Expand All @@ -19,7 +18,7 @@ class EnumValueGiven:
access: str = None


class EnumItem(Node):
class EnumItem(ExtraMetaData):
source: str
prepare: Expr
access: Access = None
Expand Down
Empty file.
Empty file.
10 changes: 10 additions & 0 deletions spinta/manifests/internal_sql/commands/bootstrap.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from spinta import commands
from spinta.components import Context
from spinta.manifests.internal_sql.components import InternalSQLManifest


@commands.bootstrap.register(Context, InternalSQLManifest)
def bootstrap(context: Context, manifest: InternalSQLManifest):
store = context.get('store')
for backend in store.backends.values():
commands.bootstrap(context, backend)
13 changes: 13 additions & 0 deletions spinta/manifests/internal_sql/commands/configure.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from typing import Optional

from spinta import commands
from spinta.components import Context
from spinta.core.config import RawConfig
from spinta.manifests.internal_sql.components import InternalSQLManifest


@commands.configure.register(Context, InternalSQLManifest)
def configure(context: Context, manifest: InternalSQLManifest):
rc: RawConfig = context.get('rc')
path: Optional[str] = rc.get('manifests', manifest.name, 'path')
manifest.path = path
Loading