You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by dp...@apache.org on 2023/08/17 10:50:32 UTC

[superset] branch master updated: chore: isolate examples database by default (#25003)

This is an automated email from the ASF dual-hosted git repository.

dpgaspar pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new 269c99293f chore: isolate examples database by default (#25003)
269c99293f is described below

commit 269c99293f42089958dc98b5d6e5899509fc3111
Author: Daniel Vaz Gaspar <da...@gmail.com>
AuthorDate: Thu Aug 17 11:50:24 2023 +0100

    chore: isolate examples database by default (#25003)
---
 docker-compose.yml                                 |  1 +
 docker/.env                                        |  6 ++++++
 docker/docker-entrypoint-initdb.d/examples-init.sh | 15 ++++++++++++++
 docker/pythonpath_dev/superset_config.py           | 24 +++++++++++++++-------
 superset/config.py                                 |  2 +-
 superset/utils/database.py                         |  6 +-----
 6 files changed, 41 insertions(+), 13 deletions(-)

diff --git a/docker-compose.yml b/docker-compose.yml
index 2b8ad0cc47..dc9f9d5589 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -47,6 +47,7 @@ services:
       - "127.0.0.1:5432:5432"
     volumes:
       - db_home:/var/lib/postgresql/data
+      - ./docker/docker-entrypoint-initdb.d:/docker-entrypoint-initdb.d
 
   superset:
     env_file: docker/.env
diff --git a/docker/.env b/docker/.env
index a2e9faeacd..25bdac0ab7 100644
--- a/docker/.env
+++ b/docker/.env
@@ -22,6 +22,12 @@ DATABASE_HOST=db
 DATABASE_PASSWORD=superset
 DATABASE_USER=superset
 
+EXAMPLES_DB=examples
+EXAMPLES_HOST=db
+EXAMPLES_USER=examples
+EXAMPLES_PASSWORD=examples
+EXAMPLES_PORT=5432
+
 # database engine specific environment variables
 # change the below if you prefer another database engine
 DATABASE_PORT=5432
diff --git a/docker/docker-entrypoint-initdb.d/examples-init.sh b/docker/docker-entrypoint-initdb.d/examples-init.sh
new file mode 100755
index 0000000000..9a72907823
--- /dev/null
+++ b/docker/docker-entrypoint-initdb.d/examples-init.sh
@@ -0,0 +1,15 @@
+# ------------------------------------------------------------------------
+# Creates the examples database and repective user. This database location
+# and access credentials are defined on the environment variables
+# ------------------------------------------------------------------------
+set -e
+
+psql -v ON_ERROR_STOP=1 --username "${POSTGRES_USER}" <<-EOSQL
+  CREATE USER ${EXAMPLES_USER} WITH PASSWORD '${EXAMPLES_PASSWORD}';
+  CREATE DATABASE ${EXAMPLES_DB};
+  GRANT ALL PRIVILEGES ON DATABASE ${EXAMPLES_DB} TO ${EXAMPLES_USER};
+EOSQL
+
+psql -v ON_ERROR_STOP=1 --username "${POSTGRES_USER}" -d "${EXAMPLES_DB}" <<-EOSQL
+   GRANT ALL ON SCHEMA public TO ${EXAMPLES_USER};
+EOSQL
diff --git a/docker/pythonpath_dev/superset_config.py b/docker/pythonpath_dev/superset_config.py
index 199e79f66e..2e3c1fdda4 100644
--- a/docker/pythonpath_dev/superset_config.py
+++ b/docker/pythonpath_dev/superset_config.py
@@ -51,14 +51,24 @@ DATABASE_HOST = get_env_variable("DATABASE_HOST")
 DATABASE_PORT = get_env_variable("DATABASE_PORT")
 DATABASE_DB = get_env_variable("DATABASE_DB")
 
+EXAMPLES_USER = get_env_variable("EXAMPLES_USER")
+EXAMPLES_PASSWORD = get_env_variable("EXAMPLES_PASSWORD")
+EXAMPLES_HOST = get_env_variable("EXAMPLES_HOST")
+EXAMPLES_PORT = get_env_variable("EXAMPLES_PORT")
+EXAMPLES_DB = get_env_variable("EXAMPLES_DB")
+
+
 # The SQLAlchemy connection string.
-SQLALCHEMY_DATABASE_URI = "{}://{}:{}@{}:{}/{}".format(
-    DATABASE_DIALECT,
-    DATABASE_USER,
-    DATABASE_PASSWORD,
-    DATABASE_HOST,
-    DATABASE_PORT,
-    DATABASE_DB,
+SQLALCHEMY_DATABASE_URI = (
+    f"{DATABASE_DIALECT}://"
+    f"{DATABASE_USER}:{DATABASE_PASSWORD}@"
+    f"{DATABASE_HOST}:{DATABASE_PORT}/{DATABASE_DB}"
+)
+
+SQLALCHEMY_EXAMPLES_URI = (
+    f"{DATABASE_DIALECT}://"
+    f"{EXAMPLES_USER}:{EXAMPLES_PASSWORD}@"
+    f"{EXAMPLES_HOST}:{EXAMPLES_PORT}/{EXAMPLES_DB}"
 )
 
 REDIS_HOST = get_env_variable("REDIS_HOST")
diff --git a/superset/config.py b/superset/config.py
index 0b70328e0b..21f275e42d 100644
--- a/superset/config.py
+++ b/superset/config.py
@@ -1452,7 +1452,7 @@ SEND_FILE_MAX_AGE_DEFAULT = int(timedelta(days=365).total_seconds())
 
 # URI to database storing the example data, points to
 # SQLALCHEMY_DATABASE_URI by default if set to `None`
-SQLALCHEMY_EXAMPLES_URI = None
+SQLALCHEMY_EXAMPLES_URI = "sqlite:///" + os.path.join(DATA_DIR, "examples.db")
 
 # Optional prefix to be added to all static asset paths when rendering the UI.
 # This is useful for hosting assets in an external CDN, for example
diff --git a/superset/utils/database.py b/superset/utils/database.py
index 70730554f3..b34dda1164 100644
--- a/superset/utils/database.py
+++ b/superset/utils/database.py
@@ -65,11 +65,7 @@ def get_or_create_db(
 
 
 def get_example_database() -> Database:
-    db_uri = (
-        current_app.config.get("SQLALCHEMY_EXAMPLES_URI")
-        or current_app.config["SQLALCHEMY_DATABASE_URI"]
-    )
-    return get_or_create_db("examples", db_uri)
+    return get_or_create_db("examples", current_app.config["SQLALCHEMY_EXAMPLES_URI"])
 
 
 def get_main_database() -> Database: