Skip to content

Commit

Permalink
chore: use _supabase database for internals
Browse files Browse the repository at this point in the history
This is to avoid overloading our user postgres database
with every new addition to _analytics or _realtime
  • Loading branch information
avallete committed Sep 24, 2024
1 parent 5701583 commit bb28463
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 16 deletions.
3 changes: 0 additions & 3 deletions internal/db/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ func dumpData(ctx context.Context, config pgconn.Config, schema, excludeTable []
// "storage",
// "supabase_functions",
"supabase_migrations",
"_analytics",
"_realtime",
"_supavisor",
}
var env []string
if len(schema) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions internal/db/reset/reset.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,9 @@ func recreateDatabase(ctx context.Context, options ...func(*pgx.ConnConfig)) err
sql := migration.MigrationFile{
Statements: []string{
"DROP DATABASE IF EXISTS postgres WITH (FORCE)",
"DROP DATABASE IF EXISTS _supabase WITH (FORCE)",
"CREATE DATABASE postgres WITH OWNER postgres",
"CREATE DATABASE _supabase WITH OWNER postgres",
},
}
return sql.ExecBatch(ctx, conn)
Expand Down
2 changes: 1 addition & 1 deletion internal/db/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func initRealtimeJob(host string) utils.DockerJob {
"DB_PORT=5432",
"DB_USER=supabase_admin",
"DB_PASSWORD=" + utils.Config.Db.Password,
"DB_NAME=postgres",
"DB_NAME=_supabase",
"DB_AFTER_CONNECT_QUERY=SET search_path TO _realtime",
"DB_ENC_KEY=" + utils.Config.Realtime.EncryptionKey,
"API_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
Expand Down
24 changes: 16 additions & 8 deletions internal/db/start/templates/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,22 @@ ALTER USER supabase_storage_admin WITH PASSWORD :'pgpass';
ALTER USER supabase_replication_admin WITH PASSWORD :'pgpass';
ALTER USER supabase_read_only_user WITH PASSWORD :'pgpass';

create schema if not exists _realtime;
alter schema _realtime owner to postgres;

create schema if not exists _analytics;
alter schema _analytics owner to postgres;

create schema if not exists _supavisor;
alter schema _supavisor owner to postgres;
CREATE DATABASE _supabase WITH OWNER postgres;
-- Connect to the _supabase database
\c _supabase
-- Create schemas in _supabase database for
-- internals tools and reports to not overload user database
-- with non-user activity
CREATE SCHEMA IF NOT EXISTS _realtime;
ALTER SCHEMA _realtime OWNER TO postgres;

CREATE SCHEMA IF NOT EXISTS _analytics;
ALTER SCHEMA _analytics OWNER TO postgres;

CREATE SCHEMA IF NOT EXISTS _supavisor;
ALTER SCHEMA _supavisor OWNER TO postgres;
-- Switch back to the main database
\c postgres

BEGIN;

Expand Down
8 changes: 4 additions & 4 deletions internal/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ func run(p utils.Program, ctx context.Context, fsys afero.Fs, excludedContainers
// Start Logflare
if utils.Config.Analytics.Enabled && !isContainerExcluded(utils.Config.Analytics.Image, excluded) {
env := []string{
"DB_DATABASE=" + dbConfig.Database,
"DB_DATABASE=_supabase",
"DB_HOSTNAME=" + dbConfig.Host,
fmt.Sprintf("DB_PORT=%d", dbConfig.Port),
"DB_SCHEMA=_analytics",
Expand Down Expand Up @@ -228,7 +228,7 @@ func run(p utils.Program, ctx context.Context, fsys afero.Fs, excludedContainers
)
case config.LogflarePostgres:
env = append(env,
fmt.Sprintf("POSTGRES_BACKEND_URL=postgresql://%s:%s@%s:%d/%s", dbConfig.User, dbConfig.Password, dbConfig.Host, dbConfig.Port, dbConfig.Database),
fmt.Sprintf("POSTGRES_BACKEND_URL=postgresql://%s:%s@%s:%d/%s", dbConfig.User, dbConfig.Password, dbConfig.Host, dbConfig.Port, "_supabase"),
"POSTGRES_BACKEND_SCHEMA=_analytics",
)
}
Expand Down Expand Up @@ -750,7 +750,7 @@ EOF
fmt.Sprintf("DB_PORT=%d", dbConfig.Port),
"DB_USER=supabase_admin",
"DB_PASSWORD=" + dbConfig.Password,
"DB_NAME=" + dbConfig.Database,
"DB_NAME=_supabase",
"DB_AFTER_CONNECT_QUERY=SET search_path TO _realtime",
"DB_ENC_KEY=" + utils.Config.Realtime.EncryptionKey,
"API_JWT_SECRET=" + utils.Config.Auth.JwtSecret,
Expand Down Expand Up @@ -1045,7 +1045,7 @@ EOF
"PORT=4000",
fmt.Sprintf("PROXY_PORT_SESSION=%d", portSession),
fmt.Sprintf("PROXY_PORT_TRANSACTION=%d", portTransaction),
fmt.Sprintf("DATABASE_URL=ecto://%s:%s@%s:%d/%s", dbConfig.User, dbConfig.Password, dbConfig.Host, dbConfig.Port, dbConfig.Database),
fmt.Sprintf("DATABASE_URL=ecto://%s:%s@%s:%d/%s", dbConfig.User, dbConfig.Password, dbConfig.Host, dbConfig.Port, "_supabase"),
"CLUSTER_POSTGRES=true",
"SECRET_KEY_BASE=" + utils.Config.Db.Pooler.SecretKeyBase,
"VAULT_ENC_KEY=" + utils.Config.Db.Pooler.EncryptionKey,
Expand Down

0 comments on commit bb28463

Please sign in to comment.