add v2 file import

This commit is contained in:
Maxi Quoß
2023-01-16 20:01:08 +01:00
parent a51157f0f3
commit f1a73e7bbe
5 changed files with 211 additions and 2 deletions

View File

@@ -0,0 +1,52 @@
package migrations
import (
"encoding/json"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/models/schema"
)
func init() {
m.Register(func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("z5lghx2r3tm45n1")
if err != nil {
return err
}
// add
new_wake := &schema.SchemaField{}
json.Unmarshal([]byte(`{
"system": false,
"id": "kzvdbbws",
"name": "wake",
"type": "text",
"required": false,
"unique": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
}`), new_wake)
collection.Schema.AddField(new_wake)
return dao.SaveCollection(collection)
}, func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("z5lghx2r3tm45n1")
if err != nil {
return err
}
// remove
collection.Schema.RemoveField("kzvdbbws")
return dao.SaveCollection(collection)
})
}

View File

@@ -0,0 +1,66 @@
package migrations
import (
"encoding/json"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
m "github.com/pocketbase/pocketbase/migrations"
"github.com/pocketbase/pocketbase/models/schema"
)
func init() {
m.Register(func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("z5lghx2r3tm45n1")
if err != nil {
return err
}
// update
edit_shutdown := &schema.SchemaField{}
json.Unmarshal([]byte(`{
"system": false,
"id": "1a7yrwo9",
"name": "shutdown",
"type": "text",
"required": false,
"unique": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
}`), edit_shutdown)
collection.Schema.AddField(edit_shutdown)
return dao.SaveCollection(collection)
}, func(db dbx.Builder) error {
dao := daos.New(db);
collection, err := dao.FindCollectionByNameOrId("z5lghx2r3tm45n1")
if err != nil {
return err
}
// update
edit_shutdown := &schema.SchemaField{}
json.Unmarshal([]byte(`{
"system": false,
"id": "1a7yrwo9",
"name": "shutdown_cmd",
"type": "text",
"required": false,
"unique": false,
"options": {
"min": null,
"max": null,
"pattern": ""
}
}`), edit_shutdown)
collection.Schema.AddField(edit_shutdown)
return dao.SaveCollection(collection)
})
}

View File

@@ -17,7 +17,7 @@
"eslint-plugin-svelte3": "^4.0.0",
"prettier": "^2.8.3",
"prettier-plugin-svelte": "^2.9.0",
"svelte": "^3.55.0",
"svelte": "^3.55.1",
"svelte-preprocess": "^4.10.7",
"vite": "^4.0.4"
},

View File

@@ -14,7 +14,7 @@ specifiers:
prettier: ^2.8.3
prettier-plugin-svelte: ^2.9.0
sass: ^1.57.1
svelte: ^3.55.0
svelte: ^3.55.1
svelte-fa: ^3.0.3
svelte-preprocess: ^4.10.7
vite: ^4.0.4

View File

@@ -0,0 +1,91 @@
<script>
import PocketBase from 'pocketbase';
import { onMount } from 'svelte';
let pb;
let files;
onMount(() => {
pb = new PocketBase('http://127.0.0.1:8090');
});
async function restoreV2Backup(e) {
// delete all devices in pocketbase
let result = await pb.collection('devices').getFullList();
for (let index = 0; index < result.length; index++) {
pb.collection('devices').delete(result[index].id);
}
// delete all ports in pocketbase
result = await pb.collection('ports').getFullList();
for (let index = 0; index < result.length; index++) {
await pb.collection('ports').delete(result[index].id);
}
let file = e.target.files[0];
let reader = new FileReader();
reader.readAsText(file);
reader.onload = async (e) => {
// parse uploaded file
let data = JSON.parse(e.target.result);
if (!Array.isArray(data)) {
return;
}
let createdPorts = {};
for (let index = 0; index < data.length; index++) {
const device = data[index];
// create ports
let thisDevicePorts = [];
for (let index = 0; index < device.ports.length; index++) {
// keep track of already created ports
const port = device.ports[index];
if (createdPorts[port.number] === undefined) {
const record = await pb.collection('ports').create(
{
name: port.name,
number: port.number
},
{ $autoCancel: false }
);
createdPorts[port.number] = record;
thisDevicePorts.push(record.id);
} else {
thisDevicePorts.push(createdPorts[port.number].id);
}
}
// create device
await pb.collection('devices').create(
{
name: device.name,
ip: device.ip,
mac: device.mac,
netmask: device.netmask,
port: thisDevicePorts,
link: device.link,
wake: device.wake,
shutdown: device.shutdown
},
{ $autoCancel: false }
);
}
};
}
</script>
<div class="container">
<h1>Settings</h1>
<div class="input-group flex-nowrap">
<input
class="form-control"
placeholder="Username"
aria-label="Username"
aria-describedby="addon-wrapping"
type="file"
accept=".json"
bind:files
on:change={(e) => restoreV2Backup(e)}
/>
</div>
</div>