Skip to content

Commit

Permalink
feat(model): remove the data_content field from the view user_data_list
Browse files Browse the repository at this point in the history
  • Loading branch information
a-lubert committed Feb 28, 2025
1 parent 765b473 commit 09408d9
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 5 deletions.
2 changes: 1 addition & 1 deletion model/userData.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class UserDataModel {
this._checkIdentifier(identifier);

const connection = getKnexConnection(this._mainConfig.database);
const results = await connection.select("*").from("user_data_list").where("id", identifier).first();
const results = await connection.select("*").from("user_data").where("id", identifier).first();
if (results === undefined) {
cleanupConnection(connection);
throw Error("The specified identifier do not exists", { cause: 404 });
Expand Down
50 changes: 50 additions & 0 deletions scripts/migrations/migration_NEXT_userData.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
const fs = require("fs");
const knex = require("knex");
const path = require("path");
const yargs = require("yargs");

const migrateUserDataView = async (configDirectory, writeMode) => {
const configPath = path.resolve(configDirectory, "mainConfig.json");
const configJSON = JSON.parse(fs.readFileSync(configPath, { encoding: "utf-8" }));

const userDataSchema = path.resolve("scripts", "sql", "003-user-data.sql");
if (fs.existsSync(userDataSchema)) {
const connection = await knex({ client: "pg", connection: configJSON.database });
if (await connection.schema.hasColumn("user_data_list", "data_content")) {
if (writeMode) {
/* Remove the view since the library cannot do it on his own */
await connection.schema.dropViewIfExists("user_data_list");
/* Read the schema again to add the view again with the latest
* modification */
await connection.raw(fs.readFileSync(userDataSchema, "utf-8"));
connection.destroy();
console.info(`The script ${userDataSchema} have beed executed`);
} else {
console.info(`Will run the script ${userDataSchema}`);
}
} else {
console.info("The view is already up to date");
}
}
};

const main = async () => {
const argv = yargs
.alias("c", "config")
.describe("c", "Path to the config directory")
.alias("w", "write")
.describe("w", "Write the migration in the file")
.boolean("w")
.demandOption(["config"])
.help().argv;

console.info(argv.write ? "🚧 Prepare the migration…" : "🔧 Dry run mode…");
await migrateUserDataView(argv.config, argv.write);
};

main()
.then(() => process.exit(0))
.catch((err) => {
console.error(err);
process.exit(1);
});
7 changes: 4 additions & 3 deletions scripts/sql/003-user-data.sql
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ create table if not exists user_data(
owned_by integer references users (id)
);

create view user_data_list as
-- Add a view to retrieve the list of data without their content
create or replace view user_data_list as
select user_data.id, data_path, data_type, data_label, data_comment,
data_group, data_content, is_shared, shared_profiles,
shared_users, created_at, login as owned_by
data_group, is_shared, shared_profiles, shared_users,
created_at, login as owned_by
from user_data, users
where user_data.owned_by = users.id;
2 changes: 1 addition & 1 deletion tests/model.userData.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ describe("UserDataModel", () => {
});

test("find userData", async () => {
tracker.on.select("user_data_list").response(dbUserDataList);
tracker.on.select("user_data").response(dbUserDataList);
const userData = await userDataModel.find(1);
expect(userData).toBeTruthy();
});
Expand Down

0 comments on commit 09408d9

Please sign in to comment.