-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9fdad5b
commit 2d3a471
Showing
5 changed files
with
20,659 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]", | ||
|
@@ -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", | ||
|
@@ -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({ | ||
|
@@ -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, | ||
}, | ||
}, | ||
{ | ||
|
@@ -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: {}, | ||
}); | ||
|
@@ -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: [ | ||
|
@@ -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: "", | ||
}, | ||
}, | ||
{ | ||
|
@@ -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: {}, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.