-
-
Notifications
You must be signed in to change notification settings - Fork 311
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Editable schema #174
Comments
Replacing schema is trivial to do in user code. What I don't want to do is to provide schema altering fn that will do that and ignore all the issues that arise from the fact that data stored in DS might not suit new schema (unique values, ref type, arities). If I ever provide such fn, it should deal with all the issues (~the same way Datomic deals with them, checking constraints, raising errors, not allowing certain migrations, etc). I'm not against changing schema, but for now only user of DS library is responsible for all the inconsistencies. That's why I think such a fn should not be included in DS yet (but all the tools to build it yourself are there already) |
Fair enough. In my case (datsync), the only schema changes that come through are ones that had already passed through Datomic, so they'd more or less been "vetted". But you're right that there shouldn't be surprises in something that's a core part of datascript, and it would be easy for someone not thinking about the consequences to goof. Here are my thoughts on these issues:
Here are the relevant Datomic docs for reference: http://docs.datomic.com/schema.html#Schema-Alteration. I get that since it's possible for users to deal with these issues themselves when they need it, this isn't a high priority in your book. However, I think for new users especially, it may not be obvious how might use |
I think schema migration should work one of three possible ways:
Important here is that DataScript will not change any user data as a result of schema alteration. That way user will have a chance to clean up data in the way that suits them, and DataScript will keep them safe by providing guarantees. This would be an important piece of DataScript and I'll be happy to include it. |
Agreed; Sounds good. |
@tonsky I have an implementation of what you and @metasoarous describe in |
@tonsky did you mean the If I want to add new attributes and don't touch already existing ones, then can I use a function like this? ;; just an example
(defn patch-schema [db patch]
(let [schema (:schema db)
new-schema (merge patch schema)
datoms (d/datoms db :eavt)]
(d/init-db datoms new-schema))) The order of merge arguments is important. Probably it will be better to check conflicts there. |
Seems fine, but |
Updating existed database is very tricky and requires support for new features like tuples. (defn with-schema [db new-schema]
(-> (d/empty-db new-schema)
(with-meta (meta db))
(d/db-with (d/datoms db :eavt))))
(defn vary-schema [db f & args]
(with-schema db (apply f (d/schema db) args))) Validation is done by I use a DB as a value. So I can use prototypes: (def blank-entity
(-> (d/empty-db {:error/entity {:db/valueType :db.type/ref}})
(d/db-with [{:db/id 1
:db/ident :root}])))
(def blank-user
(-> blank-entity
(vary-schema assoc :user/aka {:db/cardinality :db.cardinality/many})))
(def john (-> blank-user
(d/db-with [{:db/ident :root
:user/name "Maksim"
:user/aka ["Max Otto von Stierlitz", "Jack Ryan"]}]))) Can I send PR with these functions? |
This is what I am thinking:
If at any point in the future we might introduce actual schema migrations, they will probably have different API that will allow for more efficient implementation (e.g. by providing schema deltas instead of entirely new schema). So I don’t want to be locked right now into this API |
I have an implementation of this in Datsync (here), and I'd really rather have it just be a function in
datascript.core
if you're amenable. More or less, it creates a new database from the old datoms and the updated schema.There's a question about what the should be sent to listeners when this happens. It could perhaps be a more or less empty transaction report with keys
:schema-changes
,:new-schema
and:old-schema
, perhaps.Happy to clean this up and PR if you like.
The text was updated successfully, but these errors were encountered: