Skip to content

Commit

Permalink
debugged migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
TobiasNickel committed Jun 2, 2024
1 parent 9fdad5b commit 2d3a471
Show file tree
Hide file tree
Showing 5 changed files with 20,659 additions and 77 deletions.
3 changes: 3 additions & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ const password = options.opt.password || "sidecart"

async function main(){
const pb = await connect(HOST, options.opt.admin, userName, password)
console.log('connected to pocketbase', {HOST, userName, password, isAdmin: options.opt.admin})


if(!options.opt.command){ return }
if (options.opt.command.toLowerCase() === "migrate") {
Expand All @@ -74,6 +76,7 @@ async function main(){

main().catch(e=>{
console.error(e)
console.log(e.stack)
process.exit(1)
})

140 changes: 95 additions & 45 deletions lib/migrate.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,102 @@
import PocketBase from 'pocketbase'
import { migrations } from '../migrations';
import PocketBase from "pocketbase";
import { migrations } from "../migrations";

export const runMigrations = async (pb: PocketBase): Promise<void> => {
const versionModel = await getMigrateVersion(pb)
const version = versionModel.value
console.log('migrations', version , '/',migrations.length)
for (let i = version; i < migrations.length; i++) {
console.log('running migration', i)
await migrations[i].up(pb)
await pb.collection('SC_config').update(versionModel.id, { value: i+1 })
console.log('migrations done', i+1, '/', migrations.length)
}
}
const versionModel = await getMigrateVersion(pb);
const version = versionModel.value;
console.log("migrations", version, "/", migrations.length);
for (let i = version; i < migrations.length; i++) {
console.log("running migration", i);
await migrations[i].up(pb);
await pb.collection("SC_config").update(versionModel.id, { value: i + 1 });
console.log("migrations done", i + 1, "/", migrations.length);
}
};

async function getMigrateVersion(pb: PocketBase) {
// todo: try read from SC_config
try {
return (await pb.collection('SC_config').getList(0,1,{filter: 'key="migrate_version"'})).items[0]
} catch (e) {
await pb.collections.create({
id: "",
created: "",
updated: "",
name: "SC_config",
type: "base",
system: true,
listRule: '@request.auth.id = @collection.SC_user.id',
viewRule: '@request.auth.id = @collection.SC_user.id',
createRule: null,
updateRule: null,
deleteRule: null,
schema: [
{ "id": "", "name": "key", "type": "text", "system": false, "required": false, "unique": false, "options": { "min": 1, "max": 100, "pattern": "" } },
{ "id": "", "name": "value", "type": "json", "system": false, "required": true, "unique": false, "options": { "maxSize": 2000000 }},
],
indexes: [],
options: {},
originalName: ""
})
return await pb.collection('SC_config').create({ key: 'migrate_version', value: '0' })
}
// todo: try read from SC_config
try {
console.log("getMigrateVersion");
return (
await pb
.collection("SC_config")
.getList(0, 1, { filter: 'key="migrate_version"' })
).items[0];
} catch (e) {
const userCollection = await pb.collections.create({
name: "SC_user",
type: "auth",
system: false,
schema: [],
indexes: [],
options: {
allowEmailAuth: true,
allowOAuth2Auth: false,
allowUsernameAuth: true,
exceptEmailDomains: [],
manageRule: null,
minPasswordLength: 8,
onlyEmailDomains: [],
onlyVerified: false,
requireEmail: false,
},
});
await pb.collections.update(userCollection.id, {
listRule: "@request.auth.id = @collection.SC_user.id",
viewRule: "@request.auth.id = @collection.SC_user.id",
createRule: "@request.auth.id = @collection.SC_user.id",
updateRule: "@request.auth.id = @collection.SC_user.id",
deleteRule: "@request.auth.id = @collection.SC_user.id",
})
console.log("creating SC_config");
await pb.collections.create({
id: "",
created: "",
updated: "",
name: "SC_config",
type: "base",
system: true,
listRule: "@request.auth.id = @collection.SC_user.id",
viewRule: "@request.auth.id = @collection.SC_user.id",
createRule: "@request.auth.id = @collection.SC_user.id",
updateRule: "@request.auth.id = @collection.SC_user.id",
deleteRule: "@request.auth.id = @collection.SC_user.id",
schema: [
{
id: "",
name: "key",
type: "text",
system: false,
required: false,
unique: false,
options: { min: 1, max: 100, pattern: "" },
},
{
id: "",
name: "value",
type: "json",
system: false,
required: true,
unique: false,
options: { maxSize: 2000000 },
},
],
indexes: [],
options: {},
originalName: "",
});
console.log("creating migrate_version");
return await pb
.collection("SC_config")
.create({ key: "migrate_version", value: "0" });
}
}

export async function checkMigration(pb: PocketBase) {
const migrationVersion = await getMigrateVersion(pb)
if (migrations.length > migrationVersion.value) {
console.log('Migration needed')
} else {
console.log('Migrations up to date')
}
}
const migrationVersion = await getMigrateVersion(pb);
if (migrations.length > migrationVersion.value) {
console.log("Migration needed");
} else {
console.log("Migrations up to date");
}
}
90 changes: 63 additions & 27 deletions migrations/0001.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import PocketBase from "pocketbase";

export const up = async (db: PocketBase) => {

await db.collection("SC_user").create({
username: "sidecart",
email: "[email protected]",
Expand Down Expand Up @@ -53,6 +54,11 @@ export const up = async (db: PocketBase) => {
name: "SC_function",
type: "base",
system: false,
listRule: "@request.auth.id = @collection.SC_user.id",
viewRule: "@request.auth.id = @collection.SC_user.id",
createRule: "@request.auth.id = @collection.SC_user.id",
updateRule: "@request.auth.id = @collection.SC_user.id",
deleteRule: "@request.auth.id = @collection.SC_user.id",
schema: [
{
name: "name",
Expand All @@ -78,8 +84,22 @@ export const up = async (db: PocketBase) => {
pattern: "",
},
},
{
name: "error",
type: "text",
system: false,
required: false,
unique: false,
options: {
min: null,
max: null,
pattern: "",
},
},
],
indexes: [
"CREATE UNIQUE INDEX `idx_functions_name` ON `SC_function` (`name`)",
],
indexes: ["CREATE UNIQUE INDEX `idx_A1yFVVJ` ON `SC_function` (`name`)"],
options: {},
});
await db.collections.create({
Expand All @@ -101,16 +121,25 @@ export const up = async (db: PocketBase) => {
},
{
name: "function",
type: "relation",
type: "text",
system: false,
required: false,
unique: false,
options: {
collectionId: functionCollection.id,
cascadeDelete: false,
minSelect: null,
maxSelect: 1,
displayFields: null,
min: null,
max: null,
pattern: "",
},
},
{
system: false,
name: "arg",
type: "json",
required: false,
presentable: false,
unique: false,
options: {
maxSize: 2000000,
},
},
{
Expand All @@ -127,17 +156,20 @@ export const up = async (db: PocketBase) => {
},
},
{
name: "paused",
type: "bool",
system: false,
name: "arg",
type: "json",
required: false,
presentable: false,
unique: false,
options: {
maxSize: 2000000,
},
options: {},
},
],
listRule: "@request.auth.id = @collection.SC_user.id",
viewRule: "@request.auth.id = @collection.SC_user.id",
createRule: "@request.auth.id = @collection.SC_user.id",
updateRule: "@request.auth.id = @collection.SC_user.id",
deleteRule: "@request.auth.id = @collection.SC_user.id",
indexes: [],
options: {},
});
Expand Down Expand Up @@ -195,17 +227,19 @@ export const up = async (db: PocketBase) => {
options: {},
},
],
indexes: ["CREATE UNIQUE INDEX `idx_path` ON `SC_public_files` (`path`)"],
listRule: "@[email protected]_user.id",
viewRule: "@[email protected]_user.id",
createRule: null,
updateRule: null,
deleteRule: "@[email protected]_user.id",
indexes: [
"CREATE UNIQUE INDEX `idx_public_files_path` ON `SC_public_files` (`path`)",
],
listRule: "@request.auth.id = @collection.SC_user.id",
viewRule: "@request.auth.id = @collection.SC_user.id",
createRule: "@request.auth.id = @collection.SC_user.id",
updateRule: "@request.auth.id = @collection.SC_user.id",
deleteRule: "@request.auth.id = @collection.SC_user.id",
options: {},
});

