<p>Do you generate code from a database schema, or do you generate the database schema from Go code?</p>
<hr/>**评论:**<br/><br/>fr0z3nph03n1x: <pre><p>I was using goose. <a href="https://getgophish.com/blog/post/database-migrations-in-go/" rel="nofollow">https://getgophish.com/blog/post/database-migrations-in-go/</a></p>
<p>I didn't like it as much as how knex handles migrations in node but it was ok. You should deff be using a migration manager. </p></pre>mattinielsen: <pre><p>I am using it as well, i didn't pick it, just stuck at it. I think its a problem using git with it, because the versions are sequential. So if two programmers do migrations and runs them without pushing it could become out of sync. I was thinking about using <a href="https://github.com/syncthing/syncthing" rel="nofollow">https://github.com/syncthing/syncthing</a> for fixing it, just a tip. </p></pre>indienick: <pre><p>I've recently started using github.com/mattes/migrate as a library, within my programs, and using github.com/jteeuwen/ go-bindata to bundle the migration scripts up.</p></pre>semisync: <pre><p>I'm currently working on better support for this in <a href="http://github.com/rbastic/dyndao" rel="nofollow">http://github.com/rbastic/dyndao</a> - already there exists a layer where Go Schema structures can be determined using either an information_schema parser, or an Oracle metadata parser.</p></pre>drink_with_me_to_day: <pre><p>I currently have a home-baked information_schema parser that generates table structs (to use with sqlx), where each table column is a type and implements, along with Valuer and Scanner, these interfaces (to facilitate custom queries, i.e., with squirrel):</p>
<pre><code>type Named interface {
Name() string
}
type Column interface {
Named
Type() string
NonNull() bool
PK() bool
FK() (Column, bool)
Table() Table
Validate() error
}
type Table interface {
Named
Columns() []Column
}
</code></pre>
<p>I made some squirrel CRUD helpers, but when trying to select from the database, I don't want to receive an <code>interface{}</code>, nor want to use <code>reflect</code>. </p>
<p>After a while I just rolled with hand-made queries and copy-paste squirrel CRUD code.</p></pre>kemitche: <pre><p>I'm confused about your squirrel comment. I use squirrel and I never "receive" an <code>interface{}</code>. I pass my structs in to squirrel and squirrel fills things out for me. Squirrel <em>accepts</em> <code>interface{}</code>, but that just means squirrel doesn't require any particular methods to be declared on the structs you pass in.</p></pre>drink_with_me_to_day: <pre><p>The <code>interface{}</code> comment was when making generic struct CRUD functions, like <code>getOne(c Table) interface{}</code> not squirrel.</p></pre>kemitche: <pre><p>Ah I see, my mistake.</p></pre>semisync: <pre><p>Yeah, dyndao offers methods to cast / convert for your preferred type. It tries to hide away the interface{}s underneath the hood, but they're there if you need them. Reflect is only minimally used.</p>
<p>I see the value in all approaches to Go and databases, there's no one size fits all. Is your code open source? Would love to take a look through it.</p></pre>drink_with_me_to_day: <pre><p>The code is very messy, so I didn't share it.</p></pre>semisync: <pre><p>ORM code is messy by definition, but I understand :)</p></pre>foolano23: <pre><p><a href="https://povilasv.me/go-schema-migration-tools/" rel="nofollow">https://povilasv.me/go-schema-migration-tools/</a></p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传