Merge lp:~jameinel/u1db/u1db-client-init into lp:u1db

Proposed by John A Meinel
Status: Merged
Merged at revision: 105
Proposed branch: lp:~jameinel/u1db/u1db-client-init
Merge into: lp:u1db
Prerequisite: lp:~jameinel/u1db/u1db-client-commandline
Diff against target: 79 lines (+37/-0)
2 files modified
u1db/commandline/client.py (+15/-0)
u1db/tests/commandline/test_client.py (+22/-0)
To merge this branch: bzr merge lp:~jameinel/u1db/u1db-client-init
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+80896@code.launchpad.net

Description of the change

This adds 'u1db-client init-db test.db' as a command. Basically get/put/create require the database to already exist, this exposes a way to create a database.

I think we'll want to have a more api-way of creating a database rather than creating the SQLite instance, but this at least gets us something for the demo.

To post a comment you must log in.
Revision history for this message
John A Meinel (jameinel) wrote :

Small update, we need a way to set the 'replica_uid'.

We could do this by setting a random (an actual uuid), and make setting it optional.
But this was the fastest path to getting something.

lp:~jameinel/u1db/u1db-client-init updated
103. By John A Meinel

We need a way to set the uid for the new database.

Ultimately, we could create this ourselves from a random number
generator. For now, pass it on the commandline.

Revision history for this message
Samuele Pedroni (pedronis) wrote :

looks good (but see comments about u1db-client-commandline).

Seems we really need to make set_replica_uid and get_replica_uid public and test what happens if the uid is changed after the fact (I'm thinking copy db change replica_uid, try to sync with original (should be a nop), update try to sync again).

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'u1db/commandline/client.py'
2--- u1db/commandline/client.py 2011-11-01 13:49:23 +0000
3+++ u1db/commandline/client.py 2011-11-01 13:49:23 +0000
4@@ -53,6 +53,16 @@
5 return cmd_get(args.database, args.doc_id, args.outfile, sys.stderr)
6
7
8+def cmd_init_db(database, replica_uid):
9+ """Create a new database"""
10+ db = sqlite_backend.SQLitePartialExpandDatabase(database)
11+ db._set_replica_uid(replica_uid)
12+
13+
14+def client_init_db(args):
15+ return cmd_init_db(args.database, args.replica_uid)
16+
17+
18 def cmd_put(database, doc_id, old_doc_rev, in_file, out_file):
19 """run 'put_doc' and update the data."""
20 db = sqlite_backend.SQLiteDatabase.open_database(database)
21@@ -85,6 +95,11 @@
22 p.add_argument('-V', '--version', action='version', version=_u1db_version)
23 p.add_argument('-v', '--verbose', action='store_true', help='be chatty')
24 subs = p.add_subparsers(title='commands')
25+ parser_init_db = subs.add_parser('init-db', help='Create a new database')
26+ parser_init_db.add_argument('database', help='The database to create')
27+ parser_init_db.add_argument('replica_uid',
28+ help='The unique identifier for this database')
29+ parser_init_db.set_defaults(func=client_init_db)
30 parser_create = subs.add_parser('create',
31 help='Create a new document from scratch')
32 parser_create.add_argument('database', help='The database to update')
33
34=== modified file 'u1db/tests/commandline/test_client.py'
35--- u1db/tests/commandline/test_client.py 2011-11-01 13:49:23 +0000
36+++ u1db/tests/commandline/test_client.py 2011-11-01 13:49:23 +0000
37@@ -73,6 +73,12 @@
38 self.assertEqual('doc-id', args.doc_id)
39 self.assertEqual(sys.stdout, args.outfile)
40
41+ def test_init_db(self):
42+ args = self.parse_args(['init-db', 'test.db', 'replica-uid'])
43+ self.assertEqual(client.client_init_db, args.func)
44+ self.assertEqual('test.db', args.database)
45+ self.assertEqual('replica-uid', args.replica_uid)
46+
47 def test_put(self):
48 args = self.parse_args(['put', 'test.db', 'doc-id', 'old-doc-rev'])
49 self.assertEqual(client.client_put, args.func)
50@@ -127,6 +133,17 @@
51 err.getvalue())
52
53
54+class TestCmdInit(TestCaseWithDB):
55+
56+ def test_init_new(self):
57+ path = self.working_dir + '/test2.db'
58+ self.assertFalse(os.path.exists(path))
59+ client.cmd_init_db(path, 'test-uid')
60+ self.assertTrue(os.path.exists(path))
61+ db = sqlite_backend.SQLiteDatabase.open_database(path)
62+ self.assertEqual('test-uid', db._replica_uid)
63+
64+
65 class TestCmdPut(TestCaseWithDB):
66
67 def setUp(self):
68@@ -211,6 +228,11 @@
69 self.assertEqual(tests.simple_doc, stdout)
70 self.assertEqual('doc_rev: %s\n' % (doc_rev,), stderr)
71
72+ def test_init(self):
73+ path = self.working_dir + '/test2.db'
74+ ret, stdout, stderr = self.run_main(['init-db', path, 'uid'])
75+ db2 = sqlite_backend.SQLiteDatabase.open_database(path)
76+
77 def test_put(self):
78 _, doc_rev = self.db.create_doc(tests.simple_doc, doc_id='test-id')
79 ret, stdout, stderr = self.run_main(

Subscribers

People subscribed via source and target branches