db-migrate-x 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a8946c58a0fbd70adc0f1c92b27dcfc001ede4438379d1c90731c061c34ad79d
4
+ data.tar.gz: 804e612a9163f3692b2265b881b806800dbe13eeb2cb3ce6c1c36e0dbab36d09
5
+ SHA512:
6
+ metadata.gz: c62649721fe87d70e29554f15f147bcba2a168b951d28834f340ba42363d7c6984d6b0b4fe0e16c5bbd879d0f3eb2ddeef63e9f91feee77611eadffe9702cc3c
7
+ data.tar.gz: ba5da48eaf8bf907a8e71f3264d71f8372426b67938ab28631185a02314f72d3b64cb57fd11063c27e7d59c10d9a25e925e4d261bfd6fd66f3d861ac32d94ca0
checksums.yaml.gz.sig ADDED
Binary file
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021-2024, by Samuel Williams.
5
+
6
+ require_relative "drop_index"
7
+
8
+ module DB
9
+ module Migrate
10
+ class CreateIndex
11
+ def initialize(name, table, columns = [], unique: false, drop_if_exists: false, if_not_exists: false, method: nil)
12
+ @name = name
13
+ @table = table
14
+ @columns = Array(columns)
15
+
16
+ @unique = unique
17
+ @drop_if_exists = drop_if_exists
18
+ @if_not_exists = if_not_exists
19
+ @method = method
20
+ end
21
+
22
+ def call(session)
23
+ if @drop_if_exists
24
+ DropIndex.new(@name, if_exists: true).call(session)
25
+ end
26
+
27
+ if @unique
28
+ statement = session.clause("CREATE UNIQUE INDEX")
29
+ else
30
+ statement = session.clause("CREATE INDEX")
31
+ end
32
+
33
+ if @if_not_exists
34
+ statement.clause("IF NOT EXISTS")
35
+ end
36
+
37
+ statement.identifier(@name)
38
+
39
+ statement.clause("ON")
40
+ statement.identifier(@table)
41
+
42
+ if @method
43
+ statement.clause("USING")
44
+ statement.identifier(@method)
45
+ end
46
+
47
+ statement.clause("(")
48
+ first = true
49
+ indexes = []
50
+
51
+ @columns.each do |name|
52
+ if first
53
+ first = false
54
+ else
55
+ statement.clause(",")
56
+ end
57
+
58
+ statement.identifier(name)
59
+ end
60
+
61
+ statement.clause(")")
62
+
63
+ Console.logger.info(self, statement)
64
+ statement.call
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,113 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021-2024, by Samuel Williams.
5
+
6
+ require_relative "create_index"
7
+ require_relative "drop_table"
8
+
9
+ module DB
10
+ module Migrate
11
+ class CreateTable
12
+ def initialize(name, drop_if_exists: false, if_not_exists: false)
13
+ @name = name
14
+ @columns = []
15
+ @indexes = []
16
+
17
+ @drop_if_exists = drop_if_exists
18
+ @if_not_exists = if_not_exists
19
+ end
20
+
21
+ def drop_if_exists!
22
+ @drop_if_exists = true
23
+ end
24
+
25
+ def if_not_exists!
26
+ @if_not_exists = true
27
+ end
28
+
29
+ def primary_key(name = :id, **options)
30
+ options[:primary] = true
31
+
32
+ @columns << [name, :key_column, options]
33
+ end
34
+
35
+ def foreign_key(name, index: true, **options)
36
+ options[:primary] = false
37
+
38
+ @columns << [name, :key_column, options]
39
+
40
+ if index
41
+ @indexes << name
42
+ end
43
+ end
44
+
45
+ def column(name, type, index: false, **options)
46
+ @columns << [name, type, options]
47
+
48
+ if index
49
+ @indexes << name
50
+ end
51
+ end
52
+
53
+ def timestamps
54
+ self.column :created_at, "TIMESTAMP"
55
+ self.column :updated_at, "TIMESTAMP"
56
+ end
57
+
58
+ def call(session)
59
+ if @drop_if_exists
60
+ DropTable.new(@name, if_exists: true).call(session)
61
+ end
62
+
63
+ statement = session.clause("CREATE TABLE")
64
+
65
+ if @if_not_exists
66
+ statement.clause("IF NOT EXISTS")
67
+ end
68
+
69
+ statement.identifier(@name)
70
+
71
+ statement.clause("(")
72
+ first = true
73
+
74
+ @columns.each do |name, type, options|
75
+ if first
76
+ first = false
77
+ else
78
+ statement.clause(",")
79
+ end
80
+
81
+ if type == :key_column
82
+ statement.clause(session.connection.key_column(name, **options))
83
+ else
84
+ statement.identifier(name)
85
+ statement.clause(type)
86
+
87
+ if !options[:null]
88
+ statement.clause("NOT NULL")
89
+ end
90
+
91
+ if options.key?(:default)
92
+ statement.clause("DEFAULT").literal(options[:default])
93
+ end
94
+
95
+ if unique = options[:unique]
96
+ statement.clause("UNIQUE")
97
+ end
98
+ end
99
+ end
100
+
101
+ statement.clause(")")
102
+
103
+ Console.logger.info(self, statement)
104
+ statement.call
105
+
106
+ @indexes.each do |column|
107
+ name = "index_#{@name}_on_#{column}"
108
+ CreateIndex.new(name, @name, column, if_not_exists: @if_not_exists).call(session)
109
+ end
110
+ end
111
+ end
112
+ end
113
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021-2024, by Samuel Williams.
5
+
6
+ module DB
7
+ module Migrate
8
+ class DropIndex
9
+ def initialize(name, if_exists: false)
10
+ @name = name
11
+ @if_exists = if_exists
12
+ end
13
+
14
+ def if_exists!
15
+ @if_exists = true
16
+ end
17
+
18
+ def call(session)
19
+ statement = session.clause("DROP INDEX")
20
+
21
+ if @if_exists
22
+ statement.clause("IF EXISTS")
23
+ end
24
+
25
+ statement.identifier(@name)
26
+
27
+ Console.logger.info(self, statement)
28
+ statement.call
29
+ end
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021-2024, by Samuel Williams.
5
+
6
+ require_relative "create_index"
7
+
8
+ module DB
9
+ module Migrate
10
+ class DropTable
11
+ def initialize(name, if_exists: false)
12
+ @name = name
13
+ @if_exists = if_exists
14
+ end
15
+
16
+ def if_exists!
17
+ @if_exists = true
18
+ end
19
+
20
+ def call(session)
21
+ statement = session.clause("DROP TABLE")
22
+
23
+ if @if_exists
24
+ statement.clause("IF EXISTS")
25
+ end
26
+
27
+ statement.identifier(@name)
28
+
29
+ Console.logger.info(self, statement)
30
+ statement.call
31
+ end
32
+ end
33
+ end
34
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021-2024, by Samuel Williams.
5
+
6
+ module DB
7
+ module Migrate
8
+ class InformationSchema
9
+ def initialize(session)
10
+ @session = session
11
+ end
12
+
13
+ TABLES = DB::Identifier[:information_schema, :tables]
14
+
15
+ def table_exist?(name)
16
+ statement = @session.clause("SELECT * FROM")
17
+ statement.identifier(TABLES)
18
+ statement.clause("WHERE")
19
+ statement.identifier(:table_name)
20
+ statement.clause("=")
21
+ statement.literal(name)
22
+
23
+ return statement.call.to_a.any?
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021-2024, by Samuel Williams.
5
+
6
+ require "async"
7
+
8
+ require_relative "create_table"
9
+ require_relative "rename_table"
10
+ require_relative "create_index"
11
+
12
+ module DB
13
+ module Migrate
14
+ class Migration
15
+ def initialize(name, session)
16
+ @name = name
17
+ @session = session
18
+ end
19
+
20
+ def call(&block)
21
+ create_table?(:migration) do
22
+ primary_key
23
+ column :name, "TEXT NOT NULL", unique: true, index: true
24
+ timestamps
25
+ end
26
+
27
+ self.instance_eval(&block)
28
+ end
29
+
30
+ def information_schema
31
+ @information_schema ||= InformationSchema.new(@session)
32
+ end
33
+
34
+ def create_table(name, **options, &block)
35
+ create_table = CreateTable.new(name, **options)
36
+ create_table.instance_eval(&block)
37
+ create_table.call(@session)
38
+ end
39
+
40
+ def create_table?(name, **options, &block)
41
+ options[:if_not_exists] = true
42
+
43
+ create_table = CreateTable.new(name, **options)
44
+ create_table.instance_eval(&block)
45
+ create_table.call(@session)
46
+ end
47
+
48
+ def rename_table(name, new_name)
49
+ rename_table = RenameTable.new(name, new_name)
50
+ rename_table.call(@session)
51
+ end
52
+
53
+ def create_index(...)
54
+ create_index = CreateIndex.new(...)
55
+ create_index.call(@session)
56
+ end
57
+
58
+ def drop_table(name, if_exists: false)
59
+ drop_table = DropTable.new(name, if_exists: if_exists)
60
+ drop_table.call(@session)
61
+ end
62
+ end
63
+
64
+ def self.migrate(name, client, &block)
65
+ Sync do
66
+ client.transaction do |session|
67
+ Migration.new(name, session).call(&block)
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021-2024, by Samuel Williams.
5
+
6
+ require_relative "drop_index"
7
+
8
+ module DB
9
+ module Migrate
10
+ class RenameTable
11
+ def initialize(name, new_name)
12
+ @name = name
13
+ @new_name = new_name
14
+ end
15
+
16
+ def call(session)
17
+ statement = session.clause("ALTER TABLE")
18
+ statement.identifier(@name)
19
+ statement.clause("RENAME TO")
20
+ statement.identifier(@new_name)
21
+
22
+ Console.logger.info(self, statement)
23
+ statement.call
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021-2024, by Samuel Williams.
5
+
6
+ module DB
7
+ module Migrate
8
+ VERSION = "0.2.0"
9
+ end
10
+ end
data/lib/db/migrate.rb ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Released under the MIT License.
4
+ # Copyright, 2021-2024, by Samuel Williams.
5
+
6
+ require_relative "migrate/version"
7
+ require_relative "migrate/migration"
8
+ require_relative "migrate/information_schema"
data/license.md ADDED
@@ -0,0 +1,21 @@
1
+ # MIT License
2
+
3
+ Copyright, 2021-2024, by Samuel Williams.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/readme.md ADDED
@@ -0,0 +1,27 @@
1
+ # DB::Migrate
2
+
3
+ Provides convenient abstractions for creating tables, indexes and general database migrations.
4
+
5
+ [![Development Status](https://github.com/socketry/db-migrate/workflows/Test/badge.svg)](https://github.com/socketry/db-migrate/actions?workflow=Test)
6
+
7
+ ## Usage
8
+
9
+ Please see the [project documentation](https://socketry.github.io/db-migrate).
10
+
11
+ ## Contributing
12
+
13
+ We welcome contributions to this project.
14
+
15
+ 1. Fork it.
16
+ 2. Create your feature branch (`git checkout -b my-new-feature`).
17
+ 3. Commit your changes (`git commit -am 'Add some feature'`).
18
+ 4. Push to the branch (`git push origin my-new-feature`).
19
+ 5. Create new Pull Request.
20
+
21
+ ### Developer Certificate of Origin
22
+
23
+ In order to protect users of this project, we require all contributors to comply with the [Developer Certificate of Origin](https://developercertificate.org/). This ensures that all contributions are properly licensed and attributed.
24
+
25
+ ### Community Guidelines
26
+
27
+ This project is best served by a collaborative and respectful environment. Treat each other professionally, respect differing viewpoints, and engage constructively. Harassment, discrimination, or harmful behavior is not tolerated. Communicate clearly, listen actively, and support one another. If any issues arise, please inform the project maintainers.
data.tar.gz.sig ADDED
Binary file
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: db-migrate-x
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.0
5
+ platform: ruby
6
+ authors:
7
+ - Samuel Williams
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain:
11
+ - |
12
+ -----BEGIN CERTIFICATE-----
13
+ MIIE2DCCA0CgAwIBAgIBATANBgkqhkiG9w0BAQsFADBhMRgwFgYDVQQDDA9zYW11
14
+ ZWwud2lsbGlhbXMxHTAbBgoJkiaJk/IsZAEZFg1vcmlvbnRyYW5zZmVyMRIwEAYK
15
+ CZImiZPyLGQBGRYCY28xEjAQBgoJkiaJk/IsZAEZFgJuejAeFw0yMjA4MDYwNDUz
16
+ MjRaFw0zMjA4MDMwNDUzMjRaMGExGDAWBgNVBAMMD3NhbXVlbC53aWxsaWFtczEd
17
+ MBsGCgmSJomT8ixkARkWDW9yaW9udHJhbnNmZXIxEjAQBgoJkiaJk/IsZAEZFgJj
18
+ bzESMBAGCgmSJomT8ixkARkWAm56MIIBojANBgkqhkiG9w0BAQEFAAOCAY8AMIIB
19
+ igKCAYEAomvSopQXQ24+9DBB6I6jxRI2auu3VVb4nOjmmHq7XWM4u3HL+pni63X2
20
+ 9qZdoq9xt7H+RPbwL28LDpDNflYQXoOhoVhQ37Pjn9YDjl8/4/9xa9+NUpl9XDIW
21
+ sGkaOY0eqsQm1pEWkHJr3zn/fxoKPZPfaJOglovdxf7dgsHz67Xgd/ka+Wo1YqoE
22
+ e5AUKRwUuvaUaumAKgPH+4E4oiLXI4T1Ff5Q7xxv6yXvHuYtlMHhYfgNn8iiW8WN
23
+ XibYXPNP7NtieSQqwR/xM6IRSoyXKuS+ZNGDPUUGk8RoiV/xvVN4LrVm9upSc0ss
24
+ RZ6qwOQmXCo/lLcDUxJAgG95cPw//sI00tZan75VgsGzSWAOdjQpFM0l4dxvKwHn
25
+ tUeT3ZsAgt0JnGqNm2Bkz81kG4A2hSyFZTFA8vZGhp+hz+8Q573tAR89y9YJBdYM
26
+ zp0FM4zwMNEUwgfRzv1tEVVUEXmoFCyhzonUUw4nE4CFu/sE3ffhjKcXcY//qiSW
27
+ xm4erY3XAgMBAAGjgZowgZcwCQYDVR0TBAIwADALBgNVHQ8EBAMCBLAwHQYDVR0O
28
+ BBYEFO9t7XWuFf2SKLmuijgqR4sGDlRsMC4GA1UdEQQnMCWBI3NhbXVlbC53aWxs
29
+ aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MC4GA1UdEgQnMCWBI3NhbXVlbC53aWxs
30
+ aWFtc0BvcmlvbnRyYW5zZmVyLmNvLm56MA0GCSqGSIb3DQEBCwUAA4IBgQB5sxkE
31
+ cBsSYwK6fYpM+hA5B5yZY2+L0Z+27jF1pWGgbhPH8/FjjBLVn+VFok3CDpRqwXCl
32
+ xCO40JEkKdznNy2avOMra6PFiQyOE74kCtv7P+Fdc+FhgqI5lMon6tt9rNeXmnW/
33
+ c1NaMRdxy999hmRGzUSFjozcCwxpy/LwabxtdXwXgSay4mQ32EDjqR1TixS1+smp
34
+ 8C/NCWgpIfzpHGJsjvmH2wAfKtTTqB9CVKLCWEnCHyCaRVuKkrKjqhYCdmMBqCws
35
+ JkxfQWC+jBVeG9ZtPhQgZpfhvh+6hMhraUYRQ6XGyvBqEUe+yo6DKIT3MtGE2+CP
36
+ eX9i9ZWBydWb8/rvmwmX2kkcBbX0hZS1rcR593hGc61JR6lvkGYQ2MYskBveyaxt
37
+ Q2K9NVun/S785AP05vKkXZEFYxqG6EW012U4oLcFl5MySFajYXRYbuUpH6AY+HP8
38
+ voD0MPg1DssDLKwXyt1eKD/+Fq0bFWhwVM/1XiAXL7lyYUyOq24KHgQ2Csg=
39
+ -----END CERTIFICATE-----
40
+ date: 2024-09-21 00:00:00.000000000 Z
41
+ dependencies:
42
+ - !ruby/object:Gem::Dependency
43
+ name: db
44
+ requirement: !ruby/object:Gem::Requirement
45
+ requirements:
46
+ - - ">="
47
+ - !ruby/object:Gem::Version
48
+ version: '0'
49
+ type: :runtime
50
+ prerelease: false
51
+ version_requirements: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '0'
56
+ - !ruby/object:Gem::Dependency
57
+ name: migrate
58
+ requirement: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - "~>"
61
+ - !ruby/object:Gem::Version
62
+ version: '0.3'
63
+ type: :runtime
64
+ prerelease: false
65
+ version_requirements: !ruby/object:Gem::Requirement
66
+ requirements:
67
+ - - "~>"
68
+ - !ruby/object:Gem::Version
69
+ version: '0.3'
70
+ description:
71
+ email:
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - lib/db/migrate.rb
77
+ - lib/db/migrate/create_index.rb
78
+ - lib/db/migrate/create_table.rb
79
+ - lib/db/migrate/drop_index.rb
80
+ - lib/db/migrate/drop_table.rb
81
+ - lib/db/migrate/information_schema.rb
82
+ - lib/db/migrate/migration.rb
83
+ - lib/db/migrate/rename_table.rb
84
+ - lib/db/migrate/version.rb
85
+ - license.md
86
+ - readme.md
87
+ homepage: https://github.com/socketry/db-migrate
88
+ licenses:
89
+ - MIT
90
+ metadata:
91
+ documentation_uri: https://socketry.github.io/db-migrate/
92
+ funding_uri: https://github.com/sponsors/ioquatix/
93
+ source_code_uri: https://github.com/socketry/db-migrate.git
94
+ post_install_message:
95
+ rdoc_options: []
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !ruby/object:Gem::Requirement
99
+ requirements:
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: '3.1'
103
+ required_rubygems_version: !ruby/object:Gem::Requirement
104
+ requirements:
105
+ - - ">="
106
+ - !ruby/object:Gem::Version
107
+ version: '0'
108
+ requirements: []
109
+ rubygems_version: 3.5.11
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Database migrations.
113
+ test_files: []
metadata.gz.sig ADDED
Binary file