1
0
mirror of https://github.com/pocketbase/pocketbase.git synced 2025-11-06 09:29:19 +02:00

initial public commit

This commit is contained in:
Gani Georgiev
2022-07-07 00:19:05 +03:00
commit 3d07f0211d
484 changed files with 92412 additions and 0 deletions

View File

@@ -0,0 +1,141 @@
// Package migrations contains the system PocketBase DB migrations.
package migrations
import (
"fmt"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/daos"
"github.com/pocketbase/pocketbase/models"
"github.com/pocketbase/pocketbase/models/schema"
"github.com/pocketbase/pocketbase/tools/migrate"
)
var AppMigrations migrate.MigrationsList
// Register is a short alias for `AppMigrations.Register()`
// that is usually used in external/user defined migrations.
func Register(
up func(db dbx.Builder) error,
down func(db dbx.Builder) error,
optFilename ...string,
) {
AppMigrations.Register(up, down, optFilename...)
}
func init() {
AppMigrations.Register(func(db dbx.Builder) error {
_, tablesErr := db.NewQuery(`
CREATE TABLE {{_admins}} (
[[id]] TEXT PRIMARY KEY,
[[avatar]] INTEGER DEFAULT 0 NOT NULL,
[[email]] TEXT UNIQUE NOT NULL,
[[tokenKey]] TEXT UNIQUE NOT NULL,
[[passwordHash]] TEXT NOT NULL,
[[lastResetSentAt]] TEXT DEFAULT "" NOT NULL,
[[created]] TEXT DEFAULT "" NOT NULL,
[[updated]] TEXT DEFAULT "" NOT NULL
);
CREATE TABLE {{_users}} (
[[id]] TEXT PRIMARY KEY,
[[verified]] BOOLEAN DEFAULT FALSE NOT NULL,
[[email]] TEXT UNIQUE NOT NULL,
[[tokenKey]] TEXT UNIQUE NOT NULL,
[[passwordHash]] TEXT NOT NULL,
[[lastResetSentAt]] TEXT DEFAULT "" NOT NULL,
[[lastVerificationSentAt]] TEXT DEFAULT "" NOT NULL,
[[created]] TEXT DEFAULT "" NOT NULL,
[[updated]] TEXT DEFAULT "" NOT NULL
);
CREATE TABLE {{_collections}} (
[[id]] TEXT PRIMARY KEY,
[[system]] BOOLEAN DEFAULT FALSE NOT NULL,
[[name]] TEXT UNIQUE NOT NULL,
[[schema]] JSON DEFAULT "[]" NOT NULL,
[[listRule]] TEXT DEFAULT NULL,
[[viewRule]] TEXT DEFAULT NULL,
[[createRule]] TEXT DEFAULT NULL,
[[updateRule]] TEXT DEFAULT NULL,
[[deleteRule]] TEXT DEFAULT NULL,
[[created]] TEXT DEFAULT "" NOT NULL,
[[updated]] TEXT DEFAULT "" NOT NULL
);
CREATE TABLE {{_params}} (
[[id]] TEXT PRIMARY KEY,
[[key]] TEXT UNIQUE NOT NULL,
[[value]] JSON DEFAULT NULL,
[[created]] TEXT DEFAULT "" NOT NULL,
[[updated]] TEXT DEFAULT "" NOT NULL
);
`).Execute()
if tablesErr != nil {
return tablesErr
}
// inserts the system profiles collection
// -----------------------------------------------------------
profileOwnerRule := fmt.Sprintf("%s = @request.user.id", models.ProfileCollectionUserFieldName)
collection := &models.Collection{
Name: models.ProfileCollectionName,
System: true,
CreateRule: &profileOwnerRule,
ListRule: &profileOwnerRule,
ViewRule: &profileOwnerRule,
UpdateRule: &profileOwnerRule,
Schema: schema.NewSchema(
&schema.SchemaField{
Name: models.ProfileCollectionUserFieldName,
Type: schema.FieldTypeUser,
Unique: true,
Required: true,
System: true,
Options: &schema.UserOptions{
MaxSelect: 1,
CascadeDelete: true,
},
},
&schema.SchemaField{
Name: "name",
Type: schema.FieldTypeText,
Options: &schema.TextOptions{},
},
&schema.SchemaField{
Name: "avatar",
Type: schema.FieldTypeFile,
Options: &schema.FileOptions{
MaxSelect: 1,
MaxSize: 5242880,
MimeTypes: []string{
"image/jpg",
"image/jpeg",
"image/png",
"image/svg+xml",
"image/gif",
},
},
},
),
}
return daos.New(db).SaveCollection(collection)
}, func(db dbx.Builder) error {
tables := []string{
"_params",
"_collections",
"_users",
"_admins",
models.ProfileCollectionName,
}
for _, name := range tables {
if _, err := db.DropTable(name).Execute(); err != nil {
return err
}
}
return nil
})
}

View File

@@ -0,0 +1,38 @@
package logs
import (
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/tools/migrate"
)
var LogsMigrations migrate.MigrationsList
func init() {
LogsMigrations.Register(func(db dbx.Builder) (err error) {
_, err = db.NewQuery(`
CREATE TABLE {{_requests}} (
[[id]] TEXT PRIMARY KEY,
[[url]] TEXT DEFAULT "" NOT NULL,
[[method]] TEXT DEFAULT "get" NOT NULL,
[[status]] INTEGER DEFAULT 200 NOT NULL,
[[auth]] TEXT DEFAULT "guest" NOT NULL,
[[ip]] TEXT DEFAULT "127.0.0.1" NOT NULL,
[[referer]] TEXT DEFAULT "" NOT NULL,
[[userAgent]] TEXT DEFAULT "" NOT NULL,
[[meta]] JSON DEFAULT "{}" NOT NULL,
[[created]] TEXT DEFAULT "" NOT NULL,
[[updated]] TEXT DEFAULT "" NOT NULL
);
CREATE INDEX _request_status_idx on {{_requests}} ([[status]]);
CREATE INDEX _request_auth_idx on {{_requests}} ([[auth]]);
CREATE INDEX _request_ip_idx on {{_requests}} ([[ip]]);
CREATE INDEX _request_created_hour_idx on {{_requests}} (strftime('%Y-%m-%d %H:00:00', [[created]]));
`).Execute()
return err
}, func(db dbx.Builder) error {
_, err := db.DropTable("_requests").Execute()
return err
})
}