watermelon_db_sync 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/README.md +121 -0
- data/Rakefile +4 -0
- data/lib/generators/watermelon_db_sync/add_sync_fields/add_sync_fields_generator.rb +32 -0
- data/lib/generators/watermelon_db_sync/install/install_generator.rb +26 -0
- data/lib/generators/watermelon_db_sync/templates/add_sync_fields_migration.rb.erb +12 -0
- data/lib/generators/watermelon_db_sync/templates/create_sequence.rb +5 -0
- data/lib/generators/watermelon_db_sync/templates/initializer.rb +4 -0
- data/lib/watermelon_db_sync/configuration.rb +9 -0
- data/lib/watermelon_db_sync/model_sync_callback.rb +30 -0
- data/lib/watermelon_db_sync/sync.rb +13 -0
- data/lib/watermelon_db_sync/sync_pull.rb +69 -0
- data/lib/watermelon_db_sync/sync_push.rb +110 -0
- data/lib/watermelon_db_sync/version.rb +5 -0
- data/lib/watermelon_db_sync.rb +28 -0
- data/sig/watermelon_db_sync.rbs +4 -0
- metadata +132 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 9b9015f5a02d5c6b6e26d8a0052fb9f051ea6c8117809d31eb3b47c3e71a2534
|
4
|
+
data.tar.gz: 9dfd41be7553e2ab5ba5e6446b7cfabcd7b3d0d20a191fbd0c8f8737dd50dcce
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 3834629e0e18f18fd6013c4f00758187638eaf2e83e3611b9ab5a8e025143d4ca85cfafb527708a6f3f5a4aca9b9b4b3d7a507d21d0db4633f9b5aa4676d7775
|
7
|
+
data.tar.gz: 07621474f58f843f13bb37d12b7ae05a63defd525f70ebca2aada8839fb1e36e691f35aba7320eec70e40227ca58fed200e3d9581ac727d0dbbd2788610d584a
|
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at yasfi.mail@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
|
2
|
+
# WatermelonDbSync
|
3
|
+
|
4
|
+
Make synchronize pull & push easier & faster.
|
5
|
+
|
6
|
+
This gem is built to handle synchronize between pull & push with [WatermelonDb](https://github.com/Nozbe/WatermelonDB) even you are use different library and keep the synchronization throguh API then you can use this gem, basically this gem follows this article to handle the sync [How to Build WatermelonDB Sync Backend in Elixir](https://fahri.id/posts/how-to-build-watermelondb-sync-backend-in-elixir) which is using Auto-incrementing Counter (Version) + Timestamp for Tracking Changes approach, but this is a Ruby version of it
|
7
|
+
|
8
|
+
## Features
|
9
|
+
|
10
|
+
- Seamless sync between WatermelonDB and Rails through API endpoint.
|
11
|
+
- Customizable sync strategies.
|
12
|
+
- Easy integration with existing Rails applications.
|
13
|
+
- Supported Postgresql
|
14
|
+
|
15
|
+
|
16
|
+
# WatermelonDB Sync
|
17
|
+
|
18
|
+
## Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem 'watermelon_db_sync'
|
24
|
+
```
|
25
|
+
|
26
|
+
You can find the gem on [RubyGems](https://rubygems.org/gems/watermelon_db_sync).
|
27
|
+
|
28
|
+
After updating the Gemfile, run:
|
29
|
+
|
30
|
+
```bash
|
31
|
+
$ bundle install
|
32
|
+
```
|
33
|
+
|
34
|
+
Or, install the gem manually using:
|
35
|
+
|
36
|
+
```bash
|
37
|
+
$ gem install watermelon_db_sync
|
38
|
+
```
|
39
|
+
|
40
|
+
## Getting Started
|
41
|
+
|
42
|
+
To set up the gem, you need to run the installation generator:
|
43
|
+
|
44
|
+
```bash
|
45
|
+
rails g watermelon_db_sync:install
|
46
|
+
```
|
47
|
+
|
48
|
+
This will:
|
49
|
+
- Create an initializer at `config/initializers/watermelon_db_sync.rb` where all configuration options are described.
|
50
|
+
- Generate a migration to create a global sequence table: `db/migrate/[timestamp]_create_sequence.rb`.
|
51
|
+
|
52
|
+
### Adding Sync Fields to a Model
|
53
|
+
|
54
|
+
Once you have the basic setup, you can add sync fields to an existing model. This assumes that the table (e.g., `orders`) already exists in your database. To add sync fields, run:
|
55
|
+
|
56
|
+
```bash
|
57
|
+
rails g watermelon_db_sync:add_sync_fields Order
|
58
|
+
```
|
59
|
+
|
60
|
+
This will generate a migration that adds the necessary sync fields to the `orders` table.
|
61
|
+
|
62
|
+
Next run:
|
63
|
+
|
64
|
+
```bash
|
65
|
+
rails db:migrate
|
66
|
+
```
|
67
|
+
|
68
|
+
## Usage
|
69
|
+
```bash
|
70
|
+
Pull:
|
71
|
+
|
72
|
+
sync_pull = WatermelonDbSync::SyncPull.new(last_pulled_version: 0)
|
73
|
+
sync_pull.pull
|
74
|
+
sync_pull.data[:response]
|
75
|
+
|
76
|
+
response:
|
77
|
+
|
78
|
+
{:orders=>
|
79
|
+
{:created=>
|
80
|
+
[{"id"=>1,
|
81
|
+
"name"=>"asdasad",
|
82
|
+
"phone"=>nil,
|
83
|
+
"created_at"=>"2024-09-11T11:31:07.867Z",
|
84
|
+
"updated_at"=>"2024-09-11T11:43:06.214Z",
|
85
|
+
"version"=>1,
|
86
|
+
"version_created"=>2,
|
87
|
+
"created_at_server"=>"2024-09-11T11:31:07.869Z",
|
88
|
+
"updated_at_server"=>"2024-09-11T11:31:07.869Z",
|
89
|
+
"deleted_at_server"=>nil,
|
90
|
+
"push_id"=>nil}],
|
91
|
+
:deleted=>[],
|
92
|
+
:updated=>[]}}
|
93
|
+
|
94
|
+
Push:
|
95
|
+
data={
|
96
|
+
"orders": {
|
97
|
+
"created": [
|
98
|
+
{
|
99
|
+
"id": "1",
|
100
|
+
"name": "Cust 1"
|
101
|
+
}
|
102
|
+
],
|
103
|
+
"updated": [
|
104
|
+
{
|
105
|
+
"id": "1",
|
106
|
+
"name": "Cust Edit"
|
107
|
+
}
|
108
|
+
],
|
109
|
+
"deleted": []
|
110
|
+
}
|
111
|
+
}
|
112
|
+
sync_push = WatermelonDbSync::SyncPush.new(data)
|
113
|
+
sync_push.push
|
114
|
+
sync_push.data[:success]
|
115
|
+
=> true
|
116
|
+
```
|
117
|
+
|
118
|
+
|
119
|
+
## Contributing
|
120
|
+
|
121
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/aapiw/watermelon_db_sync. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/aapiw/watermelon_db_sync/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module WatermelonDbSync::Generators
|
2
|
+
class AddSyncFieldsGenerator < Rails::Generators::Base
|
3
|
+
include Rails::Generators::Migration
|
4
|
+
|
5
|
+
# Specify the source for the migration template
|
6
|
+
source_root File.expand_path('../templates', __dir__)
|
7
|
+
|
8
|
+
# Take the model name as an argument
|
9
|
+
argument :model_name, type: :string
|
10
|
+
|
11
|
+
def copy_migrations
|
12
|
+
# Use the helper methods directly to set the migration file name and path
|
13
|
+
migration_template "add_sync_fields_migration.rb.erb", "db/migrate/#{migration_file_name}.rb"
|
14
|
+
end
|
15
|
+
|
16
|
+
# Generates the next migration number with a timestamp
|
17
|
+
def self.next_migration_number(dirname)
|
18
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
19
|
+
end
|
20
|
+
|
21
|
+
# Generates the migration file name based on the model name
|
22
|
+
def migration_file_name
|
23
|
+
"add_sync_fields_to_#{model_name.tableize}"
|
24
|
+
end
|
25
|
+
|
26
|
+
# Generates the migration class name to use in the template
|
27
|
+
def migration_class_name
|
28
|
+
"AddSyncFieldsTo#{model_name.tableize.camelize}"
|
29
|
+
end
|
30
|
+
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module WatermelonDbSync::Generators
|
2
|
+
class InstallGenerator < Rails::Generators::Base
|
3
|
+
|
4
|
+
include Rails::Generators::Migration
|
5
|
+
|
6
|
+
source_root File.expand_path('../templates', __dir__)
|
7
|
+
|
8
|
+
def copy_initializer
|
9
|
+
template "initializer.rb", "config/initializers/watermelon_db_sync.rb"
|
10
|
+
end
|
11
|
+
|
12
|
+
def copy_migrations
|
13
|
+
migration_template "create_sequence.rb", "db/migrate/create_global_seqs.rb"
|
14
|
+
end
|
15
|
+
|
16
|
+
def self.next_migration_number(dirname)
|
17
|
+
InstallGenerator.timestamp
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.timestamp
|
21
|
+
Time.now.utc.strftime("%Y%m%d%H%M%S")
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
26
|
+
2
|
@@ -0,0 +1,12 @@
|
|
1
|
+
class <%= migration_class_name %> < ActiveRecord::Migration[6.0]
|
2
|
+
def change
|
3
|
+
add_column :<%= model_name.tableize %>, :version, :bigint, default: -> { "nextval('global_seqs')" }
|
4
|
+
add_column :<%= model_name.tableize %>, :version_created, :bigint, default: -> { "nextval('global_seqs')" }
|
5
|
+
add_column :<%= model_name.tableize %>, :created_at_server, :datetime, default: -> { 'CURRENT_TIMESTAMP' }
|
6
|
+
add_column :<%= model_name.tableize %>, :updated_at_server, :datetime, default: -> { 'CURRENT_TIMESTAMP' }
|
7
|
+
add_column :<%= model_name.tableize %>, :deleted_at_server, :datetime
|
8
|
+
add_column :<%= model_name.tableize %>, :push_id, :integer
|
9
|
+
|
10
|
+
add_index :<%= model_name.tableize %>, :deleted_at_server
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
# lib/watermelon_db_sync/model_sync.rb
|
2
|
+
module WatermelonDbSync
|
3
|
+
module ModelSyncCallback
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
# Code to run when the module is included in a model
|
8
|
+
acts_as_paranoid column: 'deleted_at_server'
|
9
|
+
|
10
|
+
before_update :update_version_and_updated_at_server
|
11
|
+
before_destroy :update_version
|
12
|
+
end
|
13
|
+
|
14
|
+
# handle updating version when record is updated
|
15
|
+
def update_version_and_updated_at_server
|
16
|
+
return unless WatermelonDbSync.configuration.sync_models.include? self.class.name
|
17
|
+
self.version = Sync.next_global_seqs
|
18
|
+
self.updated_at_server = Time.zone.now
|
19
|
+
end
|
20
|
+
|
21
|
+
# handle updating version when record is deleted
|
22
|
+
def update_version
|
23
|
+
return unless WatermelonDbSync.configuration.sync_models.include? self.class.name
|
24
|
+
update_column(:version, Sync.next_global_seqs) #skip callback
|
25
|
+
end
|
26
|
+
|
27
|
+
class_methods do
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
module WatermelonDbSync
|
2
|
+
class Sync
|
3
|
+
class << self
|
4
|
+
def last_global_seqs
|
5
|
+
ActiveRecord::Base.connection.execute("select last_value from global_seqs").first["last_value"]
|
6
|
+
end
|
7
|
+
|
8
|
+
def next_global_seqs
|
9
|
+
ActiveRecord::Base.connection.select_value("SELECT nextval('global_seqs')")
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
module WatermelonDbSync
|
2
|
+
class SyncPull < Sync
|
3
|
+
|
4
|
+
attr_accessor :last_pulled_version, :models, :data
|
5
|
+
|
6
|
+
def initialize(params)
|
7
|
+
@last_pulled_version = params[:last_pulled_version] || 0
|
8
|
+
@push_id = params[:push_id]
|
9
|
+
@format_response = {created: [], updated: [], deleted: [] }
|
10
|
+
@models = WatermelonDbSync.configuration.sync_models
|
11
|
+
@data = {last_global_seqs: 0, response: build_default_response}
|
12
|
+
end
|
13
|
+
|
14
|
+
def pull
|
15
|
+
self.get_from_all
|
16
|
+
end
|
17
|
+
|
18
|
+
def get_from_all
|
19
|
+
begin
|
20
|
+
max_list = []
|
21
|
+
@models.each do |model|
|
22
|
+
max_list << self.query(eval(model))
|
23
|
+
end
|
24
|
+
|
25
|
+
@data[:last_global_seqs] = max_list.flatten.max || @last_pulled_version.to_i #Sync.last_global_seqs
|
26
|
+
return true
|
27
|
+
rescue => e
|
28
|
+
e.message
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
# Filtered by push_id, for differentiate record just pushed
|
33
|
+
def query(model=nil)
|
34
|
+
|
35
|
+
all_with_deleted = model.with_deleted.where("version_created > #{@last_pulled_version} OR version > #{@last_pulled_version}")
|
36
|
+
max_list = all_with_deleted.pluck(:version, :version_created)
|
37
|
+
|
38
|
+
all_with_deleted = all_with_deleted.where("push_id != ? or push_id is ?", @push_id, nil) if @push_id.present?
|
39
|
+
|
40
|
+
created = all_with_deleted.where(deleted_at_server: nil).where("version_created > #{@last_pulled_version}")
|
41
|
+
|
42
|
+
updated = all_with_deleted.where(deleted_at_server: nil).where("created_at_server != updated_at_server")
|
43
|
+
new_updated = []
|
44
|
+
updated.each do |obj_updated|
|
45
|
+
new_updated << obj_updated unless created.map{|d|d.as_json}.include?(obj_updated.as_json)
|
46
|
+
end
|
47
|
+
|
48
|
+
deleted = all_with_deleted.only_deleted
|
49
|
+
deleted = deleted.pluck(:id)
|
50
|
+
|
51
|
+
@data[:response][model.table_name.to_sym][:created] = created&.as_json
|
52
|
+
@data[:response][model.table_name.to_sym][:updated] = new_updated&.as_json
|
53
|
+
@data[:response][model.table_name.to_sym][:deleted] = deleted&.as_json
|
54
|
+
|
55
|
+
max_list
|
56
|
+
end
|
57
|
+
|
58
|
+
def build_default_response
|
59
|
+
default = Hash.new { |hash, key| hash[key] = {} }
|
60
|
+
@models.each do |model|
|
61
|
+
default[eval(model).table_name.to_sym][:created] = {}
|
62
|
+
default[eval(model).table_name.to_sym][:deleted] = {}
|
63
|
+
default[eval(model).table_name.to_sym][:updated] = {}
|
64
|
+
end
|
65
|
+
default
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,110 @@
|
|
1
|
+
module WatermelonDbSync
|
2
|
+
class SyncPush < Sync
|
3
|
+
|
4
|
+
attr_accessor :models, :data, :params
|
5
|
+
|
6
|
+
def initialize(params)
|
7
|
+
@models = WatermelonDbSync.configuration.sync_models
|
8
|
+
@params = params
|
9
|
+
@data = {last_global_seqs:0}
|
10
|
+
@push_id = rand(1..1_000_000_000)
|
11
|
+
end
|
12
|
+
|
13
|
+
def push
|
14
|
+
begin
|
15
|
+
if has_conflict_version?
|
16
|
+
@data[:error_code] = "WDBS2"
|
17
|
+
raise StandardError.new("the data has a conflict version, please pull first")
|
18
|
+
else
|
19
|
+
ActiveRecord::Base.transaction do
|
20
|
+
@models.each do |model|
|
21
|
+
self.submit_records!(model) if @params.keys.include? eval(model).table_name
|
22
|
+
end
|
23
|
+
@data[:success] = true
|
24
|
+
sync_pull = SyncPull.new(last_pulled_version: @params["last_pulled_version"], push_id: @push_id)
|
25
|
+
sync_pull.pull
|
26
|
+
@data[:response] = sync_pull.data[:response]
|
27
|
+
@data[:last_global_seqs] = sync_pull.data[:last_global_seqs]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
rescue StandardError => e
|
31
|
+
@data[:success] = false
|
32
|
+
@data[:message] = e.message
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
|
37
|
+
def submit_records!(model)
|
38
|
+
table = @params[model.tableize]
|
39
|
+
table["created"]&.each do |data|
|
40
|
+
data = except_data(data)
|
41
|
+
data["push_id"] = @push_id
|
42
|
+
|
43
|
+
find_or_new = eval(model).find(data["id"]) rescue nil
|
44
|
+
if find_or_new.blank?
|
45
|
+
find_or_new = eval(model).new(data)
|
46
|
+
find_or_new.id = data["id"]
|
47
|
+
find_or_new.created_at = data["created_at"]
|
48
|
+
find_or_new.updated_at = data["updated_at"]
|
49
|
+
find_or_new.save!
|
50
|
+
else
|
51
|
+
find_or_new.update!(data)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
table["updated"]&.each do |data|
|
56
|
+
data = except_data(data)
|
57
|
+
data["push_id"] = @push_id
|
58
|
+
|
59
|
+
find_record = eval(model).find(data["id"]) rescue nil
|
60
|
+
|
61
|
+
if find_record.nil? && eval(model).only_deleted.find(data["id"])
|
62
|
+
@data[:error_code] = "WDBS1"
|
63
|
+
raise StandardError.new("id=#{data["id"]} of the #{model} was deleted, please pull first")
|
64
|
+
end
|
65
|
+
find_record.update!(data.except("id"))
|
66
|
+
end
|
67
|
+
|
68
|
+
table["deleted"]&.each do |data|
|
69
|
+
find_record = eval(model).find(data) rescue nil
|
70
|
+
|
71
|
+
if find_record.present?
|
72
|
+
find_record.update_column(:push_id, @push_id)
|
73
|
+
find_record.destroy
|
74
|
+
end
|
75
|
+
end
|
76
|
+
|
77
|
+
end
|
78
|
+
|
79
|
+
def except_data(data)
|
80
|
+
data.except("version", "version_created", "created_at_server", "updated_at_server", "deleted_at_server", "push_id", "_status", "_changed")
|
81
|
+
end
|
82
|
+
|
83
|
+
def has_conflict_version?
|
84
|
+
conflict = false
|
85
|
+
@models.each do |model|
|
86
|
+
|
87
|
+
#keep original object @params
|
88
|
+
params = Marshal.load(Marshal.dump(@params))
|
89
|
+
table = params[model.tableize]
|
90
|
+
if (params.keys.include? eval(model).table_name)
|
91
|
+
collect_ids = table["created"]
|
92
|
+
|
93
|
+
if table["updated"].present?
|
94
|
+
updated = table["updated"]
|
95
|
+
collect_ids.concat(updated)
|
96
|
+
end
|
97
|
+
|
98
|
+
collect_ids = collect_ids&.map{|d|d["id"]}
|
99
|
+
collect_ids.concat(table["deleted"]) if table["deleted"].present?
|
100
|
+
conflict = eval(model).with_deleted
|
101
|
+
.where(id: collect_ids)
|
102
|
+
.where("version_created > #{@params["last_pulled_version"]} OR version > #{@params["last_pulled_version"]}").exists?
|
103
|
+
end
|
104
|
+
break if conflict
|
105
|
+
end
|
106
|
+
return conflict
|
107
|
+
end
|
108
|
+
|
109
|
+
end
|
110
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require 'acts_as_paranoid'
|
3
|
+
|
4
|
+
require_relative "watermelon_db_sync/version"
|
5
|
+
require_relative "watermelon_db_sync/configuration"
|
6
|
+
require_relative "watermelon_db_sync/model_sync_callback"
|
7
|
+
require_relative "watermelon_db_sync/sync"
|
8
|
+
require_relative "watermelon_db_sync/sync_pull"
|
9
|
+
require_relative "watermelon_db_sync/sync_push"
|
10
|
+
|
11
|
+
module WatermelonDbSync
|
12
|
+
class Error < StandardError; end
|
13
|
+
|
14
|
+
# @configuration = Configuration.new
|
15
|
+
|
16
|
+
class << self
|
17
|
+
attr_accessor :configuration
|
18
|
+
|
19
|
+
# Lazily initialize the configuration object
|
20
|
+
def configuration
|
21
|
+
@configuration ||= Configuration.new
|
22
|
+
end
|
23
|
+
|
24
|
+
def configure
|
25
|
+
yield(configuration)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
metadata
ADDED
@@ -0,0 +1,132 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: watermelon_db_sync
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yasfi
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2024-09-11 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rspec
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.2'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '3.2'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 7.0.8
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 7.0.8
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: thor
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: debug
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: acts_as_paranoid
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.6.0
|
76
|
+
type: :runtime
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.6.0
|
83
|
+
description: Make synchronize pull & push easier & faster.
|
84
|
+
email:
|
85
|
+
- yasfi.fauzie@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- CHANGELOG.md
|
91
|
+
- CODE_OF_CONDUCT.md
|
92
|
+
- README.md
|
93
|
+
- Rakefile
|
94
|
+
- lib/generators/watermelon_db_sync/add_sync_fields/add_sync_fields_generator.rb
|
95
|
+
- lib/generators/watermelon_db_sync/install/install_generator.rb
|
96
|
+
- lib/generators/watermelon_db_sync/templates/add_sync_fields_migration.rb.erb
|
97
|
+
- lib/generators/watermelon_db_sync/templates/create_sequence.rb
|
98
|
+
- lib/generators/watermelon_db_sync/templates/initializer.rb
|
99
|
+
- lib/watermelon_db_sync.rb
|
100
|
+
- lib/watermelon_db_sync/configuration.rb
|
101
|
+
- lib/watermelon_db_sync/model_sync_callback.rb
|
102
|
+
- lib/watermelon_db_sync/sync.rb
|
103
|
+
- lib/watermelon_db_sync/sync_pull.rb
|
104
|
+
- lib/watermelon_db_sync/sync_push.rb
|
105
|
+
- lib/watermelon_db_sync/version.rb
|
106
|
+
- sig/watermelon_db_sync.rbs
|
107
|
+
homepage: https://github.com/aapiw/watermelon_db_sync
|
108
|
+
licenses: []
|
109
|
+
metadata:
|
110
|
+
homepage_uri: https://github.com/aapiw/watermelon_db_sync
|
111
|
+
source_code_uri: https://github.com/aapiw/watermelon_db_sync
|
112
|
+
changelog_uri: https://github.com/aapiw/watermelon_db_sync
|
113
|
+
post_install_message:
|
114
|
+
rdoc_options: []
|
115
|
+
require_paths:
|
116
|
+
- lib
|
117
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
118
|
+
requirements:
|
119
|
+
- - ">="
|
120
|
+
- !ruby/object:Gem::Version
|
121
|
+
version: 3.0.0
|
122
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
123
|
+
requirements:
|
124
|
+
- - ">="
|
125
|
+
- !ruby/object:Gem::Version
|
126
|
+
version: '0'
|
127
|
+
requirements: []
|
128
|
+
rubygems_version: 3.3.7
|
129
|
+
signing_key:
|
130
|
+
specification_version: 4
|
131
|
+
summary: Make synchronize pull & push easier & faster.
|
132
|
+
test_files: []
|