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

fix: migrations #814

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions .cruft.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"template": "https://github.com/dyne/starters",
"checkout": "main",
"commit": "498509f2d6575f10d08d2767b1da36b05355fdf9",
"checkout": null,
"commit": "0b112ec719d9350f52092573bdc615601223d03d",
"context": {
"cookiecutter": {
"project_name": "signroom",
Expand Down
20 changes: 15 additions & 5 deletions admin/pb_migrations/1685000000_create_admin.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
/// <reference path="../pb_data/types.d.ts" />

const ADMIN_EMAIL = "[email protected]";

migrate(
(db) => {
const admin = new Admin();
admin.email = "[email protected]";
admin.setPassword("adminadmin");
return Dao(db).saveAdmin(admin);
const dao = new Dao(db);

try {
dao.findAdminByEmail(ADMIN_EMAIL);
} catch {
const admin = new Admin();
admin.email = ADMIN_EMAIL;
admin.setPassword("adminadmin");
return dao.saveAdmin(admin);
}
},
(db) => {
const dao = new Dao(db);
const admin = dao.findAdminByEmail("[email protected]");
const admin = dao.findAdminByEmail(ADMIN_EMAIL);
return dao.deleteAdmin(admin);
}
);
37 changes: 30 additions & 7 deletions admin/pb_migrations/1685000000_seed_features.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// @ts-check

/// <reference path="../pb_data/types.d.ts" />
/**
* @typedef {import('../../webapp/src/lib/pocketbase/types').FeaturesRecord} Feature
Expand Down Expand Up @@ -33,7 +35,19 @@ const features = [
name: "maintenance",
envVariables: {},
active: false,
}
},
{
name: "organizations",
envVariables: {},
},
{
name: "webauthn",
envVariables: {
DISPLAY_NAME: "signroom",
RPID: "localhost",
RPORIGINS: "http://localhost:5173",
},
},
];

migrate(
Expand All @@ -44,12 +58,21 @@ migrate(
);

for (const feature of features) {
const record = new Record(collection, {
name: feature.name,
envVariables: feature.envVariables,
active: feature.active ?? true,
});

/** @type {models.Record} */
let record;
try {
record = dao.findFirstRecordByData(
FEATURES_COLLECTION_NAME,
"name",
feature.name
);
} catch (e) {
record = new Record(collection, {
name: feature.name,
});
}
record.set("envVariables", feature.envVariables);
record.set("active", feature.active ?? true);
dao.saveRecord(record);
}
},
Expand Down
64 changes: 0 additions & 64 deletions admin/pb_migrations/1685000001_seed_test_data.js

This file was deleted.

81 changes: 81 additions & 0 deletions admin/pb_migrations/1685000001_seed_users.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/// <reference path="../pb_data/types.d.ts" />

const USERS_COLLECTION_NAME = "users";

/**
* @param {string} letter
*/
function createSampleUserData(letter) {
const name = `user${letter}`;
return {
email: `user${letter}@example.org`,
username: name,
password: `user${letter}user${letter}`,
name: name,
};
}

/**
*
* @param {Dao} dao
* @param {string} letter
* @returns
*/
function addSampleUser(dao, letter) {
const { email, username, password, name } = createSampleUserData(letter);
const collection = dao.findCollectionByNameOrId(USERS_COLLECTION_NAME);

/** @type {models.Record} */
let record;

try {
record = dao.findAuthRecordByEmail(collection.name, email);
} catch (e) {
record = new Record(collection);
}

record.setUsername(username);
record.setEmail(email);
record.setPassword(password);
record.setVerified(true);
record.set("name", name);
record.set("emailVisibility", true);

dao.saveRecord(record);

return record;
}

migrate(
(db) => {
const dao = new Dao(db);

/* Users */
const userA = addSampleUser(dao, "A");
const userB = addSampleUser(dao, "B");
const userC = addSampleUser(dao, "C");

// /* AuthorizationExample */
// const authorizationsExamples = dao.findCollectionByNameOrId(
// "authorizationsExamples"
// );
// const authorizationExample = new Record(authorizationsExamples);
// authorizationExample.set("name", "authorizationExample");
// authorizationExample.set("owner", userA.id);
// dao.saveRecord(authorizationExample);

// /* Authorization */
// const authorizations = dao.findCollectionByNameOrId("authorizations");
// const authorization = new Record(authorizations);
// authorization.set("owner", userA.id);
// authorization.set("users", [userB.id]);
// authorization.set("collection_id", authorizationsExamples.id);
// authorization.set("record_id", authorizationExample.id);
// dao.saveRecord(authorization);

return;
},
(db) => {
return;
}
);
53 changes: 0 additions & 53 deletions admin/pb_migrations/1685000001_webauthn_feature.js

This file was deleted.

Loading