await db.collections.create({
name: "SC_hooks",
name: "SC_hooks_files",
type: "base",
system: false,
schema: [
Expand All @@ -225,12 +259,14 @@ export const up = async (db: PocketBase) => {
{
system: false,
name: "textContent",
type: "editor",
type: "text",
required: false,
presentable: false,
unique: false,
options: {
convertUrls: false,
min: null,
max: null,
pattern: "",
},
},
{
Expand All @@ -243,12 +279,12 @@ export const up = async (db: PocketBase) => {
options: {},
},
],
indexes: ["CREATE UNIQUE INDEX `idx_path` ON `SC_public_files` (`path`)"],
listRule: "@request.auth.id=@collection.SC_user.id",
viewRule: "@request.auth.id=@collection.SC_user.id",
createRule: null,
updateRule: null,
deleteRule: "@request.auth.id=@collection.SC_user.id",
indexes: ["CREATE UNIQUE INDEX `idx_hooks_name` ON `SC_hooks` (`name`)"],
listRule: "@request.auth.id = @collection.SC_user.id",
viewRule: "@request.auth.id = @collection.SC_user.id",
createRule: "@request.auth.id = @collection.SC_user.id",
updateRule: "@request.auth.id = @collection.SC_user.id",
deleteRule: "@request.auth.id = @collection.SC_user.id",
options: {},
});
};
10 changes: 5 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
"type": "module",
"scripts": {
"build": "bun run buildLinux && bun run buildArm64 && bun run buildWindows64 && bun run buildMacArm64 && bun run buildMacX64",
"buildLinux": "bun build --compile index.ts --target=bun-linux-x64 --outfile pocketbasesidecart_linux_x64",
"buildArm64": "bun build --compile index.ts --target=bun-linux-arm64 --outfile pocketbasesidecart_linux_arm64",
"buildWindows64": "bun build --compile index.ts --target=bun-windows-x64 --outfile pocketbasesidecart_windows_x64",
"buildMacArm64": "bun build --compile index.ts --target=bun-darwin-arm64 --outfile pocketbasesidecart_mac_arm64",
"buildMacX64": "bun build --compile index.ts --target=bun-darwin-x64 --outfile pocketbasesidecart_mac_x64",
"buildLinux": "bun build --compile index.ts --target=bun-linux-x64 --sourcemap --outfile pocketbasesidecart_linux_x64",
"buildArm64": "bun build --compile index.ts --target=bun-linux-arm64 --sourcemap --outfile pocketbasesidecart_linux_arm64",
"buildWindows64": "bun build --compile index.ts --target=bun-windows-x64 --sourcemap --outfile pocketbasesidecart_windows_x64",
"buildMacArm64": "bun build --compile index.ts --target=bun-darwin-arm64 --sourcemap --outfile pocketbasesidecart_mac_arm64",
"buildMacX64": "bun build --compile index.ts --target=bun-darwin-x64 --sourcemap --outfile pocketbasesidecart_mac_x64",
"start": "bun run --entry dist/index.js"
},
"devDependencies": {
Expand Down
Loading

0 comments on commit 2d3a471

Please sign in to comment.