关于git:8000-Star一个使用-Git-命令操作的数据库

3次阅读

共计 3516 个字符,预计需要花费 9 分钟才能阅读完成。

Git 和 MySQL 的「孩子」,一个能够应用 Git 操作的数据库。近期间断在 GitHub 趋势榜霸榜,新增 4k+ Star。

简介

Dolt 是一个 SQL 数据库,咱们能够应用 fork、clone、branch、merge、push、pull 等性能,就像在操作一个 git 仓库一样;同时,它也像 MySQL 一样,只有连贯上 Dolt,咱们就能够应用 SQL 语句进行数据的查问、更新等操作。应用命令行导入 CSV 文件,提交更改,将其推送到近程或合并团队成员的更改。

Git 的所有命令对于 Dolt 来说都是试用的,完全一致,Dolt 感觉就像是 Git 和 MySQL 的孩子一样。

Dolt 有以下命令:

$ dolt
Valid commands for dolt are
                init - 创立一个 Dolt 数据仓库.
              status - 查看工作空间状态.
                 add - 增加批改到暂存区.
               reset - 移除暂存区的批改.
              commit - 提交提交到仓库.
                 sql - 在仓库中运行某一个 sql 命令.
          sql-server - 启动 MySQL 兼容服务器.
                 log - 查看提交日志.
                diff - 比拟表.
               blame - 查看表每行最初批改的用户及版本号 e.
               merge - 合并分支.
              branch - 创立,查看,编辑或删除分支.
                 tag - 创立,查看,编辑或删除标签.
            checkout - 切换某个分支或笼罩表.
              remote - 治理近程仓库.
                push - 推送到近程仓库.
                pull - 拉取近程仓库数据并合并.
               fetch - 从近程仓库更新数据.
               clone - clone 近程仓库数据.
               creds - 身份凭证的治理.
               login - 登录近程 Dolt 主机.
             version - 查看 Dolt 版本.
              config - Dolt 相干配置.
                  ls - 查看工作区中的表.
              schema - 查看或导入表构造.
               table - 复制,重命名,删除或导出表.
           conflicts - 查看以及解决合并抵触.
             migrate - 执行存储库迁徙以更新为最新格局.
         read-tables - 将特定提交处的表提取到新的仓库中
                  gc - 从仓库中革除未援用的数据.

我的项目地址:
https://github.com/dolthub/dolt

装置

  • 在 Linux 或 Mac 上应用以下命令装置:
sudo bash -c 'curl -L https://github.com/dolthub/dolt/releases/latest/download/install.sh | bash'
  • 应用 Homebrew 装置:
brew install dolt
  • Windows 装置下载 msi 文件间接运行即可
  • 源码装置,依赖 Go 环境,将 github 源码 clone 之后,进入到 go 文件夹,运行以下命令:
go install ./cmd/dolt

应用

以存储州人口数据为例,简略介绍 Dolt 应用。

$ mkdir state-pops
$ cd state-pops
  • 执行 dolt init 创立一个 dolt 仓库,并运行 SQL 语句增加数据
$ dolt init
Successfully initialized dolt data repository.
$ dolt sql -q "create table state_populations (state varchar(14), population int, primary key (state) )"
$ dolt sql -q "show tables"
+-------------------+
| tables            |
+-------------------+
| state_populations |
+-------------------+
$ dolt sql -q "insert into state_populations (state, population) values
('Delaware', 59096),
('Maryland', 319728),
('Tennessee', 35691),
('Virginia', 691937),
('Connecticut', 237946),
('Massachusetts', 378787),
('South Carolina', 249073),
('New Hampshire', 141885),
('Vermont', 85425),
('Georgia', 82548),
('Pennsylvania', 434373),
('Kentucky', 73677),
('New York', 340120),
('New Jersey', 184139),
('North Carolina', 393751),
('Maine', 96540),
('Rhode Island', 68825)"
Query OK, 17 rows affected
  • 应用 dolt sql 进入 SQL 命令窗口,或者应用 - q 间接执行 SQL 语句
$ dolt sql -q "select * from state_populations where state ='New York'"
+----------+------------+
| state    | population |
+----------+------------+
| New York | 340120     |
+----------+------------+
  • add 新的表并提交。每一个命令的含意都和 git 一样,只不过 Dolt 针对表,Git 针对文件
$ dolt add .
$ dolt commit -m "initial data"
$ dolt status
On branch master
nothing to commit, working tree clean
  • 应用 SQL 更新表,这次进入 SQL 命令窗口执行:
$ dolt sql
# Welcome to the DoltSQL shell.
# Statements must be terminated with ';'.
# "exit" or "quit" (or Ctrl-D) to exit.
state_pops> update state_populations set population = 0 where state like 'New%';
Query OK, 3 rows affected
Rows matched: 3  Changed: 3  Warnings: 0
state_pops> exit
Bye
  • 应用 diff 看看有什么变动:
$ dolt diff
diff --dolt a/state_populations b/state_populations
--- a/state_populations @ qqr3vd0ea6264oddfk4nmte66cajlhfl
+++ b/state_populations @ 17cinjh5jpimilefd57b4ifeetjcbvn2
+-----+---------------+------------+
|     | state         | population |
+-----+---------------+------------+
|  <  | New Hampshire | 141885     |
|  >  | New Hampshire | 0          |
|  <  | New Jersey    | 184139     |
|  >  | New Jersey    | 0          |
|  <  | New York      | 340120     |
|  >  | New York      | 0          |
+-----+---------------+------------+
  • 提交批改:
$ dolt add state_populations
$ dolt commit -m "More like Old Jersey"
  • 导入数据,应用 dolt table import 能够导入 CSV 或者 JSON 数据。-u 选项示意将数据导入到已有表中,- c 示意创立表并导入数据:
$ head -n3 data.csv
state,population
Delaware,59096
Maryland,319728
$ dolt table import -c -pk=state state_populations data.csv
  • 就像 git 一样,最好在本人的分支上批改,而后再合并到 master 中
$ dolt checkout -b <branch>
$ dolt merge <branch>
  • Dolt 也反对近程仓库治理,在 clone 数据的时候,近程仓库会主动建设关联
$ dolt clone dolthub/corona-virus
...
$ cd corona-virus
$ dolt remote -v
origin https://doltremoteapi.dolthub.com/dolthub/corona-virus
  • 如果仓库是在本地创立,也能够推送到远端并创立近程仓库
$ dolt remote add origin myname/myRepo
$ dolt remote -v
origin https://doltremoteapi.dolthub.com/myname/myRepo
$ dolt push origin master

内容转载于「开源前哨」

正文完
 0