Skip to content

Commit

Permalink
Merge pull request #32 from folio-org/current
Browse files Browse the repository at this point in the history
Rename system schema to ldp_catalog
  • Loading branch information
nassibnassar authored Dec 12, 2019
2 parents 5c8c28a + 72f921f commit fade9a5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 15 deletions.
6 changes: 4 additions & 2 deletions USER_GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,10 @@ ignored.

The data in these tables are extracted from Okapi-based APIs and loaded
into the database by the LDP data loader. The data loader typically
should be run once per day, and so the LDP database reflects the state
of the source data as of sometime within the past 24 hours.
runs once per day, and so the LDP database reflects the state of the
source data as of sometime within the past 24 hours or so. A table
called `ldp_catalog.table_updates` records when each data table was last
updated.


2\. JSON queries
Expand Down
15 changes: 9 additions & 6 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,14 +58,16 @@ static void initDB(const Options& opt, etymon::Postgres* db)
{
string sql;

sql = "CREATE SCHEMA IF NOT EXISTS ldp;";
sql = "CREATE SCHEMA IF NOT EXISTS ldp_catalog;";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }

sql =
"CREATE TABLE IF NOT EXISTS ldp.table_updates (\n"
" table_name VARCHAR(65535) NOT NULL PRIMARY KEY,\n"
" updated TIMESTAMPTZ NOT NULL\n"
"CREATE TABLE IF NOT EXISTS ldp_catalog.table_updates (\n"
" table_name VARCHAR(65535) NOT NULL,\n"
" updated TIMESTAMPTZ NOT NULL,\n"
" tenant_id SMALLINT NOT NULL,\n"
" PRIMARY KEY (table_name, tenant_id)\n"
");";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }
Expand All @@ -83,11 +85,12 @@ static void updateDBPermissions(const Options& opt, etymon::Postgres* db)
{
string sql;

sql = "GRANT USAGE ON SCHEMA ldp TO " + opt.ldpUser + ";";
sql = "GRANT USAGE ON SCHEMA ldp_catalog TO " + opt.ldpUser + ";";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }

sql = "GRANT SELECT ON ALL TABLES IN SCHEMA ldp TO " + opt.ldpUser + ";";
sql = "GRANT SELECT ON ALL TABLES IN SCHEMA ldp_catalog TO " +
opt.ldpUser + ";";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }

Expand Down
24 changes: 19 additions & 5 deletions src/merge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,24 @@ static void mergeTable(const Options& opt, const TableSchema& table,
" updated TIMESTAMPTZ NOT NULL,\n"
" tenant_id SMALLINT NOT NULL,\n"
" CONSTRAINT ldp_history_" + table.tableName + "_pkey\n"
" PRIMARY KEY (tenant_id, id, updated)\n"
" PRIMARY KEY (id, updated, tenant_id)\n"
");";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }

// Temporary: reorder primary key.
sql =
"ALTER TABLE " + historyTable + "\n"
" DROP CONSTRAINT ldp_history_" + table.tableName + "_pkey;\n";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }
sql =
"ALTER TABLE " + historyTable + "\n"
" ADD CONSTRAINT ldp_history_" + table.tableName + "_pkey\n"
" PRIMARY KEY (id, updated, tenant_id);";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }

string latestHistoryTable;
latestHistoryTableName(table.tableName, &latestHistoryTable);

Expand Down Expand Up @@ -79,14 +92,15 @@ static void updateStatus(const Options& opt, const TableSchema& table,
etymon::Postgres* db)
{
string sql =
"DELETE FROM ldp.table_updates WHERE table_name = '" +
table.tableName + "';";
"DELETE FROM ldp_catalog.table_updates WHERE table_name = '" +
table.tableName + "' AND tenant_id = 1;";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }

sql =
"INSERT INTO ldp.table_updates (table_name, updated)\n"
" VALUES ('" + table.tableName + "', 'now');";
"INSERT INTO ldp_catalog.table_updates\n"
" (table_name, updated, tenant_id)\n"
" VALUES ('" + table.tableName + "', 'now', 1);";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }
}
Expand Down
2 changes: 1 addition & 1 deletion src/names.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

void loadingTableName(const string& table, string* newtable)
{
*newtable = "stage_" + table;
*newtable = "ldp_" + table;
}

void latestHistoryTableName(const string& table, string* newtable)
Expand Down
2 changes: 1 addition & 1 deletion src/stage_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,7 @@ static void createLoadingTable(const Options& opt, const TableSchema& table,
sql += opt.dbtype.jsonType();
sql += ",\n"
" tenant_id SMALLINT NOT NULL,\n"
" PRIMARY KEY (tenant_id, id)\n"
" PRIMARY KEY (id, tenant_id)\n"
");";
printSQL(Print::debug, opt, sql);
{ etymon::PostgresResult result(db, sql); }
Expand Down

0 comments on commit fade9a5

Please sign in to comment.