prick 0.35.0 → 0.36.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b8acf851da65b094d2abab5a6d9469f20a974732b9e8a8f5bbc4542f72bf8dae
4
- data.tar.gz: d7ce7beb146b47aea6f25a93771b206a41434bd2ecbd3a1df33b8580374e29ea
3
+ metadata.gz: 49ed6762f3557cc979f89bb1aaee3d5aa1d8d11a5ba9c7880a8b692d917dc733
4
+ data.tar.gz: 16436521e2f9a51ed6c2076ba2a54f968735868a711d170253691a69a4b34fb4
5
5
  SHA512:
6
- metadata.gz: b861e5129259bcb0dd384ba7e3c2db09c826cd50105ec514bda309268779e56458a99a3ef8ca1b83226accf7ce6569b957790e5c4374473a3cfb3700ba5ef380
7
- data.tar.gz: 6c1b7227ffa63e49825f4acb503d855b9110f18224c1b3f2f7b27b72a92ac0eb3865ea8926470995a71988cb64ab9aa4a5bb26420adafd152364fd31ee28d23a
6
+ metadata.gz: eedba5e0757b8f19c94d4bf898ea156b93090cd2466e333860d7a4da9ce91d75fbc4acab596bbc06b0a8f260909c9770715299c55040c017d45b7f679d5528f0
7
+ data.tar.gz: 7d98f2a0a8e6ed8230bc44b3d16d1c46f4baa85956bdbcd893325e2d062d8a6d46c4277cf85af8c5e833fb20bbf8324a75d8788d05418cd87185976c6c16a76a
data/exe/prick CHANGED
@@ -150,6 +150,12 @@ SPEC = %(
150
150
  different data sets
151
151
 
152
152
  # TODO: A --clean option that resets data
153
+ #
154
+
155
+ snapshot!
156
+ Records maximum ID for all tables and stores them in prick.snapshot. It is
157
+ used by 'drop data' to reset data to the point in time of the snapshot.
158
+ Only one snapshot can be created at a time
153
159
 
154
160
  version!
155
161
  Print project version
@@ -296,7 +302,6 @@ begin
296
302
  database = state.database
297
303
  username = state.username
298
304
  environment = state.environment
299
-
300
305
  # Process subcommands
301
306
  case opts.subcommand
302
307
  when :version!
@@ -379,12 +384,18 @@ begin
379
384
  step: cmd.step?, timer: cmd.time?, dump: dump, schema: cmd.schema)
380
385
 
381
386
  when :bash!
387
+ args.expect(0)
382
388
  Prick::SubCommand.bash(main: cmd.main?)
383
389
 
384
390
  when :fox!
385
391
  require_db
386
392
  Prick::SubCommand.fox(database, username, args)
387
393
 
394
+ when :snapshot!
395
+ require_db
396
+ args.expect(0)
397
+ Prick::SubCommand.snapshot(database, username)
398
+
388
399
  when :drop!
389
400
  case cmd.subcommand
390
401
  when :users!
@@ -400,7 +411,9 @@ begin
400
411
  database = args.expect(0..1) || database
401
412
  Prick::SubCommand.drop_database(database)
402
413
  when :data!
403
- raise NotImplementedError
414
+ args.expect(0)
415
+ require_db
416
+ Prick::SubCommand.drop_data(database)
404
417
  when :schema!
405
418
  require_db
406
419
  schemas = args.to_a
@@ -35,3 +35,12 @@ create table builds (
35
35
  make_duration float -- Duration of enclosing make script
36
36
  );
37
37
 
38
+ create table snapshots (
39
+ id integer generated by default as identity primary key,
40
+ schema_name varchar not null,
41
+ table_name varchar not null,
42
+ max_id integer, -- may be null
43
+
44
+ unique(schema_name, table_name)
45
+ );
46
+
@@ -41,6 +41,35 @@ module Prick::SubCommand
41
41
  State.connection { |conn| conn.rdbms.drop database }
42
42
  end
43
43
 
