Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,29 @@ jobs:
bundler-cache: true
- run: bundle exec standardrb --format github

test:
name: Tests (Spectacles)
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby-version: [2.7, head, jruby-head]
continue-on-error: ${{ endsWith(matrix.ruby-version, 'head') || matrix.ruby-version == 'debug' }}
steps:
- uses: actions/checkout@v4
- uses: ruby/setup-ruby@v1
with:
ruby-version: ${{ matrix.ruby-version }}
bundler-cache: true
- run: bundle exec rake test:spectacles

test-mysql:
name: Tests (MySQL)
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
ruby-version: [2.7, jruby-9.4, head, jruby-head]
ruby-version: [2.7, head, jruby-head]
continue-on-error: ${{ endsWith(matrix.ruby-version, 'head') || matrix.ruby-version == 'debug' }}

services:
Expand Down Expand Up @@ -60,7 +76,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby-version: [2.7, jruby-9.4, head, jruby-head]
ruby-version: [2.7, head, jruby-head]
continue-on-error: ${{ endsWith(matrix.ruby-version, 'head') || matrix.ruby-version == 'debug' }}

services:
Expand Down Expand Up @@ -101,7 +117,7 @@ jobs:
strategy:
fail-fast: false
matrix:
ruby-version: [2.7, jruby-9.4, head, jruby-head]
ruby-version: [2.7, head, jruby-head]
continue-on-error: ${{ endsWith(matrix.ruby-version, 'head') || matrix.ruby-version == 'debug' }}

env:
Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@
/doc/
/pkg/
/spec/reports/
/test/*.db
/test/*.db*
/tmp/
15 changes: 12 additions & 3 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,18 @@ source "https://rubygems.org"
gemspec

platforms :jruby do
gem "activerecord-jdbcmysql-adapter"
gem "activerecord-jdbcpostgresql-adapter"
gem "activerecord-jdbcsqlite3-adapter"
gem "activerecord-jdbc-adapter",
git: "https://github.com/jruby/activerecord-jdbc-adapter",
glob: "activerecord-jdbc-adapter.gemspec"
gem "activerecord-jdbcsqlite3-adapter",
git: "https://github.com/jruby/activerecord-jdbc-adapter",
glob: "activerecord-jdbcsqlite3-adapter/activerecord-jdbcsqlite3-adapter.gemspec"
gem "activerecord-jdbcpostgresql-adapter",
git: "https://github.com/jruby/activerecord-jdbc-adapter",
glob: "activerecord-jdbcpostgresql-adapter/activerecord-jdbcpostgresql-adapter.gemspec"
gem "activerecord-jdbcmysql-adapter",
git: "https://github.com/jruby/activerecord-jdbc-adapter",
glob: "activerecord-jdbcmysql-adapter/activerecord-jdbcmysql-adapter.gemspec"
end

platforms :ruby do
Expand Down
28 changes: 4 additions & 24 deletions lib/spectacles/schema_statements/mysql2_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,14 @@ module SchemaStatements
module Mysql2Adapter
include Spectacles::SchemaStatements::AbstractAdapter

# overrides the #tables method from ActiveRecord's MysqlAdapter
# to return only tables, and not views.
def tables(name = nil, database = nil, like = nil)
database = database ? quote_table_name(database) : "DATABASE()"
by_name = like ? "AND table_name LIKE #{quote(like)}" : ""

sql = <<-SQL.squish
SELECT table_name, table_type
FROM information_schema.tables
WHERE table_schema = #{database}
AND table_type = 'BASE TABLE'
#{by_name}
SQL

execute_and_free(sql, "SCHEMA") do |result|
rows_from(result).map(&:first)
end
end

def views(name = nil) # :nodoc:
result = execute("SHOW FULL TABLES WHERE TABLE_TYPE='VIEW'")

rows_from(result).map(&:first)
values_from(result).map(&:first)
end

def view_build_query(view, name = nil)
result = execute("SHOW CREATE VIEW #{view}", name)
algorithm_string = rows_from(result).first[1]
algorithm_string = values_from(result).first[1]

algorithm_string.gsub(/CREATE .*? (AS)+/i, "")
rescue ActiveRecord::StatementInvalid => e
Expand All @@ -39,8 +19,8 @@ def view_build_query(view, name = nil)

private

def rows_from(result)
result.respond_to?(:rows) ? result.rows : result
def values_from(result)
result.first.respond_to?(:values) ? result.map(&:values) : result
end
end
end
Expand Down
15 changes: 0 additions & 15 deletions lib/spectacles/schema_statements/sqlite3_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,6 @@ module SchemaStatements
module SQLite3Adapter
include Spectacles::SchemaStatements::AbstractAdapter

# overrides the #tables method from ActiveRecord's SQLite3Adapter
# to return only tables, and not views.
def tables(name = nil, table_name = nil)
sql = <<-SQL
SELECT name
FROM sqlite_master
WHERE type = 'table' AND NOT name = 'sqlite_sequence'
SQL
sql << " AND name = #{quote_table_name(table_name)}" if table_name

exec_query(sql, "SCHEMA").map do |row|
row["name"]
end
end

def generate_view_query(*columns)
<<-SQL
SELECT #{columns.join(",")}
Expand Down
4 changes: 2 additions & 2 deletions spectacles.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ Gem::Specification.new do |spec|
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
spec.require_paths = ["lib"]

spec.add_dependency "activerecord", "~> 7.0.0"
spec.add_dependency "activesupport", "~> 7.0.0"
spec.add_dependency "activerecord", "~> 7.1.0"
spec.add_dependency "activesupport", "~> 7.1.0"

spec.add_development_dependency "minitest", ">= 5.0"
spec.add_development_dependency "rake"
Expand Down
6 changes: 6 additions & 0 deletions test/support/schema_statement_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ def self.execute(query)
describe "ActiveRecord::SchemaDumper#dump" do
before(:each) do
ActiveRecord::Base.connection.drop_view(:new_product_users)
ActiveRecord::Base.connection.drop_view(:other_product_users)

ActiveRecord::Base.connection.create_view(:new_product_users) do
"SELECT name AS product_name, first_name AS username FROM
products JOIN users ON users.id = products.user_id"
end

ActiveRecord::Base.connection.create_view(:other_product_users) do
"SELECT name AS product_name, first_name AS username FROM
products JOIN users ON users.id = products.user_id"
end

if ActiveRecord::Base.connection.supports_materialized_views?
ActiveRecord::Base.connection.drop_materialized_view(:materialized_product_users)
ActiveRecord::Base.connection.drop_materialized_view(:empty_materialized_product_users)
Expand Down