-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsetup_schema.py
More file actions
162 lines (138 loc) · 5.26 KB
/
setup_schema.py
File metadata and controls
162 lines (138 loc) · 5.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
"""
setup_schema.py — Create the memory.* schema for clawbot-sql-memory
Run once before first use:
python3 setup_schema.py # uses 'local' profile
python3 setup_schema.py --cloud # uses 'cloud' profile
Requires .env with SQL credentials. See README.md for format.
"""
import argparse
import sys
import os
sys.path.insert(0, os.path.dirname(__file__))
try:
from sql_connector import get_connector
except ImportError:
print("ERROR: sql-connector not found. Install it first:")
print(" clawhub install sql-connector")
sys.exit(1)
SCHEMA_SQL = [
("CREATE SCHEMA memory", "memory schema"),
("""
CREATE TABLE memory.Memories (
id INT IDENTITY(1,1) PRIMARY KEY,
category NVARCHAR(100) NOT NULL,
[key] NVARCHAR(255) NOT NULL,
content NVARCHAR(MAX) NOT NULL,
importance INT DEFAULT 3,
tags NVARCHAR(500) DEFAULT '',
status NVARCHAR(50) DEFAULT 'active',
created_at DATETIME2 DEFAULT GETUTCDATE(),
updated_at DATETIME2 DEFAULT GETUTCDATE()
)
""", "memory.Memories"),
("""
CREATE TABLE memory.TaskQueue (
id INT IDENTITY(1,1) PRIMARY KEY,
agent NVARCHAR(100) NOT NULL,
task_type NVARCHAR(100) NOT NULL,
payload NVARCHAR(MAX) DEFAULT '',
priority INT DEFAULT 5,
status NVARCHAR(50) DEFAULT 'pending',
retries INT DEFAULT 0,
model_hint NVARCHAR(100) DEFAULT '',
created_at DATETIME2 DEFAULT GETUTCDATE(),
updated_at DATETIME2 DEFAULT GETUTCDATE(),
claimed_at DATETIME2 NULL,
completed_at DATETIME2 NULL,
error NVARCHAR(MAX) DEFAULT ''
)
""", "memory.TaskQueue"),
("""
CREATE TABLE memory.ActivityLog (
id INT IDENTITY(1,1) PRIMARY KEY,
event_type NVARCHAR(100) NOT NULL,
agent NVARCHAR(100) DEFAULT '',
description NVARCHAR(MAX) DEFAULT '',
metadata NVARCHAR(MAX) DEFAULT '',
importance INT DEFAULT 3,
created_at DATETIME2 DEFAULT GETUTCDATE()
)
""", "memory.ActivityLog"),
("""
CREATE TABLE memory.Sessions (
id INT IDENTITY(1,1) PRIMARY KEY,
session_key NVARCHAR(255) NOT NULL,
agent NVARCHAR(100) DEFAULT '',
status NVARCHAR(50) DEFAULT 'active',
metadata NVARCHAR(MAX) DEFAULT '',
started_at DATETIME2 DEFAULT GETUTCDATE(),
ended_at DATETIME2 NULL
)
""", "memory.Sessions"),
("""
CREATE TABLE memory.KnowledgeIndex (
id INT IDENTITY(1,1) PRIMARY KEY,
domain NVARCHAR(100) NOT NULL,
[key] NVARCHAR(255) NOT NULL,
content NVARCHAR(MAX) NOT NULL,
source NVARCHAR(255) DEFAULT '',
tags NVARCHAR(500) DEFAULT '',
created_at DATETIME2 DEFAULT GETUTCDATE()
)
""", "memory.KnowledgeIndex"),
("""
CREATE TABLE memory.Todos (
id INT IDENTITY(1,1) PRIMARY KEY,
title NVARCHAR(500) NOT NULL,
description NVARCHAR(MAX) DEFAULT '',
priority INT DEFAULT 3,
status NVARCHAR(50) DEFAULT 'open',
tags NVARCHAR(500) DEFAULT '',
created_at DATETIME2 DEFAULT GETUTCDATE(),
updated_at DATETIME2 DEFAULT GETUTCDATE(),
closed_at DATETIME2 NULL
)
""", "memory.Todos"),
]
def table_exists(db, table_name):
schema, name = table_name.split('.')
rows = db.query(
"SELECT 1 FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = %s AND TABLE_NAME = %s",
(schema, name)
)
return len(rows) > 0
def schema_exists(db, schema_name):
rows = db.query(
"SELECT 1 FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = %s",
(schema_name,)
)
return len(rows) > 0
def run(profile='local'):
print(f"\nclawbot-sql-memory schema setup — profile: {profile}\n")
db = get_connector(profile)
if not db.ping():
print("ERROR: Cannot connect to SQL Server. Check your .env credentials.")
sys.exit(1)
print("✅ Connected to SQL Server\n")
for sql, label in SCHEMA_SQL:
if label == "memory schema":
if schema_exists(db, 'memory'):
print(f" SKIP {label} (already exists)")
continue
else:
if table_exists(db, label):
print(f" SKIP {label} (already exists)")
continue
ok = db.execute(sql.strip())
if ok:
print(f" CREATE {label}")
else:
print(f" ERROR {label} — check logs")
print("\nSchema setup complete.\n")
print("Next step: configure your .env and run:")
print(" python3 -c \"from sql_memory import get_memory; print(get_memory('local').ping())\"")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Create memory.* schema for clawbot-sql-memory')
parser.add_argument('--cloud', action='store_true', help='Use cloud SQL profile')
args = parser.parse_args()
run('cloud' if args.cloud else 'local')