44
+ # Drop data not in snapshot
45
+ def self.drop_data(database, schemas = nil)
46
+ conn = Prick.state.conn
47
+ conn.session.triggers(false) {
48
+ if schemas.nil?
49
+ builder = Prick::Build::Builder.new(conn, Prick.state.schema_dir)
50
+ pool = builder.pool
51
+ schemas = pool.refresh_schemas
52
+ end
53
+ schema_list = conn.quote_literal_list(schemas)
54
+ schema_expr = schemas ? "true = true" : "schema_name in #{schema_list}"
55
+ tables = conn.tuples %(
56
+ select schema_name, table_name, max_id
57
+ from prick.snapshots
58
+ where #{schema_expr}
59
+ )
60
+ tables.each { |schema, table, max_id|
61
+ uid = "#{schema}.#{table}"
62
+ if max_id
63
+ conn.exec "delete from #{uid} where id > #{max_id}"
64
+ conn.schema.set_serial(schema, table, max_id) if conn.schema.sequence(schema, table)
65
+ else
66
+ conn.exec "delete from #{uid}"
67
+ conn.schema.set_serial(schema, table, nil) if conn.schema.sequence(schema, table)
68
+ end
69
+ }
70
+ }
71
+ end
72
+
44
73
  # Empty the database
45
74
  def self.drop_schema(database, schemas = [])
46
75
  constrain database, String
@@ -65,7 +65,7 @@ module Prick::SubCommand
65
65
  end
66
66
  end
67
67
 
68
- # Write-back schema to builder
68
+ # Write back schema to builder
69
69
  builder.root.schema = schema
70
70
 
71
71
  # Drop schema if needed
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require_relative '../builder/builder.rb'
4
+
5
+ module Prick::SubCommand
6
+ def self.snapshot(
7
+ database, username,
8
+ builddir: Prick.state.schema_dir)
9
+
10
+ conn = Prick.state.connection
11
+ builder = Prick::Build::Builder.new(conn, builddir)
12
+ pool = builder.pool
13
+ schemas = pool.refresh_schemas
14
+
15
+ # Clear snapshots table
16
+ conn.exec "delete from prick.snapshots"
17
+
18
+ # Fill it again
19
+ records = []
20
+ for schema in schemas
21
+ conn.schema.list_tables(schema).each { |table|
22
+ max_id = conn.value "select max(id) from #{schema}.#{table}"
23
+ records << { schema_name: schema, table_name: table, max_id: max_id}
24
+ }
25
+ end
26
+ conn.insert("prick", "snapshots", records)
27
+ end
28
+ end
29
+
@@ -1,4 +1,4 @@
1
- module Prick::Subcommand
1
+ module Prick::SubCommand
2
2
  # def self.parse_username_database
3
3
  # case username_database
4
4
  # when nil
data/lib/prick/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Prick
4
- VERSION = "0.35.0"
4
+ VERSION = "0.36.0"
5
5
  end
data/lib/prick.rb CHANGED
@@ -53,6 +53,7 @@ require_relative 'prick/subcommand/prick-release.rb'
53
53
  require_relative 'prick/subcommand/prick-run.rb'
54
54
  require_relative 'prick/subcommand/prick-set.rb'
55
55
  require_relative 'prick/subcommand/prick-setup.rb'
56
+ require_relative 'prick/subcommand/prick-snapshot.rb'
56
57
  require_relative 'prick/subcommand/prick-teardown.rb'
57
58
 
58
59
  module Prick
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: prick
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.35.0
4
+ version: 0.36.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-04-28 00:00:00.000000000 Z
11
+ date: 2024-05-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: semantic
@@ -233,6 +233,7 @@ files:
233
233
  - lib/prick/subcommand/prick-run.rb
234
234
  - lib/prick/subcommand/prick-set.rb
235
235
  - lib/prick/subcommand/prick-setup.rb
236
+ - lib/prick/subcommand/prick-snapshot.rb
236
237
  - lib/prick/subcommand/prick-teardown.rb
237
238
  - lib/prick/subcommand/subcommand.rb
238
239
  - lib/prick/version.rb